Extend contigency table with proportions (percentages)

后端 未结 6 1218
悲哀的现实
悲哀的现实 2020-11-29 02:39

I have a contingency table of counts, and I want to extend it with corresponding proportions of each group.

Some sample data (tips data set from gg

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 03:06

    Your code doesn't seem so ugly to me...
    however, an alternative (not much better) could be e.g. :

    df <- data.frame(table(yn))
    colnames(df) <- c('Smoker','Freq')
    df$Perc <- df$Freq / sum(df$Freq) * 100
    
    ------------------
      Smoker Freq Perc
    1     No   19 47.5
    2    Yes   21 52.5
    

提交回复
热议问题