gpt4 book ai didi

r - 汇总多个因子变量的水平

转载 作者:行者123 更新时间:2023-12-04 12:34:05 27 4
gpt4 key购买 nike

我搜索了类似的问题,但找不到所需的确切解决方案。 This question有点相似,但只涉及汇总多个连续变量的问题,而不是因素。

我有一个由 4 个因子变量( sexagegrouphivgroup )组成的数据框,例如

set.seed(20150710)
df<-data.frame(sex=as.factor(c(sample(1:2, 10000, replace=T))),
agegroup=as.factor(c(sample(1:5,10000, replace=T))),
hiv=as.factor(c(sample(1:3,10000, replace=T))),
group=as.factor(c(sample(1:2,10000, replace=T)))
)

levels(df$sex)<- c("Male", "Female")
levels(df$agegroup)<- c("16-24", "25-34", "35-44", "45-54", "55+")
levels(df$hiv)<-c("Positive", "Negative", "Not tested")
levels(df$group)<-c("Intervention", "Control")

我想创建一个汇总表,给出每个暴露变量级别的计数和比例 sex , agegrouphiv ,按 group 分层.

编辑:这就是我的目标:
                X N_Control Percent_Control N_Intervention     Percent_Intervention
1 sex_Female 2517 0.5041057 2480 0.4953066
2 sex_Male 2476 0.4958943 2527 0.5046934
3 agegroup_16-24 1005 0.2012818 992 0.1981226
4 agegroup_25-34 1001 0.2004807 996 0.1989215
5 agegroup_35-44 1010 0.2022832 997 0.1991212
6 agegroup_45-54 976 0.1954737 996 0.1989215
7 agegroup_55+ 1001 0.2004807 1026 0.2049131
8 hiv_Negative 1679 0.3362708 1642 0.3279409
9 hiv_Not tested 1633 0.3270579 1660 0.3315359
10 hiv_Positive 1681 0.3366713 1705 0.3405233

但我无法让它与 summarise_each 一起使用在 dplyr 中;仅给出整体变量计数和比例,而不是每个因子水平:
df.out<-df %>%
group_by(group) %>%
summarise_each(funs(N=n(), Percent=n()/sum(n())), sex, agegroup, hiv)
print(df.out)

group sex_N agegroup_N hiv_N sex_Percent agegroup_Percent hiv_Percent
1 1 4973 4973 4973 1 1 1
2 2 5027 5027 5027 1 1 1

最后,是否有某种方法可以 reshape 表格(例如使用 tidyr),以便将暴露变量(性别、年龄组、艾滋病毒)报告为行?

谢谢

最佳答案

分两步做,会给你想要的结果。首先,计算n ,然后通过 group 计算百分比:

library(dplyr)
df.out <- df %>%
group_by(group, sex, agegroup, hiv) %>%
tally() %>%
group_by(group) %>%
mutate(percent=n/sum(n))
data.table的解决方案:
library(data.table)
dt.out <- setDT(df)[, .N, by=.(group, sex, agegroup, hiv)][, percent:=N/sum(N), by=group]
library(microbenchmark)
microbenchmark(df.out = df %>%
group_by(group, sex, agegroup, hiv) %>%
tally() %>%
group_by(group) %>%
mutate(percent=n/sum(n)),
dt.out = df[,.N,by=.(group, sex, agegroup, hiv)][,percent:=N/sum(N),by=group])

# Unit: milliseconds
# expr min lq mean median uq max neval cld
# df.out 8.299870 8.518590 8.894504 8.708315 8.931459 11.964930 100 b
# dt.out 2.346632 2.394788 2.540132 2.441777 2.551235 4.344442 100 a

结论: data.table解决方案要快得多(3.5 倍)。

要在编辑问题后获得您所要求的表格,您可以执行以下操作:
library(data.table)

setDT(df)
dt.sex <- dcast(df[,.N, by=.(sex,group)][,percent:=N/sum(N)], sex ~ group, value.var = c("N", "percent"))
dt.age <- dcast(df[,.N, by=.(agegroup,group)][,percent:=N/sum(N)], agegroup ~ group, value.var = c("N", "percent"))
dt.hiv <- dcast(df[,.N, by=.(hiv,group)][,percent:=N/sum(N)], hiv ~ group, value.var = c("N", "percent"))

dt.out.wide <- rbindlist(list(dt.sex, dt.age, dt.hiv), use.names=FALSE)
names(dt.out.wide) <- c("X","N_Intervention","N_Control","percent_Intervention","percent_Control")

这给出:
> dt.out.wide
X N_Intervention N_Control percent_Intervention percent_Control
1: Male 2454 2488 0.2454 0.2488
2: Female 2561 2497 0.2561 0.2497
3: 16-24 954 991 0.0954 0.0991
4: 25-34 1033 1002 0.1033 0.1002
5: 35-44 1051 1000 0.1051 0.1000
6: 45-54 983 978 0.0983 0.0978
7: 55+ 994 1014 0.0994 0.1014
8: Positive 1717 1664 0.1717 0.1664
9: Negative 1637 1659 0.1637 0.1659
10: Not tested 1661 1662 0.1661 0.1662

关于r - 汇总多个因子变量的水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31426633/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com