[R] mean

arun smartpink111 at yahoo.com
Fri Aug 30 18:40:08 CEST 2013


Hi,
In case, some of the list elements are vectors, this procedure would not work.
 a1<- list(c(3,5),4,5:6)
as.numeric(a1)
#Error: (list) object cannot be coerced to type 'double'

The OP didn't provide any info as to how the data looks like.  So, these are just assumptions.
 mean(unlist(a1)) #if the overall mean is needed. 
#[1] 4.6
#or mean of each list element

sapply(a1,mean)
#[1] 4.0 4.0 5.5

#or if it includes data.frame or matrix.
a2<- list(4, as.data.frame(matrix(1:10,5,2)),4:6)


a3<- list(4, matrix(1:10,5,2),4:6)
mean(unlist(a2))
#[1] 5.285714

mean(unlist(a3))
#[1] 5.285714


A.K.

----- Original Message -----
From: Albyn Jones <jones at reed.edu>
To: agnes69 <festre at gredeg.cnrs.fr>
Cc: r-help at r-project.org
Sent: Friday, August 30, 2013 11:59 AM
Subject: Re: [R] mean

It would be easier to diagnose the problem if you included an example
illustrating exactly what you did.  I'll guess:

> a <- list(3,4,5)
> mean(a)
[1] NA
Warning message:
In mean.default(a) : argument is not numeric or logical: returning NA

> mean(as.numeric(a))
[1] 4

But that's just a guess, as I don't know the actual contents of your list!

albyn


On Fri, Aug 30, 2013 at 5:18 AM, agnes69 <festre at gredeg.cnrs.fr> wrote:

> When I try to apply mean to a list, I get the answer :
>
> argument is not numeric or logical: returning NA
>
> Could you help me?
>
> (I am a very beginner)
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/mean-tp4674999.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

    [[alternative HTML version deleted]]

______________________________________________
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.




More information about the R-help mailing list