Home > Mobile >  remove zero value depend the zero value in another column R
remove zero value depend the zero value in another column R

Time:12-19

I have df with some in ID zero and some in an age in zero too. I want to remove any rows with Id zero if age < zero what is the best function in R

Id   | age
12   |  10
0    |  0 
0    |  5

need to be

Id   | age
12   |  10
0    |  0 

remove rows with zero or N/A if the age bigger than zero

CodePudding user response:

library(dplyr)

data <-
  data.frame(
    id = c(12,0,0),
    age = c(10,0,5)
  )

data %>% 
  filter(!(age > 0 & (is.na(id) | id == 0)))

CodePudding user response:

With base R

 subset(df1, !(id == 0 & age != 0))

-output

  id age
1 12  10
2  0   0
  • Related