我正在寻找一种加入专栏的惯用方法,比如名为' x',它存在于列表的每个data.frame元素中。我使用lapply
和Reduce
提出了一个包含两个步骤的解决方案。尝试仅使用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
答案 0 :(得分:1)
我们可以在提取'x'列后docker run
unlist