我正在努力使用
prop.table
函数,我试图让它显示变量的每个计数的百分比。目前它只显示每个意见中 owner 变量 N 和 Y 的百分比。我希望它显示所有意见中每个 Y /N 变量的百分比,因此整个百分比输出列的总和为 100.
这是我使用的代码和输出。
hey <- data %>% group_by(opinion, owner) %>%
summarize(count = n()) %>% mutate(percentage=prop.table(count)*100)
hey
opinion owner count percentage
<chr> <chr> <int> <dbl>
1 Agree N 22 56.4
2 Agree Y 17 43.6
3 Disagree N 2 16.7
4 Disagree Y 10 83.3
5 Not Sure N 1 16.7
6 Not Sure Y 5 83.3
7 Strongly Agree N 57 66.3
8 Strongly Agree Y 29 33.7
9 Strongly Disagree N 1 20
10 Strongly Disagree Y 4 80
您可以使用
xtabs
和 cbind
proportions
。 (请注意,“prop.table
是一个较早的名称,保留用于向后兼容。”)
as.data.frame(xtabs(~ owner + opinion, dat)) |> {\(.) cbind(., perc=proportions(.$Freq)*100)}()
# owner opinion Freq perc
# 1 N Agree 22 14.8648649
# 2 Y Agree 17 11.4864865
# 3 N Disagree 2 1.3513514
# 4 Y Disagree 10 6.7567568
# 5 N Not Sure 1 0.6756757
# 6 Y Not Sure 5 3.3783784
# 7 N Strongly Agree 57 38.5135135
# 8 Y Strongly Agree 29 19.5945946
# 9 N Strongly Disagree 1 0.6756757
# 10 Y Strongly Disagree 4 2.7027027
资料:
dat <- structure(list(opinion = c("Agree", "Agree", "Agree", "Agree",
"Agree", "Agree", "Agree", "Agree", "Agree", "Agree", "Agree",
"Agree", "Agree", "Agree", "Agree", "Agree", "Agree", "Agree",
"Agree", "Agree", "Agree", "Agree", "Agree", "Agree", "Agree",
"Agree", "Agree", "Agree", "Agree", "Agree", "Agree", "Agree",
"Agree", "Agree", "Agree", "Agree", "Agree", "Agree", "Agree",
"Disagree", "Disagree", "Disagree", "Disagree", "Disagree", "Disagree",
"Disagree", "Disagree", "Disagree", "Disagree", "Disagree", "Disagree",
"Not Sure", "Not Sure", "Not Sure", "Not Sure", "Not Sure", "Not Sure",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Agree", "Strongly Agree",
"Strongly Agree", "Strongly Agree", "Strongly Disagree", "Strongly Disagree",
"Strongly Disagree", "Strongly Disagree", "Strongly Disagree"
), owner = c("N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "Y",
"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "N", "N", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "N", "Y", "Y", "Y", "Y", "Y", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N",
"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y",
"Y", "Y", "Y", "N", "Y", "Y", "Y", "Y"), n = c(1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA,
-148L), class = "data.frame")