[R] mean

S Ellison S.Ellison at lgcgroup.com
Wed Sep 4 16:43:33 CEST 2013


 

> -----Original Message-----
> When I try to apply mean to a list, I get the answer :
> 
> argument is not numeric or logical: returning NA
> 
Example: 
l4 <- list(1:4)
class(l4) #not numeric or logical ...
mean(l4) #same error

#a list is not a number, a logical (TRUE/FALSE) or a vector or array of either of those. So mean() can't handle it unaided and tells you what it needs.

#But if your list is a list of numeric objects, unlist will often work.

unlist(l4) #a numeric vector
mean( unlist(l4) ) #no problem

l.some <- list(matrix(1:4, ncol=2), 3:7)
l.some
unlist(l.some) #a numeric vector
mean( unlist(l.some) ) #works

#But a) magic has limits and b) if you want averages, maybe you should not be using a list? A vector would save hassle if it fits ...

S Ellison

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list