Was it helpful?

Question

How to find contingency table of means from an R data frame using cast function?

R ProgrammingServer Side ProgrammingProgramming

The contingency table considers the numerical values for two categorical variables. Often, we require contingency table for counts, especially in non-parametric analysis but it is also possible that we want to use means for our analysis. Hence, we can use cast function from reshape package which solves the problem of creating contingency table easily.

Consider the below data frame −

Example

 Live Demo

set.seed(99)
x1<-rep(c("A","B","C"),times=c(8,7,5))
x2<-sample(c("Low","Medium","High"),20,replace=TRUE)
x3<-sample(1:9,20,replace=TRUE)
df1<-data.frame(x1,x2,x3)
df1

Output

    x1   x2   x3
1   A    Low 2
2   A    Medium 7
3   A    Medium 6
4   A    Low 5
5   A    High 8
6   A    Medium 7
7   A    Medium 6
8   A    Medium 5
9   B    Medium 1
10  B    Low 6
11  B    High 6
12  B   Low 7
13  B   Medium 4
14  B   Medium 5
15  B   Medium 7
16  C   Low 1
17  C   Medium 6
18  C   Medium 9
19  C   Low 9
20  C   High 3

Loading reshape package and creating contingency table for x3 using the combinations of x1 and x2 −

Example

library("reshape") cast(df1,x1~x2,mean,value="x3")

Output

x1 High Low Medium
1 A 8 3.5 6.20
2 B 6 6.5 4.25
3 C 3 5.0 7.50

Let’s have a look at another example −

Example

 Live Demo

y1<-rep(c("Male","Female"),each=10)
y2<-sample(c("Weekly","Monthly","Quarterly","Yearly"),20,replace=TRUE)
y3<-sample(101:110,20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2

Output

y1 y2 y3
1 Male Weekly 102
2 Male Yearly 107
3 Male Quarterly 110
4 Male Yearly 103
5 Male Quarterly 110
6 Male Monthly 110
7 Male Quarterly 105
8 Male Yearly 108
9 Male Quarterly 105
10 Male Quarterly 104
11 Female Quarterly 101
12 Female Monthly 108
13 Female Quarterly 103
14 Female Monthly 107
15 Female Weekly 109
16 Female Yearly 103
17 Female Quarterly 103
18 Female Quarterly 108
19 Female Quarterly 105
20 Female Weekly 105

Creating contingency table for y3 using the combinations of y1 and y2 −

cast(df2,y1~y2,mean,value="y3")
y1 Monthly Quarterly Weekly Yearly
1 Female 107.5 104.0 107 103
2 Male 110.0 106.8 102 106
raja
Published on 09-Oct-2020 17:03:44
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top