[R] functions within a list

baptiste auguie ba208 at exeter.ac.uk
Fri Mar 20 10:32:07 CET 2009


Hi,


you could have a look at the doBy package which makes these operations  
easier. Hadley's plyr package is also another option.

baptiste

On 20 Mar 2009, at 01:01, Altaweel, Mark R. wrote:

> Hi,
>
> I am trying to perform various functions on a list with a number of  
> elements. For example. I would like to take the mean of different  
> variable values in the entire list. As an example,
> say I have a list with 1000 elements and variables called Id and  
> Tick. What I would like to do is take the mean of a variable called  
> X for each Tick in the data element. So, there can be say 1 to 600  
> tick values per element in a list, that would mean I would like to  
> find the 600 mean values for each of the ticks values in the 1000  
> elements.
>
>
> I tried a simple attempt below, but I am sure it is way off as it  
> didn't produce what I expected:
>
> //res=res[[1..1000]]
> weightX<-sapply(res, function(.df) {mean(.df$X[.df$Id>0 & .df 
> $Tick<601])})
>
> Basically, I was trying to get the mean for all tick values less  
> than 600 that have an Id variable greater than 0. So, since there  
> are 600 ticks I would like to return a result with 600 mean values,  
> for each of the 600 ticks, that factors all the 1000 occurrences of  
> each tick, starting from 1.
>
> I hope this is clear. Thanks in advance.
>
> Mark
>
> ______________________________________________
> 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list