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.
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)
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@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@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]]
Use is.numeric(list) on the list to see if it is a list of numbers or if it is list of characters. If it is a list of characters (which could be the case if you read it in using read.csv or the like) and it makes sense to convert to numeric, then do something like: list<-as.numeric(list) -Roy On 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.********************** "The contents of this message do not reflect any position of the U.S. Government or NOAA." ********************** Roy Mendelssohn Supervisory Operations Research Analyst NOAA/NMFS Environmental Research Division Southwest Fisheries Science Center 1352 Lighthouse Avenue Pacific Grove, CA 93950-2097 e-mail: Roy.Mendelssohn at noaa.gov (Note new e-mail address) voice: (831)-648-9029 fax: (831)-648-8440 www: http://www.pfeg.noaa.gov/ "Old age and treachery will overcome youth and skill." "From those who have been given much, much will be expected" "the arc of the moral universe is long, but it bends toward justice" -MLK Jr.
> -----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}}