See the thread here: https://stackoverflow.com/a/79125997/11129415.
Same logic as used by Ben.
#random tree
oldTree <- ape::read.tree(text = "(t2, (t5, (t4, (t6, (t3, t1)))));")
plot(oldTree)
df <- cbind(old.name=oldTree$tip.label, new_name = c(rbind("tip_2", "tip_5", "tip_4","tip_6", "tip_3", "tip_1"))) %>% as.data.frame()
#Get the position of elements --> matching tip labels with the label of the #dataframe
pos_id <-match(oldTree$tip.label, df$old.name)
pos_id #element position
newTree <- oldTree
newTree$tip.label <- df$new_name[pos_id] #here sorting by pos_id
par(mfrow=c(1,2))
plot(oldTree)
plot(newTree)