如何获取R中两个分类变量的百分比
假设样本总数为8。 数据帧看起来是这样的。所有健康评分低于3分的人都是健康的,所有健康评分高于3分的人都是生病的。状态显示他们的就业状态如何获取R中两个分类变量的百分比,r,R,假设样本总数为8。 数据帧看起来是这样的。所有健康评分低于3分的人都是健康的,所有健康评分高于3分的人都是生病的。状态显示他们的就业状态 Status<-(Employed,Unemployed,Student,Student,Employed,Unemployed,Unemployed,Housewife) Health<-(Healthy,Healthy,Healthy,Sick,Sick,Control,Sick,Sick) df<-(Status,Health) le
Status<-(Employed,Unemployed,Student,Student,Employed,Unemployed,Unemployed,Housewife)
Health<-(Healthy,Healthy,Healthy,Sick,Sick,Control,Sick,Sick)
df<-(Status,Health)
level(Health)<-("Healthy,"Sick",Control)
level(Status)<-("Employed","Unemployed","Student","Housewife")
我正在使用以下代码。但它只是给了我频率,而不是百分比。我需要百分比
tab <- with(df, table(df$Health,df$Status))
tab我们可以统计每个状态
和健康
的个体数量,按状态分组
并计算百分比。为了更好的可见性,我们将数据转换为宽格式
library(dplyr)
df %>%
count(Status, Health) %>%
group_by(Status) %>%
mutate(n = n/sum(n) * 100) %>%
tidyr::pivot_wider(names_from = Health, values_from = n,
values_fill = list(n = 0))
# Status Healthy Sick Control
# <fct> <dbl> <dbl> <dbl>
#1 Employed 50 50 0
#2 Housewife 0 100 0
#3 Student 50 50 0
#4 Unemployed 33.3 33.3 33.3
数据
df <- structure(list(Status = structure(c(1L, 4L, 3L, 3L, 1L, 4L, 4L,
2L), .Label = c("Employed", "Housewife", "Student", "Unemployed"
), class = "factor"), Health = structure(c(2L, 2L, 2L, 3L, 3L,
1L, 3L, 3L), .Label = c("Control", "Healthy", "Sick"),
class = "factor")), class = "data.frame",row.names = c(NA, -8L))
df我想要每个职业中“健康”个体的百分比。@Aryh所以你可以尝试table(df$Status,df$Health=='Health')[,'TRUE']
来获得每个职业中“健康”个体的数量。谢谢你的回复。但我不想要数字……我想要百分比。总体百分比?那就是:table(df$Status,df$Health=='Health')[,'TRUE']/nrow(df)*100
?不全面。:(如在所有受雇人员中。(不全部)有多少人是健康的?
prop.table(table(df), 1) * 100
df <- structure(list(Status = structure(c(1L, 4L, 3L, 3L, 1L, 4L, 4L,
2L), .Label = c("Employed", "Housewife", "Student", "Unemployed"
), class = "factor"), Health = structure(c(2L, 2L, 2L, 3L, 3L,
1L, 3L, 3L), .Label = c("Control", "Healthy", "Sick"),
class = "factor")), class = "data.frame",row.names = c(NA, -8L))