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?
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')])
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With