【问题标题】:How to write a loop to calculate a score for the number of simulations repeatedly?如何编写循环来重复计算模拟次数的分数?
【发布时间】:2015-11-11 02:21:05
【问题描述】:

我需要一个非常简单的循环,从 multiphylo 对象中获取每棵系统发育树,并计算每棵树的 Ic 并将其放入数据框中。对不起,如果它这么简单,我是 R 新手,我想不通!

    library(apTreeshape)
    library(phytools)
   multi_trees<- pbtree(b=0.5, d=0, n=200, t=NULL, scale=NULL, 
  nsim=50, type="continuous", extant.only=TRUE) ### simulate 50 trees stored as multiphylo object
    converted_tree <- as.treeshape(multi_trees[[nsim=1]]) ## each tree need to be converted to class treeshapes using following function

 Ic <- colless(converted_tree, norm = NULL) #And finally calculate Ic for each tree

【问题讨论】:

    标签: r loops tree phylogeny


    【解决方案1】:

    循环很简单,所以我假设它是不言自明的。

        library(apTreeshape)
        library(phytools)
    
        # simulate 50 trees stored as multiphylo object
        multi_trees<- pbtree(b=0.5, d=0, n=200, t=NULL, scale=NULL, nsim=50, type="continuous", extant.only=TRUE)
    
        # make data.frame for results
        Ic <- matrix(nrow=50, ncol=2)
        Ic <- data.frame(Ic)
        colnames(Ic) <- c("sim", "Ic")
    
        # loop
        for (i in 1:50) {
            converted_tree <- as.treeshape(multi_trees[[i]])
            Ic[i,1] <- i # get simulation number
            Ic[i,2] <- colless(converted_tree, norm = NULL) # get Ic
        }
    
        Ic
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 2021-04-23
      • 2012-10-31
      • 1970-01-01
      • 2014-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多