Reputation: 1013
I want to save several objects in a loop in separate files, but I have problems getting it right. Below is the code (a highly simplified example of the thing I am doing). I want the objects a2014 and b2014 to be saved to a2014.Rdata and b2014.Rdata. The code clearly does not work as the object parametername is saved and not the objects a2014 and b2014.
for (i in c("a","b")) {
data <- c(1,2)
parametername <- paste0(i,"2014")
assign(parametername, data)
save(parametername, file = paste0(i, "2014.Rdata")) # This is clearly not working
}
Any help is appreciated.
Cheers
Renger
Upvotes: 0
Views: 133
Reputation: 388897
You can pass the object name in list
.
for (i in c("a","b")) {
data <- c(1,2)
parametername <- paste0(i,"2014")
assign(parametername, data)
save(list = parametername, file = paste0(i, "2014.Rdata"))
}
Upvotes: 1