I need to table my data to a 2*2 table, however, due to no values in some cells, the table command in R does not provide a column or row depending on the data. For example:
a<-matrix(c(0,1,1,1,1,1,1,1),4,2)
table(a[,1],a[,2])
This is how it presents:
1
0 1
1 3
However, I need it to be like
0 1
0 0 1
1 0 3
Any suggestion?
The problem is that your matrix a
contains numbers and with numbers R
has no chance to know which columns should be shown. The solution is though easy. You have to transform you data into a factor, where you provide all potential values:
table(factor(a[,1], levels = unique(c(a))),factor(a[,2], levels = unique(c(a))))
# 0 1
# 0 0 1
# 1 0 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.