How to find the proportion of each value for a cross tab obtained from a data frame in R?

R ProgrammingServer Side ProgrammingProgramming

To find the proportion of each value for a cross tab obtained from a data frame, we can use prop.table function. Suppose we have a data frame called df that contains three columns, two categorical say C1 and C2 and one numerical say Y then the cross tab will be created by using the command xtabs(Y~.,df). Now the proportion of each value can be found by using prop.table(xtabs(Y~.,df),1).

Example1

Consider the below data frame −

 Live Demo

f1<-sample(LETTERS[1:5],20,replace=TRUE)
f2<-sample(letters[1:5],20,replace=TRUE)
y1<-rpois(20,50)
df1<-data.frame(f1,f2,y1)
df1

Output

   f1 f2 y1
1  D  e  51
2  C  c  59
3  B  c  47
4  D  a  45
5  E  c  45
6  D  e  49
7  E  c  49
8  E  d  63
9  E  b  46
10 C  c  49
11 A  e  66
12 B  c  60
13 E  d  51
14 B  e  49
15 C  c  49
16 D  a  64
17 A  a  55
18 C  a  44
19 E  a  51
20 A  d  56

Creating cross tab for data in df1 −

xtabs(y1~.,df1)
f2
f1   a  b   c    d    e
A   55  0   0    56  66
B   0   0  107   0   49
C   44  0  157   0   0
D   109 0  0     0   100
E   51  46 94    114 0

Creating proportion table for data in df1 −

prop.table(xtabs(y1~.,df1),1)
f2
f1      a           b         c          d        e
A  0.3107345  0.0000000  0.0000000  0.3163842  0.3728814
B  0.0000000  0.0000000  0.6858974  0.0000000  0.3141026
C  0.2189055  0.0000000  0.7810945  0.0000000  0.0000000
D  0.5215311  0.0000000  0.0000000  0.0000000  0.4784689
E  0.1672131  0.1508197  0.3081967  0.3737705  0.0000000

Example 2

 Live Demo

v1<-sample(c("Asian","African","Europe","Oceania","Antarctica"),20,replace=TRUE)
v2<-sample(c("Male","Female"),20,replace=TRUE)
y2<-sample(2000:1000,20)
df2<-data.frame(v1,v2,y2)
df2

Output

    v1          v2       y2
1  African     Female   1904
2  Oceania    Male      1097
3  Asian      Male       1883
4  African    Male       1384
5  Europe     Female    1208
6  Oceania    Male       1494
7  Oceania    Female    1436
8  Antarctica Male      1858
9  Asian      Female    1396
10 Antarctica Female    1905
11 Europe     Female     1995
12 Oceania    Female    1045
13 Oceania    Female    1810
14 Europe     Female    1471
15 African    Female    1543
16 Oceania    Female    1519
17 African    Female    1912
18 Asian     Female     1520
19 Oceania    Male      1604
20 African    Male       1096

Creating cross tab for data in df2 −

xtabs(y2~.,df2)
v2
   v1       Female  Male
African     5359    2480
Antarctica  1905    1858
Asian       2916    1883
Europe      4674    0
Oceania     5810    4195

Creating proportion table for data in df2 −

prop.table(xtabs(y2~.,df2),1)
v2
  v1         Female      Male
African     0.6836331  0.3163669
Antarctica 0.5062450   0.4937550
Asian      0.6076266   0.3923734
Europe     1.0000000   0.0000000
Oceania   0.5807096   0.4192904
raja
Published on 06-Mar-2021 11:30:29
Advertisements