我有矢量列表,并且想要将矢量指定给它的一个位置(覆盖)。以下是示例代码:R - 替换矢量列表中的元素
for (nodeId in names(chains)) {
chains[nodeId] <- unlist(chains[nodeId])[-1]
}
分配后我收到许多警告,告诉我列表长度不相等。我明白,发生的任务不是我想要的。
有什么办法可以用unlist(chains[nodeId])[-1]
代替chains[nodeId]
中的元素吗?
当我做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]
你可以让你的问题重现吗?什么是输出(组成一些,或使用'dput'),期望的结果是什么?你使用哪些代码无效(你或多或少提供了这些代码)? –
你到底想做什么?你能举一个例子,说明手术前后列表的样子吗? –