I have a set of data clustered by family, research question is do 2 people in the same family with different characteristic x have the same binary (yes/no) outcome y. In some fa
With base R:
df[ave(df$y, df$famid, FUN = function(x) length(unique(x)) > 1)==1,]
With data.table:
library(data.table)
setDT(df)[, .SD[uniqueN(y)>1], by = famid]
# or:
setDT(df)[, if (uniqueN(y)>1) .SD, by = famid]
With dplyr:
library(dplyr)
df %>% group_by(famid) %>% filter(n_distinct(y) > 1)