How to draw relative frequency table

▼魔方 西西 提交于 2021-02-10 05:49:10

问题


Src=as.factor(c("nc","us","us","nc","nc","ci","nn","pr","nc","nc","ak","ak","ak","ak","ci","hv","ak","ci","nc","nc"))
Version = as.factor(c(0,4,7,0,0,0,9,0,0,0,1,1,1,1,0,2,1,0,0,0))
table(Src,Version)

Output:

    Version
Src  0 1 2 4 7 9
  ak 0 5 0 0 0 0
  ci 3 0 0 0 0 0
  hv 0 0 1 0 0 0
  nc 7 0 0 0 0 0
  nn 0 0 0 0 0 1
  pr 1 0 0 0 0 0
  us 0 0 0 1 1 0

Instead of showing the counted numbers, can I instead show relative frequencies?


回答1:


Sure. You can use prop.table() to that effect:

prop.table(table(Src, Version))

This will produce a table like:

    Version
Src     0    1    2    4    7    9
  ak 0.00 0.25 0.00 0.00 0.00 0.00
  ci 0.15 0.00 0.00 0.00 0.00 0.00
  hv 0.00 0.00 0.05 0.00 0.00 0.00
  nc 0.35 0.00 0.00 0.00 0.00 0.00
  nn 0.00 0.00 0.00 0.00 0.00 0.05
  pr 0.05 0.00 0.00 0.00 0.00 0.00
  us 0.00 0.00 0.00 0.05 0.05 0.00

You can use the optional argument marginto request row- or column relative frequencies.




回答2:


That really is a FAQ.

Just divide your table by its sum :

tab <- table(Src,Version)
tab/sum(tab)

See also ?prop.table.




回答3:


One option is to use barplot with beside=TRUE to show relative( the plot will not change with proportion options, just the scales) difference between groups.

 barplot(table(Src,Version),beside=TRUE)

enter image description here



来源:https://stackoverflow.com/questions/14581204/how-to-draw-relative-frequency-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!