Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 - Fatal编程技术网

如何获取R中两个分类变量的百分比

如何获取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

假设样本总数为8。 数据帧看起来是这样的。所有健康评分低于3分的人都是健康的,所有健康评分高于3分的人都是生病的。状态显示他们的就业状态

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))