In stead of creating objects in the environment with a specific, month dependent, name, I would use a list of objects where month
is used as name.
dat = lapply(1:4, function(x) letters)
names(dat) = c("Jan","Feb","Mar","Apr")
> dat
$Jan
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
$Feb
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
$Mar
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
$Apr
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
Saving this list can be done easily using save(dat)
. If you are keen on saving the months in separate objects:
lapply(names(dat), function(month) {
save(dat[[month]], file = sprintf("%s.rda", month)
})
or using the good old for loop:
for(month in names(dat)) {
save(dat[[month]], file = sprintf("%s.rda", month)
}