我有一个包含许多提示和内部 node 的系统发育树.我有一个树中的 node ID列表.这些是单独表格的一部分.我想在表中添加一个新列,children.为了获得子体( node 和提示),我使用phangorn::Descendants(tree, NODEID, type = 'all')
.我可以添加长度来获得子体的数量.例如
phangorn::Descendants(tree, 12514, type = 'all')
[1] 12515 12517 12516 5345 5346 5347 5343 5344
length(phangorn::Descendants(tree, 12514, type = 'all'))
[1] 8
我希望非常简单地在我的数据框" node "中获取列,并使用length(phangorn::Descendants(tree, 12514, type = 'all'))
以上的函数在数据框中基于输入 node 创建一个新列.
以下是一个示例:
tests <- data.frame(nodes=c(12551, 12514, 12519))
length(phangorn::Descendants(tree, 12519, type = 'all'))
[1] 2
length(phangorn::Descendants(tree, 12514, type = 'all'))
[1] 8
length(phangorn::Descendants(tree, 12551, type = 'all'))
[1] 2
tests$children <- length(phangorn::Descendants(tree, tests$nodes, type = 'all'))
tests
nodes children
1 12551 3
2 12514 3
3 12519 3
如上所示,子项的数量是数据的长度.框架,而不是上面计算的实际子级数.它应该是:
tests
nodes children
1 12551 2
2 12514 8
3 12519 2
如果你有什么建议或 idea ,我可以让这一切按预期进行,那就太好了.我觉得我必须使用apply()或者在使用length()函数之前需要在内部建立索引.提前谢谢你.