-3

I have checked other posts to find the method of how to drop the rows with NAs in R, but I cannot operate anything after that(for example finding the mean)

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • In the future, it is better to show the example data using `dput` as we cannot copy data from image for testing. – akrun Mar 20 '16 at 08:29
  • 2
    Please consider reading up on [ask] and how to produce a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Heroka Mar 20 '16 at 16:12

1 Answers1

2

The reason is because the 'x' and 'y' column could be factor class. We can change it to numeric and it should work

mean(as.numeric(as.character(test2$x)), na.rm=TRUE)
#[1] 300

It happens, when there are non-numeric elements already present in the dataset and we use read.csv/read.table to read the dataset with the default option stringsAsFactors=TRUE. So, any column that have non-numeric would be a factor. Even if, we use stringsAsFactors=FALSE, the column would be character and using mean directly on character class will give the same NA as result

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

We can check the str(test2) and find the class or use class(test2$x)

data

test <- data.frame(x= c(124, "*", 546, 54), y = c("*", 
    2, 34, 4), z =c(324, 354, 12, 54), b = c(133, 54, 11, 545))
test[test=="*"] <- NA
test2 <- na.omit(test)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662