我在列表中有几个(命名)载体:有没有简单的方法从列表中提取特定的值组合?
data = list(a=runif(n = 50, min = 1, max = 10), b=runif(n = 50, min = 1, max = 10), c=runif(n = 50, min = 1, max = 10), d=runif(n = 50, min = 1, max = 10))
我想玩弄依赖于从另一个阵列称为梳子行它们的不同组合:
var <- letters[1:length(data)]
combs <- do.call(expand.grid, lapply(var, function(x) c("", x)))[-1,]
我想能够提取每个组合,以便我可以使用由这些组合创建的向量。
所有这些都能够将函数应用于提取的每一行,然后应用于这些数据框的每个组合。因此,例如:
# Row 5 is "a", "c"
combs[5,]
# Use this information to extract this particular combination from my data:
# by hand it would be:
res_row5 = cbind(data[["a"]], data[["c"]])
# Extract another combination
# Row 11 is "a", "b", "d"
combs[11,]
res_row11 = cbind(data[["a"]], data[["b"]], data[["d"]])
# So that I can apply functions to each row across all these vectors
res_row_5_func = apply(res_row5, 1, sum)
# Apply another function to res_row11
res_row_5_func = apply(res_row11, 1, prod)
# Multiply the two, do other computations which can do as long as I have extracted the right vectors
我已经问了一个非常类似的问题在这里:Is there an easy way to match values of a list to array in R?
但无法弄清楚如何这么多提取的实际数据... 谢谢!
这是你想要的东西'as.data.frame(数据)[梳子[ ,5]]或数据[梳子[,5]]?我会使用'data.frame'而不是'list',因为数据中的向量具有相同的长度。 – mt1022
不清楚给我。你可以用最终的期望输出更新你的文章吗? –
感谢您的帮助...我认为一个清单更合适的原因是我在每个名称中都存储了不同的信息... – user971102