Home > Back-end >  Remove rows whose number is "NA" in R
Remove rows whose number is "NA" in R

Time:06-15

I am trying to delete some rows according to a filter.

clean <-function(z){
f <- z[!(z$"Current_status" == "T" & z$"Start_date" == 2012),]
return(f)
}

It worked on all dataframes but one. On this one, the lines I want to delete were fully emptied ("NA" value for each column) but the column remains. This is what I get:

     Current_status Start_date
1                 O       2005
2                 O       2004
3                 O       2004
4                 O       2002
5                 O       2002
NA             <NA>         NA
NA.1           <NA>         NA
8                 O          0
9                 O          0
10                O          0
11                O          0
NA.2           <NA>         NA

I tried several methods but none worked.

My hypothesis is that the problem is due to the fact that the number of the row changed and also became "NA".

How could I get rid of these rows?

Many thanks!

CodePudding user response:

You could subset with the help of is.na():

f <- f[!is.na(f$Current_status) & !is.na(f$Start_date), ]
  •  Tags:  
  • r na
  • Related