Add id to each row of dataframe before / after using ldpy to concatenate list of data into one
I have the following question based on this one .
I used df <- ldply(listOfDataFrames, data.frame)
to concatenate a list of 12000 + dataframe into one, but since each dataframe in the list has no ID, 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 for-loop
to add the list name, but it seems a little slow, I wonder if there is any faster method. Thank.
source to share
I don't have a plyr solution for you, but this is how I usually do it in the R base.
> 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
source to share
I assume you have reshape2
. I usually 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
...
source to share
Again, using base R, you can name the elements of the list:
# 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)
The string names then match the names of the list items.
row.names(myAns)
source to share