0

So i have a list of 20 data frames. Each data frame represents a year (1980-2000), id like to merge all my data frames into one but have a column representing the year of each data frame so i can continue to manipulate by time. All data frames have the same column names in the list. So merging should not be hard but i need to identify each data frame.

Example:
list of data frames
df1  <- Name2000
df2  <- Name1999
.
.
.
df20 <- Name1980

What im looking for

TotalDF:

x1 x2 x3 new_var
x  x  x  2000
x  x  x  2000
x  x  x  1999
x  x  x  1999
.  .  .  . 
.  .  .  . 
x  x  x  1980

I got my list of dataframes doing this:

filenames <- list.files(path="C:/Users/clint/Documents/R/Personal 
work/Fires/rain/", full.names=TRUE)

All <- lapply(filenames,function(i){
  i <- paste("",i,sep="")
  read.csv(i, header=FALSE)
})
filenames <- gsub("-",".",filenames)
names(All) <- gsub(".csv","",filenames)
Clinton Woods
  • 239
  • 1
  • 2
  • 11

1 Answers1

2

For additional reference,

dplyr::bind_rows([list], .id='year')

is probably the easiest way assuming you've named the list elements by year already.

Jul
  • 1,069
  • 6
  • 11