1

From searching online and in this group, it seems like this should work:

> mean(r_lab$ozone, na.rm=TRUE)

However, what I get is:

[1] NA
Warning message:
In mean.default(r_lab$ozone, na.rm = TRUE) :
  argument is not numeric or logical: returning NA

This is the contents of that column in the dataset:

> r_lab$Ozone
 [1]  41  36  12  18  NA  28  23  19   8  NA   7  16  11  14
[15]  18  14  34   6  30  11   1  11   4  32  NA  NA  NA  23

I'm sort of flustered.

4

2 回答 2

5

Your data is most likely of class character, instead of numeric.

Take a look at these examples:

# Set up some numeric data
x <- c(41, 36, 12, 18, NA, 28, 23, 19,  8, NA,  7, 16, 11, 14, 18, 14, 34,  6, 30, 11,  1, 11,  4, 32, NA, NA, NA, 23)

# Clearly taking the mean on this will work
 mean(x, na.rm = TRUE)

[1] 18.13043

However, if your data is of class character, then you get the error message you report:

y <- as.character(x)
mean(y, na.rm = TRUE)

[1] NA
Warning message:
In mean.default(y, na.rm = TRUE) :
  argument is not numeric or logical: returning NA

So you should convert your data to numeric first, then take the mean:

mean(as.numeric(x), na.rm = TRUE)

[1] 18.13043
于 2016-05-29T18:11:01.237 回答
0

I was not aware that R was case sensitive.

Richard was right, I should have been using Ozone, not ozone. Thanks to everyone for their help.

Sorry, I did not know how to provide reproducible data. What would have been sufficient in this case?

于 2016-05-29T18:30:30.410 回答