【发布时间】:2017-05-05 18:18:48
【问题描述】:
有没有办法使用 lapply 制作以下函数列表?下面,我列出了一个名为“long”的列表。我想使用 lapply (或 for 循环)简化代码,但似乎都不起作用。谢谢。
#This works as expected
long <- list(function(x) x <- within(x, DATE <- as.Date("2013-01-01")),
function(x) x <- within(x, DATE <- as.Date("2014-01-01")),
function(x) x <- within(x, DATE <- as.Date("2015-01-01")))
#This does not work
long <- lapply(2013:2015, function(i) x <- within(x, DATE <- as.Date(paste(i, "01", "01", sep = "-"))))
#This does not work
for (i in 2013:2015) long <- list(function(x) x <- within(x, DATE <- as.Date(paste(i, "01", "01", sep = "-"))))
【问题讨论】:
标签: r list function auto-generate