Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add identifier to each row of dataframe before/after use ldpy to combine list of dataframes into one

Tags:

list

dataframe

r

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.

like image 649
lokheart Avatar asked Dec 29 '25 01:12

lokheart


1 Answers

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
like image 91
Roman Luštrik Avatar answered Dec 31 '25 14:12

Roman Luštrik