calculating mean of list components

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

calculating mean of list components

jamaas53
I get a list object from an iterative function.  I'm trying to figure
out the most efficient way to calculate the mean of one element, across
all components of the overall list.

I've tried

output <- mean (listobject[[1:5]]$element)

to get the mean of "element" in the first five components?

It doesn't like the $.  I'm suspect one of the "apply" functions will
work, but again I've not had success


Thanks a bunch.

J

----
Jim Maas
University of East Anglia

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Odp: calculating mean of list components

PIKAL Petr
Hi

[hidden email] napsal dne 15.12.2010 14:22:15:

> I get a list object from an iterative function.  I'm trying to figure
> out the most efficient way to calculate the mean of one element, across
> all components of the overall list.
>
> I've tried
>
> output <- mean (listobject[[1:5]]$element)

As nobody knows exact structure of your listobject there is probably no
way to give you any reasonable answer.

what does

str(listobject) give you?

What is result of

listobject[[1:5]]

I presume that you get something like

Error in listobject[[1:5]] : recursive indexing failed at level ...

Regards
Petr


>
> to get the mean of "element" in the first five components?
>
> It doesn't like the $.  I'm suspect one of the "apply" functions will
> work, but again I've not had success
>
>
> Thanks a bunch.
>
> J
>
> ----
> Jim Maas
> University of East Anglia
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: calculating mean of list components

Henrique Dallazuanna
In reply to this post by jamaas53
Try this:

mean(unlist(sapply(listobject[1:5], '[', 'element')))

On Wed, Dec 15, 2010 at 11:22 AM, Jim Maas <[hidden email]> wrote:

> I get a list object from an iterative function.  I'm trying to figure out
> the most efficient way to calculate the mean of one element, across all
> components of the overall list.
>
> I've tried
>
> output <- mean (listobject[[1:5]]$element)
>
> to get the mean of "element" in the first five components?
>
> It doesn't like the $.  I'm suspect one of the "apply" functions will work,
> but again I've not had success
>
>
> Thanks a bunch.
>
> J
>
> ----
> Jim Maas
> University of East Anglia
>
> ______________________________________________
> [hidden email] 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.
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: calculating mean of list components

Liviu Andronic
In reply to this post by jamaas53
On Wed, Dec 15, 2010 at 2:22 PM, Jim Maas <[hidden email]> wrote:

> I get a list object from an iterative function.  I'm trying to figure out
> the most efficient way to calculate the mean of one element, across all
> components of the overall list.
>
> I've tried
>
> output <- mean (listobject[[1:5]]$element)
>
> to get the mean of "element" in the first five components?
>
> It doesn't like the $.  I'm suspect one of the "apply" functions will work,
> but again I've not had success
>
Are you looking for this?

> x <- list()
> x[[1]] <- list()
> x[[1]][['element']] <- rnorm(100)
> x[[2]] <- list()
> x[[2]][['element']] <- rnorm(100)
> x[[3]] <- list()
> x[[3]][['element']] <- rnorm(100)
> str(x)
List of 3
 $ :List of 1
  ..$ element: num [1:100] 1.822 1.818 -2.305 -0.307 0.268 ...
 $ :List of 1
  ..$ element: num [1:100] 0.0167 0.3834 1.2947 -0.4583 -0.2165 ...
 $ :List of 1
  ..$ element: num [1:100] -1.244 -0.369 0.772 -2.368 2.136 ...
> mean(x[[1]][['element']])
[1] 0.1446610
> mean(x[[2]][['element']])
[1] -0.1471191
> mapply(function(y) mean(y$element), x)
[1]  0.14466105 -0.14711910  0.05024247

Liviu

______________________________________________
[hidden email] 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.