Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mean() warning: argument is not numeric or logical: returning NA

Tags:

r

I have a data frame with two columns. When I try to calculate mean, I get this message:

[1] NA Warning message: In mean.default(results) : argument is not numeric or logical: returning NA` 

where 'results' is my data set. Any advice on getting around this problem?

like image 379
user2938738 Avatar asked Oct 31 '13 02:10

user2938738


People also ask

What does argument is not numeric or logical returning na mean?

The “Argument is not numeric or logical: returning na” appears when you use non-numerical or illogical objects rather than numerical or logical objects to calculate the mean in R.

How do you find the mean of a column in R?

In this method of computing, the mean of the given dataframe column user just need to call the colMeans function which is an in-build function in R language and pass the dataframe as its parameter, then this will be returning the mean of all the column in the provided dataframe to the user.

Is Numeric in R?

In R, the is. numeric() function returns a logical value, TRUE or FALSE , indicating if the argument passed to it has a base type of the class double or integer and the values are regarded as numeric.

How do you make a number in R?

To convert factors to the numeric value in R, use the as. numeric() function. If the input is a vector, then use the factor() method to convert it into the factor and then use the as. numeric() method to convert the factor into numeric values.


2 Answers

From R 3.0.0 onwards mean(<data.frame>) is defunct (and passing a data.frame to mean will give the error you state)

A data frame is a list of variables of the same number of rows with unique row names, given class "data.frame".

In your case, result has two variables (if your description is correct) . You could obtain the column means by using any of the following

lapply(results, mean, na.rm = TRUE) sapply(results, mean, na.rm = TRUE) colMeans(results, na.rm = TRUE) 
like image 91
mnel Avatar answered Oct 16 '22 09:10

mnel


If you just want to know the mean, you can use

summary(results) 

It will give you more information than expected.

ex) Mininum value, 1st Qu., Median, Mean, 3rd Qu. Maxinum value, number of NAs.

Furthermore, If you want to get mean values of each column, you can simply use the method below.

mean(results$columnName, na.rm = TRUE) 

That will return mean value. (you have to change 'columnName' to your variable name

like image 33
Keith Park Avatar answered Oct 16 '22 09:10

Keith Park