# [R] Help with functions within a list

Altaweel, Mark R. maltaweel at anl.gov
Fri Mar 20 18:00:20 CET 2009

```I will try to be clearer with what I wanted.

What I am trying to do is take a list, say defined as res, that has a size of 1000

For example:

res[]                      res[]                      .....until res[]
----------------------------------------------
Id    X     Tick        Id      X     Tick             ........
1     2.2   1            1       1.4   1
2     3.1   1            2       3.2   1
1     1.2   2            1       1.1   2
2     2.2   2            2       3.0   2

Now, say I want to get the mean of Id=1 or Id=2 for all instances where Tick=1 or Tick=2. So in the example, the result for X when Id=1 and Tick=1 is: 1.8 (i.e., the average between 2.2 and 1.4 in res[] and res[]). However, in reality I would calculate the mean for all 1000 instances of Tick=1 and Id=1. I would also do this for all Id and Tick values.

So basically this is what I would like to do for all the 1000 elements in my list. Take the Id and Tick values for each of the elements and find the mean of the x value for all 1000 instances that occur for the given Id and Tick values.

It would be nice to get the result in a form such as:

result<- (some function that does what I want for all Ids and Tick values in the 1000 element list)

result
--------------------
Id    meanX    Tick
1      1.8          1
2      3.15        1
1      1.15        2
2      2.6          2

This is what I tried in order to get the mean for all tick values less than 601 and Ids greater than 0:

weightX<-sapply(res, function(.df) {mean(.df\$X[.df\$Id>0 & .df\$Tick<601])})

This does not work as it seems to not provide the mean across all the elements and ids included in the conditional. I think the result is it just overwrites the previous answer so my final results is not as large as I would expect.

Mark

-----Original Message-----
From: baptiste auguie [mailto:ba208 at exeter.ac.uk]
Sent: Fri 3/20/2009 4:32 AM
To: Altaweel, Mark R.
Cc: r-help at r-project.org
Subject: Re: [R] functions within a list

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
> and provide commented, minimal, self-contained, reproducible code.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter