gpt4 book ai didi

R - 替换向量列表中的元素

转载 作者:行者123 更新时间:2023-12-02 21:36:40 24 4
gpt4 key购买 nike

我有向量列表,想要将向量分配给它的一个位置(覆盖)。这是示例代码:

for (nodeId in names(chains)) {
chains[nodeId] <- unlist(chains[nodeId])[-1]
}

分配后,我收到许多警告,告诉我列表的长度不相等。我知道所发生的任务不是我想要的。

有没有办法将chains[nodeId]中的元素替换为对象unlist(chains[nodeId])[-1]

当我执行 str(chains)str(chains[nodeId])str(unlist(chains[nodeId])[-1]) 时 我得到以下输出:

$str(chains)
List of 15
$ 4 : chr [1:3] "root" "alcohol< 9.85" "totalSulfurDioxide>=60.5"
$ 10 : chr [1:4] "root" "alcohol< 9.85" "totalSulfurDioxide< 60.5" "sulphates< 0.575"
$ 22 : chr [1:5] "root" "alcohol< 9.85" "totalSulfurDioxide< 60.5" "sulphates>=0.575" ...
(...) lots more

$str(chains[nodeId])
List of 1
$ 4: chr [1:3] "root" "alcohol< 9.85" "totalSulfurDioxide>=60.5"

$str(unlist(chains[nodeId])[-1])
Named chr [1:2] "alcohol< 9.85" "totalSulfurDioxide>=60.5"
- attr(*, "names")= chr [1:2] "42" "43"

更新: str 替换为 dput;添加了dput(chains[nodeId])

$ dput(chains)
structure(list(`4` = "alcohol< 9.85", `10` = "alcohol< 9.85",
`22` = "alcohol< 9.85", `92` = "alcohol< 9.85", `93` = "alcohol< 9.85",
`47` = "alcohol< 9.85", `24` = "alcohol>=9.85", `50` = "alcohol>=9.85",
`102` = "alcohol>=9.85", `103` = "alcohol>=9.85", `26` = "alcohol>=9.85",
`27` = "alcohol>=9.85", `28` = "alcohol>=9.85", `29` = "alcohol>=9.85",
`15` = c("root", "alcohol>=9.85", "alcohol>=11.55", "sulphates>=0.685"
)), .Names = c("4", "10", "22", "92", "93", "47", "24", "50",
"102", "103", "26", "27", "28", "29", "15"))

$ dput(chains[nodeId])
structure(list(`15` = c("root", "alcohol>=9.85", "alcohol>=11.55",
"sulphates>=0.685")), .Names = "15")

$ dput(unlist(chains[nodeId])[-1))
structure(c("alcohol>=9.85", "alcohol>=11.55", "sulphates>=0.685"
), .Names = c("152", "153", "154"))

$ dput(chains[nodeId])
structure(list(`15` = "alcohol>=9.85"), .Names = "15")

我想要实现的是从链[nodeId]中的向量中删除第一个元素

最佳答案

如果chains是一个列表,nodeId是一个字符串,那么chains[nodeId]将是一个长度为1的列表。您需要chains[[nodeId]],其中包含该列表的内容。

关于R - 替换向量列表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21231222/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com