【发布时间】:2020-04-01 11:10:38
【问题描述】:
我正在尝试生成一个表,其中包含每个实例的计数,一个变量出现在按一列中的变量分组的数据框中
我的桌子是这样的:
Infected Education age sex race Score
0 missing 35 Female missing 1371.07
1 Higher 39 Female Black 1466.49
0 Higher 27 Female Asian 8020.09
1 A-level 36 Female Black 398.67
1 GCSE 32 Male Other 1312.80
这是用于生成它的代码:
df<- structure(list(Infected = structure(c(1L, 2L, 1L, 2L, 2L), .Label = c("0",
"1"), class = "factor"), Education = structure(c(1L, 4L, 4L,
2L, 3L), .Label = c("missing", "A-level", "GCSE", "Higher"), class = "factor"),
age = c(35L, 39L, 27L, 36L, 32L), sex = structure(c(3L, 3L,
3L, 3L, 2L), .Label = c("Missing_Other", "Male", "Female"
), class = "factor"), race = structure(c(1L, 3L, 2L, 3L,
4L), .Label = c("missing", "Asian", "Black", "Other", "White"
), class = "factor"), Score = c(1371.06994628906, 1466.48999023438,
8020.08984375, 398.670013427734, 1312.80004882812)), class = "data.frame", row.names = c(221L,
261L, 444L, 561L, 702L))
我已经尝试使用 dplyr 包对实例进行计数和分组,但我是 R 新手,所以我担心我的代码没有给出我想要的结果。
这是我已经尝试过的代码,但我不确定如何更改它以产生我想要的结果:
table <-df %>% group_by(Infection) %>% count(sex,Education,age,race,Score)
我想要的输出如下所示:
Infection_1 Infection_0 Infection_All
**ALLSex**
Male 1(0%) 0(0%) 1(20%)
Female 2(40%) 2(40%) 4(80%
**Education**
Missing 0(0%) 1(20%) 1(20%)
Higher 1(20%) 1(20%) 2(40%)
Alevel 1(20%) 0(0%) 2(20%)
GCSE 1(20%) 0(0%) 1(20%)
**Race**
Black 2(40%) 0(0%) 2(40%)
Asian 1(20%) 0(0%) 1(20%)
Other 0(0%) 1(20%) 1(20%)
White 0(0%) 0(0%) 0(0%)
Other 1(20%) 0(0%) 1(20%)
【问题讨论】:
-
感谢您提供可重现的示例