Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude Blank and NA in R [duplicate]

Tags:

r

na

Possible Duplicate:
R - remove rows with NAs in data.frame

I have a dataframe named sub.new with multiple columns in it. And I'm trying to exclude any cell containing NA or a blank space "".
I tried to use subset(), but it's targeting specific column conditional. Is there anyway to scan through the whole dataframe and create a subset that no cell is either NA or blank space ?

In the example below, only the first line should be kept:

# ID               SNP             ILMN_Strand   Customer_Strand
ID1234              [A/G]          TOP           BOT
Non-Specific        NSB (Bgnd)     Green
Non-Polymorphic     NP (A)         Red
Non-Polymorphic     NP (T)         Purple
Non-Polymorphic     NP (C)         Green
Non-Polymorphic     NP (G)         Blue
Restoration         Restore        Green

Any suggestions? Thanks

like image 265
lusketeer Avatar asked Oct 06 '12 21:10

lusketeer


People also ask

How do I remove blank values from a column in R?

These blanks are actually inserted by using space key on computers. Therefore, if a data frame has any column with blank values then those rows can be removed by using subsetting with single square brackets.


1 Answers

A good idea is to set all of the "" (blank cells) to NA before any further analysis.

If you are reading your input from a file, it is a good choice to cast all "" to NAs:

foo <- read.table(file="Your_file.txt", na.strings=c("", "NA"), sep="\t") # if your file is tab delimited

If you have already your table loaded, you can act as follows:

foo[foo==""] <- NA

Then to keep only rows with no NA you may just use na.omit():

foo <- na.omit(foo)

Or to keep columns with no NA:

foo <- foo[, colSums(is.na(foo)) == 0] 
like image 110
Ali Avatar answered Oct 12 '22 22:10

Ali