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?