rename phylo tip labels

3.6k views Asked by At

I want to give a species a new name in my tree of class phylo (using the ape package).

I tried:

tree$tip.label["speciesX"] <- "speciesY"

This did not do what I wanted. Any suggestions?

1

There are 1 answers

0
Ben Bolker On

The problem is that you can't index the tip labels the way you want (you want to replace the tip label whose value is "speciesX", not the one whose name is "speciesX"; the tip label vector doesn't have names). Silly as it sounds, you need something like tree$tip.label[tree$tip.label=="speciesX"] to identify the right value to replace.

Example:

## create a tree, from ?read.tree
s <- "owls(((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3):6.3,Tyto_alba:13.5);"
cat(s, file = "ex.tre", sep = "\n")
tree.owls <- read.tree("ex.tre")

Rename:

tree.owls$tip.label[tree.owls$tip.label=="Asio_otus"] <- "something_else"

You could write a function to do this, something like (not tested!)

rename.tips <- function(phy, old_names, new_names) {
   mpos <- match(old_names,phy$tip.labels)
   phy$tip.labels[mpos] <- new_names
   return(phy)
}