我想在我的类树中给一个物种一个新名称phylo
(使用ape
包)。
我试过了:
tree$tip.label["speciesX"] <- "speciesY"
这没有达到我想要的效果。有什么建议么?
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)
}