Saturday, March 26, 2016

Applying The Same Operation On Multiple Data Frames In R

Solution #1:

a<-c(1,2,3,4)
b<-c(5,6,7,8)
pos1<-data.frame(cbind(a,b))
pos2<-pos1+1
pos3<-pos1+2

var1<-paste0('pos',1:3)
exps<-paste0(var1,'$x<-',var1,'$a+10')

for(exp in exps){
  eval(parse(text=exp))
}

Solution #2: Use lapply

x<-list(pos1,pos2,pos3)

lapply(x, function(x) x$x<-x$a+10)

lapply(x,function(x)cor(x[,2],x[,3]))


http://stackoverflow.com/questions/16115745/applying-a-set-of-operations-across-several-data-frames-in-r

http://stackoverflow.com/questions/19249303/applying-lapply-on-multiple-data-frames-in-a-list-r

https://www.datacamp.com/community/tutorials/r-tutorial-apply-family



No comments:

Post a Comment