假设您有一个环境中已经存在的 data.frames 列表:
library(magrittr)
lapply(
paste0("z", 2011:2015),
function(x) assign(
x,
data.frame(x=rnorm(10),y=rnorm(10)),
pos = 1
)
)
# should create z2011 through z2015 in your R env
我想做的是:提取一列,将它们组合成一个 data.frame,然后添加一个额外的变量来使用 magrittr 语法识别它们的来源。
ldply(list)
我意识到使用其他技术(即: 、、、、rbind.fill(listing)
)这是微不足道 rbind_all(listing)
的do.call(rbind,...)
。我的问题的重点是理解使用magrittr
语法的方法。
df <-
paste0("z",2011:2015) %>%
lapply(get) %>%
lapply(function(x) extract2(x,"x")) %>%
# what would you do next? Another approach you think is
# more appropriate for magrittr?
我不知道如何添加新变量。例如,我想结束以下内容:
do.call(
rbind,
lapply(
paste0("z",2011:2015),
function(x) {
data.frame(x = get(x)$x, year = x)
}
)
)