3

I have followup question based on this one.

I have used df <- ldply(listOfDataFrames, data.frame) to combine a list of 12000+ dataframe into one, but since each dataframe in the list has no identifier, I need to know which dataframe comes from which list.

I know I can use ldply(test,nrow) to create another dataframe and then use a for-loop to add the list name, but it seems a bit slow, I wonder if there is any faster method. Thanks.

Community
  • 1
  • 1
lokheart
  • 23,743
  • 39
  • 98
  • 169

3 Answers3

2

I don't have a plyr solution for you, but this is how I usually do it in base R.

> a <- list(data.frame(a=runif(5), b=runif(5)), data.frame(a=runif(5), b=runif(5)), data.frame(a=runif(5), b=runif(5)))
> a
[[1]]
          a         b
1 0.2994804 0.2681471
2 0.3223587 0.3663688
3 0.2662296 0.2941038
4 0.8041538 0.2991932
5 0.6860321 0.0872916

[[2]]
           a          b
1 0.84966749 0.01750988
2 0.19320093 0.05274077
3 0.63218616 0.77222663
4 0.00773626 0.53163878
5 0.19965884 0.50740204

[[3]]
          a          b
1 0.2915164 0.65905466
2 0.5676906 0.01094598
3 0.5689014 0.58943383
4 0.7937997 0.75535177
5 0.2304010 0.84012697

> indices <- lapply(a, nrow)
> a.all <- do.call(rbind, a)
> a.all$index <- rep(1:length(a), indices)
> a.all
            a          b index
1  0.29948042 0.26814714     1
2  0.32235868 0.36636880     1
3  0.26622956 0.29410382     1
4  0.80415381 0.29919316     1
5  0.68603208 0.08729160     1
6  0.84966749 0.01750988     2
7  0.19320093 0.05274077     2
8  0.63218616 0.77222663     2
9  0.00773626 0.53163878     2
10 0.19965884 0.50740204     2
11 0.29151644 0.65905466     3
12 0.56769063 0.01094598     3
13 0.56890138 0.58943383     3
14 0.79379972 0.75535177     3
15 0.23040098 0.84012697     3
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
2

I'm guessing you've got reshape2 at your disposal as well. I generally approach this with melt:

library(reshape2)
#Thanks Roman
a <- list(data.frame(a=runif(5), b=runif(5)), data.frame(a=runif(5), b=runif(5)), data.frame(a=runif(5), b=runif(5)))
melt(a)

> melt(a, id.vars = 1:2)
          a        b L1
1  0.325542 0.914199  1
2  0.947871 0.719881  1
3  0.683925 0.574832  1
4  0.715612 0.646920  1
...
Chase
  • 67,710
  • 18
  • 144
  • 161
1

Again, using base R, you can name the list items:

# Also thank you Roman
a <- list(data.frame(a=runif(5), b=runif(5)), data.frame(a=runif(5),
  b=runif(5)), data.frame(a=runif(5), b=runif(5)))
names(a)<-seq_along(a)

myAns<-do.call("rbind",a)

Then the row names correspond to the names of the list items.

row.names(myAns)
BenBarnes
  • 19,114
  • 6
  • 56
  • 74