JMR
JMR

Reputation: 13

Selecting cases that have 2 or more

This should be simple but I'm stumped.

df <- data.frame(date=rep(1:3,1),
             RID=rep(c("Amy", "Bob"), each=3),
             A=c(1, 3, 1, NA, 1, 1))`


   date | RID   |A|
    1   |Amy    |1|
    2   |Amy    |3|
    3   |Amy    |1|
    1   |Bob    |NA|
    2   |Bob    |1|
    3   |Bob    |1|

I just want to select the RID's that have both one (or more) 1 and a 3 (values are only 1 , 3 or NA) in the "A" column.

it should be doable with something like below but I have not been able to make it work.

dplyr::
df%>%group_by %>% filter ( ?????? )

Upvotes: 1

Views: 32

Answers (1)

akrun
akrun

Reputation: 887501

We can group by 'RID' and check if all of 1, 3 are there %in% A column

library(dplyr)
df %>%
   group_by(RID) %>% 
   filter(all(c(1, 3) %in% A))
# A tibble: 3 x 3
# Groups:   RID [1]
#   date RID       A
#  <int> <fct> <dbl>
#1     1 Amy       1
#2     2 Amy       3
#3     3 Amy       1

data

df <- structure(list(date = c(1L, 2L, 3L, 1L, 2L, 3L), RID = structure(c(1L, 
1L, 1L, 2L, 2L, 2L), .Label = c("Amy", "Bob"), class = "factor"), 
    A = c(1, 3, 1, NA, 1, 1)), class = "data.frame", row.names = c(NA, 
-6L))

Upvotes: 1

Related Questions