user2938738
user2938738

Reputation: 623

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

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?

Upvotes: 62

Views: 235282

Answers (3)

Ursulka Siete
Ursulka Siete

Reputation: 153

The same error appears if you do not use the correct (numeric) format of your data in your data.frame column using mean() function. Therefore, check your data using str(data.frame&column) function to see what data type you have, and convert it to numeric format if necessary. For example, if your data is Character convert it with as.numeric(data.frame$column), or as a factor with as.numeric(as.character(data.frame$column)). The mean function does not work with types other than numeric.

Upvotes: 1

mnel
mnel

Reputation: 115505

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)

Upvotes: 86

Keith Park
Keith Park

Reputation: 617

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

Upvotes: 21

Related Questions