r - Perform numerical operation in subset of IDs and return the results in the same data frame -
i have large dataset consisting of longitudinal measurements in various subjects (ids) , variables lets say:
test.df <- data.frame(id=c(rep("a", 50),rep("b", 50)), x1=rnorm(100), x2=rnorm(100))
i want perform numerical operation on records of each id , return results in same dataset.
right doing is:
test.df <- data.frame(id=c(rep("a", 50),rep("b", 50)), x1=rnorm(50), x2=rnorm(50)) test.df$mean.of.x1<-na test.df$mean.of.x2<-na for(i in unique(test.df$id)){ test.df$mean.of.x1[test.df$id==i]<-mean(test.df$x1[test.df$id==i]) test.df$mean.of.x2[test.df$id==i]<-mean(test.df$x2[test.df$id==i]) }
the example simplistic (and perhaps silly), shows need (in original problem there several function run each id not mean
). there more efficient way this? can *apply
function help?
transform(test.df, mean.of.x1 = ave(x1, id, fun=mean), mean.of.x2 = ave(x2, id, fun=mean))
Comments
Post a Comment