我不会在环境中创建具有特定的、依赖于月份的名称的对象,month
而是使用用作名称的对象列表。
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"
保存此列表可以使用save(dat)
. 如果您热衷于将月份保存在单独的对象中:
lapply(names(dat), function(month) {
save(dat[[month]], file = sprintf("%s.rda", month)
})
或使用旧的 for 循环:
for(month in names(dat)) {
save(dat[[month]], file = sprintf("%s.rda", month)
}