I am trying to create a suite of dynamically named new objects (e.g., temp2015
) using a for loop, and storing dynamic values - specifically, the names of other objects (e.g., Y2015
) and the value used in the for loop (e.g., 2015
) - in the dynamically named new objects.
I am not sure why the code below is not working.
Y2015 = data.frame(VAR1 = 6:10, VAR2 = 1:5)
Y2016 = data.frame(VAR1 = 7:11, VAR2 = 11:15)
Y2017 = data.frame(VAR1 = 7:11, VAR3 = 5:9, VAR4 = 4:8)
for (i in 2015:2017) {
paste0("temp", i) <- cbind(names(paste0("Y", i)), i)
}
all <- rbind(temp2015, temp2016, temp2017)
The code below gets the result I want (specifically, the data content in the object all_manual
) but this was done manually, on a very small sample dataset, and isn't a long-term solution.
Y2015 = data.frame(VAR1 = 6:10, VAR2 = 1:5)
Y2016 = data.frame(VAR1 = 7:11, VAR2 = 11:15)
Y2017 = data.frame(VAR1 = 7:11, VAR3 = 5:9, VAR4 = 4:8)
all_manual = data.frame(file = c('VAR1', 'VAR2', 'VAR1', 'VAR2', 'VAR1', 'VAR3', 'VAR4'),
year = c(2015, 2015, 2016, 2016, 2017, 2017, 2017))
I'd greatly appreciate any advice or ideas about how to get this working.