Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete all rows based on corresponding values in multiple columns

I would like to subset/filer a new dataframe based on a condition I could not see/find here yet. (Subset data frame based on multiple conditions)

id <- c('10','10','10','11', '11', '12', '13', '13', '14', '15', '15')
number <- c('1', '2','3', '1', '2', '2', '2', '3','1' ,'1','2')
df <- data.frame(id, number)

I want to create a new dataframe based on the following condition. If the number is 1, then delete all the rows with the corresponding id value.
Desired output:

id        number
12        2
13        2
13        3

Looks/sounds not that difficult, but unfortunately i cannot figure it out yet. Any tips?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

First of all, make sure your number columns are numeric. After that you can group_by per id and filter if all numbers are true based on your condition like this:

library(dplyr)

df %>%
  group_by(id) %>%
  filter(all(number > 1))
#> # A tibble: 3 × 2
#> # Groups:   id [2]
#>   id    number
#>   <chr>  <dbl>
#> 1 12         2
#> 2 13         2
#> 3 13         3

Created on 2023-08-16 with reprex v2.0.2


Data used:

id <- c('10','10','10','11', '11', '12', '13', '13', '14', '15', '15')
number <- c(1, 2,3, 1, 2, 2, 2, 3,1 ,1,2)
df <- data.frame(id, number)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading