Reputation: 312
In this spell dataset let's say I have user ID, migration number (0,1,2..) and a column for start that represent months from a particular starting date.
sequence_test <- tibble(id = c(1,1,2,2,3,4,4,5),
spell_number = c(0,1,0,1,0,0,1,0),
start = c(100, 120, 100, 108, 90, 120, 140, 130))
I would like to exclude certain individuals given their start date on their first spell is less than 115, but the indviduals I keep I would like to keep all of their spells even if the start date is before 115.
sequence_test %>%
group_by(id) %>%
filter(start > 115 (given) spell_number == 1)
I had thought that grouping by IDs since I would like to apply the filtering to all that are the same and then a conditional filter for if start > 115, only for spell_number == 1, but can't find a conditional filtering syntax with dplyr that's not & or |.
Upvotes: 0
Views: 32
Reputation: 51994
Use any
+ &
:
sequence_test %>%
group_by(id) %>%
filter(any(start > 115 & spell_number == 1))
id spell_number start
<dbl> <dbl> <dbl>
1 1 0 100
2 1 1 120
3 4 0 120
4 4 1 140
If, for some reason, you don't wanna use &
, there is still [
:
sequence_test %>%
group_by(id) %>%
filter(any(start[spell_number == 1] > 115))
Upvotes: 2