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

Asked

Viewed 144 times

3

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 eliminate the whole line when it exists NA 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

1 answer

3


The function filter package dplyr meets 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 studied the elements that are not NA (!is.na) of the variable a

Browser other questions tagged

You are not signed in. Login or sign up in order to post.