[R] apply over parallel lists and their elements

David Winsemius dwinsemius at comcast.net
Mon Sep 13 23:44:33 CEST 2010


On Sep 13, 2010, at 5:20 PM, Liviu Andronic wrote:

> Hello
>
> On Sun, Sep 12, 2010 at 5:37 PM, Sebastian Gibb <lists at sebastiangibb.de 
> > wrote:
>> Hello,
>>
>> thanks for your answer.
>> mapply fits to my needs.
>>
> One thing that seems strange is that if you use
> tree[[1]]$node$values <- 1:10
> tree[[2]]$node$values <- 3:12
>
> you still get
>> mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values)
> [1]  1  2  3  4  5  6  7  8  9 10
>
> But I cannot understand what's wrong.

The second argument to mean is trim. I am not sure what mean(1, 3) is  
supposed to do but what it return is 1. If you wanted 2,3,4 ..., 11  
then you would perhaps do:

mean( mapply(c, tree[[1]]$node$values, tree[[2]]$node$values) )

-- 
David
>
>
>> But I don't know how many items would "tree" have. I can't write  
>> them all by
>> hand.
>> How can I generate the arguments for mapply?
>>
> Unfortunately I cannot think of a solution. Perhaps reorganise your
> data, so that all relevant data points go into the same vector. For
> example,
> tree[[4]]<-list();
> tree[[4]][['node']]<-list();
> tree[[4]]$node$more[[1]] <- 1:10
> tree[[4]]$node$more[[2]] <- 3:12
>> mapply(mean, tree[[4]]$node$more)
> [1] 5.5 7.5
>
> Also check maply and mlply to see if they can fit your needs. [1]  
> Regards
> Liviu
>
> [1] http://had.co.nz/plyr/plyr-intro-090510.pdf
>
>
>
>> mapply(mean, tree[[1]]$node$values, tree[[2]]$node$values, ...
>> tree[[k]]$node$values);
>>
>> Kind regards,
>>
>> Sebastian
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> -- 
> Do you know how to read?
> http://www.alienetworks.com/srtest.cfm
> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
> Do you know how to write?
> http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list