Warning: argument is not numeric or logical: returning NA

r

#1

I was working on one data set and I want to calculate the mean of one column in which there are NA but I have removed the NA and when I am calculating the mean I am getting warning
z<-read.csv(“hw1_data.csv”)
head(z)
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
bad<-is.na(z$Solar.R)
mean(z[!bad,])
[1] NA
Warning message:
In mean.default(z[!bad, ]) :
argument is not numeric or logical: returning NA


#2

Here’s a hint: think about what kind of object z[!bad,] represents.

And here’s a tip: go through ?mean and see if you can find a parameter which can make this process shorter. : - )


#3

Using the vector bad, you have only removed the na values from the Solar.R column. Thus, na values from any other column may still exist. If you only want to calculate the mean, you can use mean(y, na.rm = TRUE)

Let me know if you have any more doubts.