【发布时间】:2021-12-29 23:15:33
【问题描述】:
我有一个以列表形式返回输出的函数,如下所示。我只想从中提取变量“值”。
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))
【问题讨论】:
-
你试过 output[2,1] 和 output[2,2] 吗?并输出[2, ]。你真的应该发布 dput(output) 的结果
-
@IRTFM:感谢您的回复。我在我的问题中添加了 dput(output) 的结果。