I have a list of dataframes that look like this>
crops_1990.tempor <- data.frame(study_unit=c("unit1", "unit2", "unit3"),
                                cropp=c("crop1", "crop2", "crop3"),
                                area=c(1,2,3),
                                year=NA)
crops_1991.tempor <- data.frame(study_unit=c("unit1", "unit2", "unit3"),
                                cropp=c("crop1", "crop2", "crop3"),
                                area=c(4,5,6),
                                year=NA)
crops_1992.tempor <- data.frame(study_unit=c("unit1", "unit2", "unit3"),
                                cropp=c("crop1", "crop2", "crop3"),
                                area=c(7,8,9),
                                year=NA)
df_list <- list(crops_1990.tempor, crops_1991.tempor, crops_1992.tempor)
I would like to fill the column 'year' with the year information that is in the name of each df within the list (1990, 1991 and 1992, respectively in this example).
I thought it would be very easy but I'm struggling a lot!
I've tried stuff like:
df_list <- lapply(df_list, function(x) {x$year <- as.character(x$year); x}) 
 
df_list <- lapply(df_list, function(x) {x$year <- substring(names(df_list), 7,10); x}) # add years from object name in list
but nothing seems to work. My expected result would be the dataframes within the list looking like this:
crops_1990.tempor <- data.frame(study_unit=c("unit1", "unit2", "unit3"),
                                cropp=c("crop1", "crop2", "crop3"),
                                area=c(1,2,3),
                                year=c("1990", "1990", "1990"))
crops_1991.tempor <- data.frame(study_unit=c("unit1", "unit2", "unit3"),
                                cropp=c("crop1", "crop2", "crop3"),
                                area=c(4,5,6),
                                year=c("1991", "1991", "1991"))
crops_1992.tempor <- data.frame(study_unit=c("unit1", "unit2", "unit3"),
                                cropp=c("crop1", "crop2", "crop3"),
                                area=c(7,8,9),
                                year=c("1992", "1992", "1992"))
 
     
    