I have a large list in R. It comprises 20 different dataframes, each with two variables (columns). These variables are the same for each of the dataframes within the large list. I am collapsing the dataframe with the rbindlist
function from the data.table package. This works successfully, yielding a single dataframe with all of the observations of the 2 variables. However, I would like to add a third variable for each list (and for the ultimate dataframe) that contains the number of the list that each unit/observation is contained in.
For example: Unit 1 is (1, 235) and is located in the first list. Unit 1 in the new dataframe should now be (1,235,1) in the new dataframe. Unit 2 is (2, 248) and is located in the first list. Its three columns' values should now be (2,248,1) in the new dataframe. Unit 3 is (3,78), but it is located in the second list. So its three columns' values should now be (3,78,2).
Is it possible to preserve the number/placement of the individual unit within the large list when collapsing it into one dataframe?
CodePudding user response:
Are you trying to do this?
library(dplyr)
df1 <- data.frame(v1 = 1:5, v2 = sample(50:60, 5))
df2 <- data.frame(v1 = 6:10, v2 = sample(50:60, 5))
df3 <- data.frame(v1 = 11:15, v2 = sample(50:60, 5))
l <- list(df1, df2, df3)
bind_rows(l, .id = "id")
#> id v1 v2
#> 1 1 1 58
#> 2 1 2 54
#> 3 1 3 56
#> 4 1 4 57
#> 5 1 5 60
#> 6 2 6 57
#> 7 2 7 56
#> 8 2 8 55
#> 9 2 9 60
#> 10 2 10 52
#> 11 3 11 59
#> 12 3 12 51
#> 13 3 13 52
#> 14 3 14 55
#> 15 3 15 53
Created on 2022-07-08 by the reprex package (v2.0.1)
I don't know about data.table
much, that's why used dplyr
instead