In repeated measures data, how to subset to select matched cases and controls?

后端 未结 1 359
[愿得一人]
[愿得一人] 2021-01-29 10:38

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

相关标签:
1条回答
  • 2021-01-29 10:59

    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)
    
    0 讨论(0)
提交回复
热议问题