Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw relative frequency table

Tags:

r

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?

like image 615
yasar Avatar asked Dec 01 '25 14:12

yasar


1 Answers

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.

like image 162
tophcito Avatar answered Dec 03 '25 04:12

tophcito



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!