1

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

Alison
  • 11
  • 1
  • 5

3 Answers3

4

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), ]
Hugh
  • 13,468
  • 6
  • 48
  • 90
0

You can do this with the complete.cases function

mydata = mydata[complete.cases(mydata), ]
G5W
  • 32,266
  • 10
  • 31
  • 60
0

There are multiple issues with your code:

  1. It's usually best to specify stringsAsFactors = FALSE using read.csv (unless you really want factors)

  2. There's no need to use as.data.frame after read.csv, you already have a data frame

  3. In the third line you need a comma before the closing ]

  4. You're replacing with the string "NA", just use NA (no quotes)

  5. To remove rows from data frame mydata, just use na.rm(mydata), not mydata$col2

    mydata = read.csv('file:///C:/Users/file.csv', stringsAsFactors = FALSE)
    mydata[mydata$col2 == "#N/A", ] <- NA
    na.omit(mydata)
    
neilfws
  • 26,280
  • 5
  • 44
  • 53