-3

I have a data frame. Some of the rows in various columns contain #N/A (imported from Excel). I want to delete these rows that have #N/A. How do I do this.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
user2662565
  • 509
  • 2
  • 9
  • 22
  • Start by providing a reproducible example. – David Arenburg Nov 19 '14 at 20:25
  • possible duplicate of [remove rows with NAs in data.frame](http://stackoverflow.com/questions/4862178/remove-rows-with-nas-in-data-frame) – user1436187 Nov 19 '14 at 20:27
  • @user1436187, quite likely that it's a duplicate, but probably not of the question you linked to, since those values here (#NA) are not real NAs in R. – talat Nov 19 '14 at 20:28
  • This is actually a good question. na.omit() does not remove entries with values 'n/a'. any(is.na(data)) returns FALSE despite several columns containing 'n/a'. In python dropna() removes everything though – siegfried Dec 31 '20 at 09:56

1 Answers1

4

If you have a file that looks like this

cat(x <- "a b f a\n#N/A a n b\nB #N/A #N/A c")
# a b f a
# #N/A a n b
# B #N/A #N/A c

You can read the data into R with read.table, making use of the na.strings argument while adjusting the comment.char argument accordingly.

(df <- read.table(text = x, na.strings = "#N/A", comment.char = ""))
#     V1   V2   V3 V4
# 1    a    b    f  a
# 2 <NA>    a    n  b
# 3    B <NA> <NA>  c

Then call na.omit to remove all rows that contain NA

na.omit(df)
#   V1 V2 V3 V4
# 1  a  b  f  a

To read from file, replace text = x with the file name

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245