我如何按比例汇总所有因素?

时间:2021-02-11 01:44:40

标签: r

当我运行数据集摘要时,会显示数据集中每个变量的计数。我所有的变量都是因素。如何显示百分比而不是计数?


> summary(AR)
 Phone_._on_file Credit.Card.Flag Web.Buyer.Flag  flag.spending.last.one.month flag.markdown  
 N: 2054         Mode :logical    Mode :logical   Mode :logical                Mode :logical  
 Y:12436         FALSE:8927       FALSE:13865     FALSE:11433                  FALSE:1847     
                 TRUE :5563       TRUE :625       TRUE :3057                   TRUE :12643    
  Response      
 Mode :logical  
 FALSE:12117    
 TRUE :2373

1 个答案:

答案 0 :(得分:0)

编写您自己的 summary 函数。

summary_perc <- function(data) {
  lapply(data, function(x) prop.table(table(x)) * 100)  
  #If all columns are logical maybe `sapply` would be helpful.
  #sapply(data, function(x) prop.table(table(x)) * 100)  
}

df <- dplyr::select(mtcars, cyl, am, gear, carb)
summary_perc(df)

#$cyl
#x
#   4    6    8 
#34.4 21.9 43.8 

#$am
#x
#   0    1 
#59.4 40.6 

#$gear
#x
#   3    4    5 
#46.9 37.5 15.6 

#$carb
#x
#    1     2     3     4     6     8 
#21.88 31.25  9.38 31.25  3.12  3.12 
相关问题