I share Ben's thought. I gave it a try with simulated trees and an
analysis similar to Stacey's:
R> tr <- rmtree(1e3, 100)
R> system.time(lapply(tr, drop.tip, paste("t", 1:5, sep = "")))
utilisateur système écoulé
17.278 0.050 17.359
R> system.time(for (i in 1:1000) tr[[i]] <- drop.tip(tr[[i]], paste("t",
1:5, sep = "")))
utilisateur système écoulé
46.953 0.330 47.322
Here's one case that I remember to have compared a for loop with apply
and found the former faster:
R> x <- matrix(rnorm(1e6), 1e3, 1e3)
R> system.time(apply(x, 1, mean))
utilisateur système écoulé
0.087 0.003 0.092
R> system.time({m <- numeric(1e3); for (i in 1:1e3) m[i] <- mean(x[i, ])})
utilisateur système écoulé
0.070 0.000 0.071
EP
Le 03.09.2008 12:49, [EMAIL PROTECTED] a écrit :
Hi Ben,
It may sound like folklore but I was in a summer workshop that
Marguerite taught
and we saw some stats showing the computational time difference. Not
that I can
recall exactly how much time using apply functions saved, but the take home
message was that they were always much faster.
Stacey
Quoting Ben Bolker <[EMAIL PROTECTED]>:
Is there a difference between lapply() and for loops
in timing for this application? I'd be mildly surprised.
There is folklore (stemming from some limitations of S-PLUS)
that lapply is faster than for loops, but I think it's
generally not true -- the advantages of lapply are generally just
compactness/logical structure. (On the other hand,
apply() and other vectorized operations may indeed be faster
than for loops.)
Ben Bolker
[EMAIL PROTECTED] wrote:
Thanks, Emmanuel. I used lapply because the operation (pruning a bunch
of tips
from 1000s of trees) took a long time, but for less intensive jobs, it's
good
to know that the for-loop will not have the effect of discarding the
object's
attributes.
Stacey
Quoting Emmanuel Paradis <[EMAIL PROTECTED]>:
Since lists of trees have various attributes, it is better to use a
'for' loop for this kind of operation:
for (i in 1:length(phylist))
phylist[[i]]$tip.label <- c("G","H","I","J","K")
That's because lapply returns a list with the results, eventually
discarding the original attributes.
Le 01.09.2008 03:02, Simon Blomberg a écrit :
Hmm. I should try solutions before I post them. You need to make sure
that each tree in the list is of class phylo too. This works:
phylist<-read.tree(text="((((A:0.21,B:0.21):0.28,C:0.49):0.13,D:0.62):0.38,E:1.00);((((A:0.21,B:0.21):0.28,C:0.49):0.13,D:0.62):0.38,E:1.00);")
newnames<-c("G","H","I","J","K")
newlist <- lapply(phylist,
function(z) {
z$tip.label <- c("G","H","I","J","K")
class(z) <- "phylo"
This shouldn't be necessary because you modify an element of the
object.
I've seen that the help page of write.tree needs to be updated since a
list a trees is accepted. The function may also be modified to accept
lists without the class "multiPhylo" (eg, if returned by lapply).
EP
z
})
class(newlist) <- "multiPhylo"
write.tree(newlist,file="newlist")
On Mon, 2008-09-01 at 10:55 +1000, Simon Blomberg wrote:
Try class(newlist) <- "multiPhylo"
Then use write.tree.
Cheers,
Simon.
On Sun, 2008-08-31 at 20:39 -0400, [EMAIL PROTECTED] wrote:
Hi all,
I have hit an obstacle and I hope someone will know a quick fix. I
want to read
a list of trees, do something to those trees and then write them to
a file. The list is seen as multiPhylo until I apply some function
then it becomes a
list that I cannot write to a file with write.tree. I put an
example below,
where I read in two trees, and then use a function to change the
tip names and
then try to write the trees to a file.
Thanks in advance for your help!
Stacey
phylist<-read.tree(text="((((A:0.21,B:0.21):0.28,C:0.49):0.13,D:0.62):0.38,E:1.00);
((((A:0.21,B:0.21):0.28,C:0.49):0.13,D:0.62):0.38,E:1.00);")
class(phylist)
[1] "multiPhylo"
newnames<-c("G","H","I","J","K")
newlist <- lapply(phylist,
+ function(z) {
+ z$tip.label <- c("G","H","I","J","K")
+ z
+ })
write.tree(newlist,file="newlist")
Error in write.tree(newlist, file = "newlist") :
object "phy" is not of class "phylo"
(and yes, write.tree did work on the multiphylo object before I did
the
function)
_______________________________________________
R-sig-phylo mailing list
R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
--
Emmanuel Paradis
IRD, Montpellier, France
ph: +33 (0)4 67 16 64 47
fax: +33 (0)4 67 16 64 40
http://ape.mpl.ird.fr/
_______________________________________________
R-sig-phylo mailing list
R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo
--
Emmanuel Paradis
IRD, Montpellier, France
ph: +33 (0)4 67 16 64 47
fax: +33 (0)4 67 16 64 40
http://ape.mpl.ird.fr/
_______________________________________________
R-sig-phylo mailing list
R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo