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?
>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)