Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 总结多因素变量的水平_R_Dataframe_Dplyr - Fatal编程技术网

R 总结多因素变量的水平

R 总结多因素变量的水平,r,dataframe,dplyr,R,Dataframe,Dplyr,我搜索过类似的问题,但找不到所需的确切答案。这个问题有点类似,但只涉及多个连续变量的总结问题,而不是因素 我有一个由4个因素变量组成的数据框架(sex,agegroup,hiv,group),例如 但我无法让它在dplyr中与每个摘要一起工作;仅给出总体变量计数和比例,而不是每个因子水平: df.out<-df %>% group_by(group) %>% summarise_each(funs(N=n(), Percent=n()/sum(n())), sex, a

我搜索过类似的问题,但找不到所需的确切答案。这个问题有点类似,但只涉及多个连续变量的总结问题,而不是因素

我有一个由4个因素变量组成的数据框架(
sex
agegroup
hiv
group
),例如

但我无法让它在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
df.out%
分组依据(分组)%>%
总结每个(funs(N=N(),百分比=N()/总和(N())、性别、年龄组、hiv)
打印(测向输出)
团体性别年龄组hiv性别百分比年龄组hiv百分比
1     1  4973       4973  4973           1                1           1
2     2  5027       5027  5027           1                1           1
最后,是否有一些方法可以改变表格的形状(例如使用tidyr),以便将暴露变量(性别、年龄组、hiv)报告为行


谢谢

分两步进行会得到您想要的结果。首先,计算
n
,然后按
组计算百分比:

library(dplyr)
df.out <- df %>%
  group_by(group, sex, agegroup, hiv) %>%
  tally() %>%
  group_by(group) %>%
  mutate(percent=n/sum(n))

结论:
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")

ftable(groups~sex+hiv+agegroup,data=df)
这样的东西开始吧?非常感谢-太棒了!我可能还不完全清楚我所说的“重塑”是什么意思。编辑问题以显示我的目标-不确定使用dplyr/tidyr是否可行?@PeterMacPherson查看更新问题中的所需输出,我觉得这很奇怪。你把几个变量混合在一起,这不是一件明智的事情。你能解释一下为什么要用这种格式吗?在这张表上不会做进一步的分析。纯粹是为了在我的工作流程中高效导出,以标准生物医学基线特征表格式报告研究结果。例如,参见本手稿中的示例表1,这太棒了!非常感谢。
library(dplyr)
df.out <- df %>%
  group_by(group, sex, agegroup, hiv) %>%
  tally() %>%
  group_by(group) %>%
  mutate(percent=n/sum(n))
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 
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