所以我有一个海量的数据集,我需要在其中找到病例和对照的描述性统计数据,然后能够对它们进行比较。例如,我有997名女性和1139名男性,但我需要知道有多少女性是病例,有多少是对照。Controls =0和cases = 1。我想保留所有其他变量,但只将它们分成两组。我试过使用split()函数,我试着创建一个subset(),但我仍然不知道如何让它向我显示不同的组。我对R比较陌生,但需要用它来分析我的硕士论文数据。
发布于 2021-05-11 15:56:47
我不知道我是否理解得很好,但是如果您想根据一个条件拆分数据,那就非常简单了:因为您没有提供任何示例数据,所以我在一个虚拟的data.frame上放了一个示例
df <- data.frame(gender=sample(c("M","F"),1000,replace = T),control=sample(c(0,1),1000,replace = T),other.var=runif(1000))
control <- df[df$control==0,]
cases <- df[df$control==1,]
#if you want female control
f.control <- control[control$gender=="F",]
#idem for male control
m.control <- control[control$gender=="M",]
#idem for famale and male cases
f.cases <- cases[cases$gender=="F",]
m.cases <- cases[cases$gender=="M",]https://stackoverflow.com/questions/67477257
复制相似问题