Abhishek
Abhishek

Reputation: 1625

r.table not removing NA values from CSV file

I know this question is answered before but still i am not able to handle my problem.

I am using this code to read a CSV file and remove "NA" values from it.

read.table("001.csv", header = T, na.strings = "NA")

and output still contain "NA" values. Below is one of the output containing four different columns and 1454 is row name.

*1454 2006-12-24 ,NA,NA,1

Upvotes: 1

Views: 3836

Answers (1)

Chuck
Chuck

Reputation: 998

Since you've already read in your file, you can keep all the rows with no NA values by using na.omit() or complete.cases().

Use na.omit() with the following:

foo <- na.omit(foo)

For example, suppose you have the data.frame foo:

> foo
   a b  c
1  1 1 NA
2  2 2 NA
3  3 3  3
4  4 4  3
5 NA 5  3
6  6 6  3

The above code will give you the following:

> foo <- na.omit(foo)
> foo
  a b c
3 3 3 3
4 4 4 3
6 6 6 3

Alternatively, you could use complete.cases():

foo <- foo[complete.cases(foo),]

Again, suppose you have the data.frame foo:

> foo
   a b  c
1  1 1 NA
2  2 2 NA
3  3 3  3
4  4 4  3
5 NA 5  3
6  6 6  3

The above code using complete.cases() will give you the following:

> foo <- foo[complete.cases(foo),]
> foo
  a b c
3 3 3 3
4 4 4 3
6 6 6 3

Upvotes: 3

Related Questions