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.
Asked
Active
Viewed 1,870 times
-3
-
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 Answers
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
-
-
1Not sure what you mean. `na.omit` works as well to remove rows with `NA` values – Rich Scriven Nov 19 '14 at 22:40