rtreephylogeny

Rename phylo tip labels with separate df


I am looking to rename 1290 tip labels in my phylo tree using a second df. I am able to rename one label at a time using the below code:

phylo$tip.label[phylo$tip.label=="e54924083c02bd088c69537d02406eb8"] <- "something"

but this is clearly inefficient. How may I rename all labels with a second df that contains the original tip labels and the new labels? I can provide example data if necessary (the files are very large).

Thanks!


Solution

  • Let's take an example tree:

    oldTree <- ape::read.tree(text = "(t6, (t5, (t4, (t3, (t2, t1)))));")
    old <- c("t1", "t3", "t6")
    new <- c("tip_1", "tip_3", "tip_6")
    plot(oldTree)
    

    Start tree

    A robust approach would be:

    tree <- oldTree
    tree$tip.label[match(old, tree$tip.label)] <- new
    plot(tree)
    

    enter image description here

    Thomas's code will only work if the order of labels is identical in tree$tip.label and old.labels.

    tree <- oldTree
    my_data_frame <- data.frame(old.labels = old,
                                new.labels = new)
    
    ## Find the old labels in the tree
    tree$tip.label[tree$tip.label %in% my_data_frame$old.labels] <- my_data_frame$new.labels
    
    plot(tree)
    

    Here, tips have the wrong labels: Error