The question is very similar to this one . It is for combining a list of data frames into a single longer data frame. However, I want to keep the information from which item of the list the data came from by adding an extra column with the index (id or source) of the list.
This is the data (borrowing code from the linked example):
dfList <- NULL
set.seed(1)
for (i in 1:3) {
dfList[[i]] <- data.frame(a=sample(letters, 5, rep=T), b=rnorm(5), c=rnorm(5))
}
Using the code below provides a concatenated data frame, but does not add the column for the list index.:
df <- do.call("rbind", dfList)
How do I concatenate the data frames in the list while creating a column to capture the origin within the list? Something like the following:
Thank you very much in advance.