我有一個函式,它以串列的形式回傳輸出,如下所示。我只想從中提取變數“值”。
output
[,1] [,2]
weights_used List,4 List,6
value 3 1
我可以單獨提取值,說
output[,1]$value
[1] 3
或者
output[,2]$value
[1] 1
如何在新串列中一起提取上面的值 3 和 1?(我的實際輸出會有更多的值)。如果我嘗試 output["value"] 或 output[["value"]],它們會產生一個 NULL
我也不明白為什么變數名“value”從 unlist(output) 中消失了..
> unlist(output)
weight1 weight2 weight3 weight4 weight1 weight2 weight3 weight4 weight5 weight6
0.1666667 0.1666667 0.1666667 0.5000000 3.0000000 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 1.0000000
謝謝!
> dput(output)
structure(list(list(weight1 = 0.166666666666667, weight2 = 0.166666666666667,
weight3 = 0.166666666666667, weight4 = 0.5), 3, list(weight1 = 0.166666666666667,
weight2 = 0.166666666666667, weight3 = 0.166666666666667,
weight4 = 0.166666666666667, weight5 = 0.166666666666667,
weight6 = 0.166666666666667), 1), .Dim = c(2L, 2L), .Dimnames = list(
c("weights_used", "value"), NULL))
uj5u.com熱心網友回復:
對我有用的內容如下所示..但我相信有更優雅的方式!
unlisted.output <- unlist(output)
as.list(unlisted.output[names(unlisted.output) == ""])
uj5u.com熱心網友回復:
只需按照矩陣格式并選擇帶有 name 的行'value'。
output['value', ]
# [[1]]
# [1] 3
#
# [[2]]
# [1] 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360827.html
下一篇:尋找最小平均值
