how to remove a row in the data.frame which contain a empty element

#1
I have a data.frame with many empty element, and I want to remove the row with a empty element. Is there a convient way to do this like the command na.omit.
I create a data.frame like this
dat <- data.frame(name = c("a","b","c", "", "h","k"), x2 = c(100,200, 300, 400, 500,600))
I can use the command na.omit to remove the 4th row. but how can I remove the second and the 6th row? Thank you very much.
 
Last edited:

staassis

Active Member
#2
What you are doing is wrong on so many levels.

First, you should not mix empty strings ("") and numbers in one variable, or R will view the whole variable as string type and will not process the numbers correctly at all times. If you want to say that the number is missing, use NA.

Second, if even one NA value in a row means you want to remove the row, well, this is quite a strong move. Most statistical functions in R are well-equipped to handle missing values (use "na.rm=T" for some of them) but would make good use of non-missing values in the row. Information is money. Ignoring information is throwing the money out the window.

Now, sometimes one does need to extract a sub-matrix of fully marked values. In that case use

complete.cases()
 
#3
What you are doing is wrong on so many levels.

First, you should not mix empty strings ("") and numbers in one variable, or R will view the whole variable as string type and will not process the numbers correctly at all times. If you want to say that the number is missing, use NA.

Second, if even one NA value in a row means you want to remove the row, well, this is quite a strong move. Most statistical functions in R are well-equipped to handle missing values (use "na.rm=T" for some of them) but would make good use of non-missing values in the row. Information is money. Ignoring information is throwing the money out the window.

Now, sometimes one does need to extract a sub-matrix of fully marked values. In that case use

complete.cases()
Thank you very much for your valuable response. But there my be some confusion in my description. I actually import my data from a csv file, the first column is a character vector. but there a some missing data, after I Import it into R, the element remains "" rather than NA, so I want remove the row. The data.frame was shown below
> dat
name x2
1 a 100
2 b 200
3 c 300
4 400
5 h 500
6 k 600

and I just want to remove the 4th row conveniently.
 

trinker

ggplot2orBust
#4
Note that read.csv/read.table has an argument: na.strings which you can supply a vector to as follows:

Code:
na.strings = c("NA", "999", "")