I have a list of dataframes x
with column V1
that says the country and V2
that says the year.
The names of the dataframes are y1, y2, y3 and so on. I want to rename them so that they show the country and year (ALBANIA1993, JAPAN2002, etc.). The following code works individually on the dataframes:
y1 <- as.data.frame(cbind(c("ALBANIA", "ALBANIA", "ALBANIA"), c(1999, 1999, 1999)))
y2 <- as.data.frame(cbind(c("JAPAN", "JAPAN", "JAPAN"), c(2002, 2002, 2002)))
x <- list(y1, y2)
assign(as.character(with(y1, paste0(y1$V1[1], y1$V2[1]))), y1)
assign(as.character(with(y2, paste0(y2$V1[1], y2$V2[1]))), y2)
I am trying to use lapply
to apply this to all dataframes from list x
at once.
Here's the code:
x <- list(y1, y2)
x <- lapply(x, function(y) assign(as.character(with(y, paste0(y$V1[1], y$V2[1]))), y))
As you can see, it is the same code as before, but inside lapply
. For some reason, the dataframes in x
don't get renamed and there's no error shown.
Really appreciate the help! I'll edit in case you want more info.