Friday, June 6, 2014

Generic rep (replicate) function for objects of class "phylo" & "multiPhylo"

Again, working on something else, I realized that there is no generic rep (replicate elements of vectors or lists) function for objects of class "phylo" or "multiPhylo". These were straightforward to do (although undoubtedly could've been programmed more elegantly & they use c.phylo & c.multiPhylo. The only hitch was in appending an additional tree to a list (object of class "multiPhylo"), we can't use c.multiPhylo without doing:

obj<-c(obj,list(x))

Here is the code:

rep.phylo<-function(x,...){
  if(hasArg(times)) times<-list(...)$times
  else times<-(...)[[1]]
  for(i in 1:times)
    obj<-if(i==1) x else if(i==2) c(obj,x) else
      c(obj,list(x))
  class(obj)<-"multiPhylo"
  obj
}

rep.multiPhylo<-function(x,...){
  if(hasArg(times)) times<-list(...)$times
  else times<-(...)[[1]]
  for(i in 1:times) obj<-if(i==1) x else if(i>=2) c(obj,x)
  class(obj)<-"multiPhylo"
  obj
}

I will add to a future version of phytools.

No comments:

Post a Comment

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.