I have a process which I would like to repeat a known number of times, but with a catch. The first iteration should be with the original dataset, then the next should be with the result of the first, the next with the result of the second, ......
Some background: the dataset is of type phylo
, so the append
function inside a for
loop doesn't make sense to me. Below is the actual code:
library(ape)
library(geiger)
clade.dropper <- function(phy, drop.tips) {
new.phy <- drop.tip(phy, tips(phy, drop.tips[1]))
new.phy <- drop.tip(new.phy, tips(new.phy, drop.tips[2]))
new.phy <- drop.tip(new.phy, tips(new.phy, drop.tips[3]))
new.phy <- drop.tip(new.phy, tips(new.phy, drop.tips[4]))
new.phy <- drop.tip(new.phy, tips(new.phy, drop.tips[5]))
new.phy
}
I would like to be able to prevent the hard coding of the above and somehow, loop it for a given list containing tip names of a phylogenetic tree to drop.
Thanks!
There are a couple of troubles with your approach - Trees nodes are redrawn every time you subset it - if you are subsetting by node numbers, each time you remove a node, on the next trim, your next node might not be the one you wanted (I have made this painful mistake before).
If you are subsetting by names, you might be ok, but most trees won't have node names.
What we can do is make a list of every tip you want to remove at once, and then trim all at once.
Using
geiger
andape
:first load a tree:
the tips function isnt properly vectorized, let's fix that:
Now we can drop them all at once:
For your example: