Consider a data frame (x) with 2 variables, x1 and x2, having equal values. 
It looks like:

x1   x2
1    1
2    2
3    3

Now, consider a second data frame (xk):
xk1   xk2
0.5    0.5
1.0    0.5
1.5    0.5
2.0    0.5
0.5    1
1.0    1
1.5    1
2.0    1
0.5    1.5
1.0    1.5
1.5    1.5
2.0    1.5
0.5    2
1.0    2
1.5    2
2.0    2

I have written code to calculate some differences between these two data
sets; the main idea is to subtract off each element of xk1 from each value
of x1, and similarly for xk2 and x2.  This is what I have:

w1 <- array(NA,dim=c(nrow(xk),length(x$x1)))
w2 <- array(NA,dim=c(nrow(xk),length(x$x2))) 
for (j in 1:nrow(xk)) {
        w1[j,] <- abs(x$x1-xk$xk1[j])
        w2[j,] <- abs(x$x2-xk$xk2[j])
}

Is there  a way to do the above calculation without use of a FOR loop? 
Thank you

Derek


-- 
View this message in context: 
http://www.nabble.com/alternate-methods-to-perform-a-calculation-tf4344469.html#a12376906
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@stat.math.ethz.ch 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