[R] How do I vectorize this loop....
chipmaney at hotmail.com
Wed Oct 21 23:58:09 CEST 2009
Basically I need to use the following data to calculate a squared error for
each Sample based on the expected Survival for the zone.
Basically, this code has Live/Dead for each sample, and I need to calculate
the square error based on the Expected Mean (ie, Survival). The code looks
up the expectation for each zone and applies for each sample in the zone
using a loop:
Data1 <- data.frame(Sample=1:6, Live =c(24,25,30,31,22,23), Baseline =
c(35,35,35,32,34,33),Zone = c(rep("Cottonwood",3),rep("OregonAsh",3)))
Data2 <- data.frame(Zone = c("Cottonwood","OregonAsh"), Survival =
for (i in 1:nrow(Data1)) #####(Yi -Ybar*Yo)^2
Data1$SquaredError[i] <- (Data1$Live[i] -
My question is, can I vectorize this code to avoid the loop? Obviously, I
could merge the 2 datasets first, but that would still require 2 steps and
Data1 would have a bunch of redundant data. So, is there a better
alternative? Is there some way I improve indexing syntax efficiency by
using rownames instead of a column vector?
View this message in context: http://www.nabble.com/How-do-I-vectorize-this-loop....-tp26000933p26000933.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help