从列表

时间:2016-02-15 10:24:31

标签: r

我正在寻找一种加入专栏的惯用方法,比如名为' x',它存在于列表的每个data.frame元素中。我使用lapplyReduce提出了一个包含两个步骤的解决方案。尝试仅使用Reduce的第二次尝试失败。我实际上只能使用Reduce和一个匿名函数来执行此操作吗?

#data
xs <- replicate(5, data.frame(x=sample(letters, 10, T), y =runif(10)), simplify = FALSE)
# This works, but may be still unnecessarily long
otmap = lapply(xs, function(df) df$x)
jotm = Reduce(c, otmap)
# This does not count as another solution:
jotm = Reduce(c, lapply(xs, function(df) df$x))

# Try to use only Reduce function. This produces an error
jotr =Reduce(function(a,b){c(a$x,b$x)}, xs)
# Error in a$x : $ operator is invalid for atomic vectors

1 个答案:

答案 0 :(得分:1)

我们可以在提取'x'列后docker run

unlist