在重复测量数据中,如何进行子集选择匹配的病例和对照? [英] In repeated measures data, how to subset to select matched cases and controls?

查看:24
本文介绍了在重复测量数据中,如何进行子集选择匹配的病例和对照?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组按家庭聚类的数据,研究问题是同一家庭中具有不同特征 x 的两个人是否具有相同的二元(是/否)结果 y.在某些家庭中,所有成员都对 y 表示是".在其他家庭中,对于 y,有些是是",有些是否".我只想得到结果状态不一致的家庭.我猜代码会是某种条件逻辑语句,但还不能完全弄清楚……在下面的示例数据中,例如,我只想得到家庭 2 和 3.谢谢您的帮助!

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 families, all members are "yes" for y. In other families, some are "yes" and some are "no" for y. I want to get only the families with discordant outcome statuses. I am guessing the code will be some sort of conditional logic statements but can't quite figure it out yet... In the sample data below, for example, I only want to get families 2 and 3. Thank you for your help!

#sample data
df <- as.data.frame(cbind(
famid <- c(1,1,2,2,3,3,3),
individ <- c(1,2,3,4,5,6,7),
y <- c(0,0,0,1,0,0,1)))
colnames(df) <- c("famid", "individ", "y")

推荐答案

以 R 为基础:

df[ave(df$y, df$famid, FUN = function(x) length(unique(x)) > 1)==1,]

使用 :

library(data.table)
setDT(df)[, .SD[uniqueN(y)>1], by = famid]
# or:
setDT(df)[, if (uniqueN(y)>1) .SD, by = famid]

使用 :

library(dplyr)
df %>% group_by(famid) %>% filter(n_distinct(y) > 1)

这篇关于在重复测量数据中,如何进行子集选择匹配的病例和对照?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆