2

I have a set of data that looks like this:

df <- data.frame(typeA = c(T,T,F,F,T,F,T,F), typeB = c(T,F,T,F,T,F,T,F), typeC = c(F,F,F,F,T,T,T,T))

and I would like to output a table that looks like

|       | TypeA | TypeB | TypeC |  
|-------|-------|-------|-------|  
| TypeA | 4     | 3     | 2     |  
| TypeB | 3     | 4     | 2     |  
| TypeC | 2     | 2     | 4     |  

where each cell is number of observations that has both row AND column TRUE.

eg: First row second column shows that there are 3 obs with TypeA == T && TypeB == T.

Also, what is the name of such table summary?

Ernest Han
  • 395
  • 1
  • 3
  • 10

2 Answers2

3
sapply(df, function(x) sapply(df, function(y) sum(x * y)))
#OR
t(df) %*% as.matrix(df)
#      typeA typeB typeC
#typeA     4     3     2
#typeB     3     4     2
#typeC     2     2     4
d.b
  • 32,245
  • 6
  • 36
  • 77
  • Can you please provide a brief explanation as to what is going on in this solution? Please and thank you. – Eric Jun 04 '20 at 18:10
2

crossprod is what what you are looking for

crossprod(as.matrix(df))
#      typeA typeB typeC
#typeA     4     3     2
#typeB     3     4     2
#typeC     2     2     4

That is

m <- as.matrix(df)
t(m) %*% m
markus
  • 25,843
  • 5
  • 39
  • 58