useR's,

Consider the variables defined below:
yvals <- c(25,30,35)
x1 <- c(1,2,3)
x2 <- c(3,4,5)
x3 <- c(6,7,8)
x <- as.data.frame(cbind(x1,x2,x3))
delta <- c(2.5, 1.5, 0.5)
h <- delta/2
vars <- 3
xk1 <- seq(min(x1)-0.5, max(x1)+0.5, 0.5)
xk2 <- seq(min(x2)-0.5, max(x2)+0.5, 0.5)
xk3 <- seq(min(x3)-0.5, max(x3)+0.5, 0.5)
xks <- list(xk1,xk2,xk3)
xk <- do.call("expand.grid", xks)       

I want to perform several calculations with these variables.  For only two
variables, my code is successful, however, here there are three variables
(x1,x2,x3) and there could really be n variables (x1,...xn) if need be. 
Below is code that works for two variables, I want to convert it to work for
any number of variables.  Any ideas?
yk <- numeric(nrow(xk))
for (j in 1:nrow(xk)) {
        w1 <- abs(x[,1] - xk$x1[j])
        w2 <- abs(x[,2] - xk$x2[j])
        Ik1 <- which(w1 <= h[1])
        Ik2 <- which(w2 <= h[2])
        Ik <- intersect(Ik1, Ik2)
        YIk <- yvals[Ik]
        yk[j] <- mean(YIk)     
}
The result is the one-dimensional vector yk.  Does anyone know how to extend
this code to cover any number of variables?  Thanks in advance.

dxc13
-- 
View this message in context: 
http://www.nabble.com/extending-code-to-handle-more-variables-tp15604234p15604234.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to