Remove rows with missing values in R [duplicate]


I want to remove all rows with NAs, I tried na.omit, but I still have all the "#N/A" values in my data...

This is my code:

mydata = read.csv('file:///C:/Users/file.csv')
mydata = as.data.frame(mydata)
mydata[mydata$col2== "#N/A"] <- "NA"
na.omit(mydata$col2)

What can I do?

I also tried this:

mydata = mydata[!is.na(mydata)]

But it doesn't work either


Answers:


You should instruct R to treat the string #N/A as NA immediately. The argument na.strings to read.csv tells R what strings to treat as NA.

mydata <- read.csv('file:///C:/Users/file.csv', na.strings = c("", "NA", "#N/A"))
mydata[complete.cases(mydata), ]