I'm trying to load multiple csv files into R and merge them all into one large data frame. The files I'm reading in are titled with the year that data was taken from, e.g. BirthWeight1999.csv, BirthWeight2000.csv, BirthWeight2001.csv. What I want to do is create a new column for each csv file to give the year the data originates from. For example, the file contains the columns, MotherWeight, Alcohol, BabyWeight and I want to include a new one called Year and it have the value from the files name. So if the file is BirthWeight1999, the Year column should contain 1999. I'm having real difficulty in finding out how to do this. I've got the files reading in...
filenames = list.files(dir())
do.call("rbind", lapply(filenames, read.csv, header = TRUE)
Any help is appreciated.
Thanks :)