You need to enable JavaScript to run this app. 如何对multiPhylo类列表循环执行phytools包物种添加函数
优惠活动
大模型
产品
解决方案
定价
更多

如何对multiPhylo类列表循环执行phytools包物种添加函数

系统发育树批量添加物种循环操作问题与解决

问题描述

我删除了此前表述不清的旧问题,本次想请教如何对列表中的元素循环执行函数。以下是我使用phytools包编写的示例代码:

# 一组系统发育树
List <- list(tree1= pbtree(n=3, tip.label = c("Pycnonotus_simplex", "Pycnonotus_brunneus", "Blythipicus_rubiginosus")),
          tree2 = pbtree(n=3, tip.label = c("Blythipicus_rubiginosus", "Pycnonotus_erythropthalmos", "Orthotomus_atrogularis"))) 

class(List) <- "multiPhylo"

# 待添加的物种向量
spp <- c("Pycnonotus_sp.02", "Pycnonotus_sp.08")

我计划通过add.species.to.genus函数将spp中的物种随机添加到树中,单棵树的实现代码如下:

for (i in 1:length(spp)){
    tree1 <- add.species.to.genus(tree1, spp [i], where = "random")
}

解决方法(更新)

我已经自行解决了该问题:使用add.species.to.genus函数会导致树的边长精度受限,搭配force.ultrametric函数即可解决,实现代码如下:

for (i in 1:length(List)){
  tree[[i]] <- List[[i]]
  for (j in 1:length(spp)){
    tree[[i]] <- force.ultrametric(tree[[i]])
    tree[[i]] <- add.species.to.genus(tree[[i]], spp[j], where = "random")
  }
}

内容的提问来源于stack exchange,提问作者Anh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 07:33:01