[R] mean

arun smartpink111 at yahoo.com
Fri Aug 30 14:53:51 CEST 2013


Hi,
Better would be to show a reproducible example using ?dput() and the codes you used.  Assuming that you tried something like this:
lst1<- list(1:10,c(5,4,3),4:15)
 mean(lst1)
#[1] NA
#Warning message:
#In mean.default(lst1) : argument is not numeric or logical: returning NA
 sapply(lst1,mean)  #also depends upon the list elements.  In the example I used, it is a vector.
#[1] 5.5 4.0 9.5

#or

lapply(lst1,mean)
#[[1]]
#[1] 5.5
#
#[[2]]
#[1] 4
#
#[[3]]
#[1] 9.5

#Suppose, your list elements are:
set.seed(24)
 lst2<- list(as.data.frame(matrix(sample(1:10,5*4,replace=TRUE),ncol=5)),as.data.frame(matrix(sample(1:40,8*5,replace=TRUE),ncol=8)))

#and you wanted to find the column means

 lapply(lst2,colMeans)
#[[1]]
 # V1   V2   V3   V4   V5 
#5.00 7.00 5.75 7.00 2.75 
#
#[[2]]
 # V1   V2   V3   V4   V5   V6   V7   V8 
#14.4 16.8 16.4 26.4 11.4 12.0 24.8 16.8 

Hope it helps.

A.K.



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)



More information about the R-help mailing list