Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row Binding a Set of Data Sets?

I have read in a list of data sets and called this n. What I wish to do is take a subset of the data sets from n and row bind them together in R. When I try to go rbind(n) this just gives me the data frame of all the names of the data sets instead of actually putting the elements of each data set underneath each other. What I want to do is bind subsets of the data sets that share a common name. For example, 18 of the data sets start with "4." and I want to bind all these together. Is there an easy way to do this?

like image 251
user1836894 Avatar asked Feb 25 '13 02:02

user1836894


2 Answers

Want you want to do is rbind(n[[1]],n[[2]],...) which is not the same as rbind(n).

You don't need to write this out, you can use do.call to create and execute this call

 do.call(rbind, n)

which will run the command you want. However, this is notoriously slow

You can use rbindlist from the data.table package to do the same thing much faster

 library(data.table)

 rbindlist(n)

If you only want those elements whose name start with 4

rbindlist(n[grep(names(n), pattern = '^4')])
like image 163
mnel Avatar answered Sep 28 '22 01:09

mnel


If you try to aggregate many files, You might need the rbind.fill function in plyr package ( i don'konw if there is a data.table equivalent)

ll <- list(a=data.frame(x=1,y=2,z=1),
         b= data.frame(x=2,y=3),
         c=data.frame(x=3:4,y=5))

library(plyr)
Reduce(rbind.fill,ll[c('a','b')])   ## subset by list names and 
                                    ## apply recursively using Reduce
  x y  z
1 1 2  1
2 2 3 NA
like image 30
agstudy Avatar answered Sep 28 '22 03:09

agstudy