特定の月に依存する名前で環境内にオブジェクトを作成する代わりに、名前として使用されるオブジェクトのリストを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)
}