我在列表中有幾個(命名)載體:有沒有簡單的方法從列表中提取特定的值組合?
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