data$sal.cat <- cut(data$salary, breaks=seq(1855, 1915, by=10), right=FALSE)
data$ex.cat <- cut(data$ex, breaks=seq(1855, 1915, by=10), right=FALSE)
cbind( xtabs(~ sal.cat+ex.cat, data=data),
total=rowSums(xtabs(~ sal.cat+ex.cat, data=data)))
[1855,1865) [1865,1875) [1875,1885) [1885,1895)
[1855,1865) 0 0 0 0
[1865,1875) 0 0 0 0
[1875,1885) 1 0 0 0
[1885,1895) 2 0 0 0
[1895,1905) 4 5 0 1
[1905,1915) 0 0 0 0
[1895,1905) [1905,1915) total
[1855,1865) 0 0 0
[1865,1875) 0 0 0
[1875,1885) 0 0 1
[1885,1895) 0 0 2
[1895,1905) 0 0 10
[1905,1915) 0 0 0
I should mention that there are other approaches possible. There is a CrossTable
function in one of hte contributed packages that delivers a SAS/SPSS style crosstabs output, complete with boundaries made of “+” signs. There is a base::proptable function that will turn a contingency table into either cell , row or column proportions. One can get other options with:
library(sos)
findFn("crosstabs")
findFn("crosstabulation")
7
solved how to get the cross table in R? [closed]