yasar
yasar

Reputation: 13738

How to draw relative frequency table

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?

Upvotes: 2

Views: 15465

Answers (3)

agstudy
agstudy

Reputation: 121568

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

Upvotes: 0

tophcito
tophcito

Reputation: 721

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.

Upvotes: 11

juba
juba

Reputation: 49033

That really is a FAQ.

Just divide your table by its sum :

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

See also ?prop.table.

Upvotes: 5

Related Questions