[R] More elegant way of stacking the data

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Tue Nov 25 00:12:47 CET 2014


I have the data frame 'df' and my desired solution 'out'.
I am sure there is a more elegant R-way to do it - without a loop.

df = data.frame(a=1:5,b=letters[1:5],c1=1:5,c2=2:6,c3=3:7,c4=4:8)
mylist=NULL
for(i in 1:4){
  myname<-paste("c",i,sep="")
  mylist[[i]]<-df[c("a","b",myname)]
  names(mylist[[i]])<-c("a","b","c")
}
out<-do.call(rbind,mylist)
out

Thank you!

-- 
Dimitri Liakhovitski



More information about the R-help mailing list