How to remove the rows based on the values of another variable?

4

Consider the dataframe:

data<-data.frame(a=c(1,3,4,5,6,NA,6,NA),b=c(1,NA,NA,4,6,7,NA,1))

I want to delete the entire line when NA exists in variable 'a' . So, what I hope is:

data
  a  b
1 1  1
2 3 NA
3 4 NA
4 5  4
5 6  6
6 6 NA
    
asked by anonymous 05.10.2018 / 18:16

1 answer

4

The filter function of the dplyr package fulfills what you want

library(dplyr)
data %>% 
  filter(!is.na(a))

  a  b
1 1  1
2 3 NA
3 4 NA
4 5  4
5 6  6
6 6 NA

In this case I have filtered the elements that are not NA ( !is.na ) of the variable a

    
05.10.2018 / 18:19