Reputation: 311
I have a matrix like so:
country cLabel
[1,] 3 1
[2,] 6 2
[3,] 8 1
[4,] 5 2
[5,] 5 2
[6,] 8 2
[7,] 8 2
[8,] 8 2
[9,] 8 2
[10,] 4 2
[11,] 6 2
[12,] 3 2
[13,] 5 2
[14,] 5 1
country
is a value of 1-8, and cLabel
is a value of 1-2. How can I print the contingency table for this? I tried print(table(myMatrix))
.
It is printing
1 2 3 4 5 6 7 8
60 277 31 32 83 39 24 44
and what I want is it to print each country value (1-8) and how many 1s and 2s there are for each of these 8 values.
Upvotes: 0
Views: 139
Reputation:
I guess there is a duplicate somewhere.
# Turn your matrix into a data.frame, easier to manipulate and to create labels
myDataFrame <- as.data.frame(myMatrix)
# Add factors to coutry, from 1 to 8. This will add missing levels to the final result
myDataFrame$country <- factor(myDataFrame$country, 1:8)
# Table
table(myDataFrame)
# cLabel
# country 1 2
# 1 0 0
# 2 0 0
# 3 1 1
# 4 0 1
# 5 1 3
# 6 0 2
# 7 0 0
# 8 1 4
Upvotes: 1