Reputation: 587
I am trying to drop observations in R from my dataset. I need each Person_ID to have wave 0 AND (wave 1 OR wave 3 OR wave 6 OR wave 12 OR wave 18). Can someone help me?
Initial dataset
Person_ID wave
1 0
1 1
1 3
1 6
1 12
1 18
2 0
3 0
3 1
4 6
4 12
Wanted result
Person_ID wave
1 0
1 1
1 3
1 6
1 12
1 18
3 0
3 1
Thanks!
Upvotes: 1
Views: 280
Reputation: 887048
We can also do this in base R
df1[with(df1, Person_ID %in% intersect(Person_ID[wave %in% c(1, 3, 6, 12, 18)],
Person_ID[!wave])),]
# Person_ID wave
#1 1 0
#2 1 1
#3 1 3
#4 1 6
#5 1 12
#6 1 18
#8 3 0
#9 3 1
df1 <- structure(list(Person_ID = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L,
3L, 4L, 4L), wave = c(0L, 1L, 3L, 6L, 12L, 18L, 0L, 0L, 1L, 6L,
12L)), class = "data.frame", row.names = c(NA, -11L))
Upvotes: 0
Reputation: 15072
You can do a grouped filter. We keep a person if both 0 and any of 1, 3, 6, 12, 18 are in their corresponding wave
values.
library(tidyverse)
tbl <- read_table2(
"Person_ID wave
1 0
1 1
1 3
1 6
1 12
1 18
2 0
3 0
3 1
4 6
4 12"
)
tbl %>%
group_by(Person_ID) %>%
filter(0 %in% wave, any(c(1, 3, 6, 12, 18) %in% wave))
#> # A tibble: 8 x 2
#> # Groups: Person_ID [2]
#> Person_ID wave
#> <dbl> <dbl>
#> 1 1 0
#> 2 1 1
#> 3 1 3
#> 4 1 6
#> 5 1 12
#> 6 1 18
#> 7 3 0
#> 8 3 1
Created on 2019-03-25 by the reprex package (v0.2.1)
Upvotes: 3