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


Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
R-sig-phylo mailing list
R-sig-phylo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-phylo

Reply via email to