Skip to content
Prev 105454 / 398506 Next

Force "square" crosstabulation

Hello list members,

I'm looking for a way to force the results of a crosstabulation to be
square - that is, to include 0 values.

For example:

table(letters[1:4],letters[c(1:3,3)])

yields:
    a b c
  a 1 0 0
  b 0 1 0
  c 0 0 1
  d 0 0 1

I would like to return:
    a b c d
  a 1 0 0 0
  b 0 1 0 0
  c 0 0 1 0
  d 0 0 1 0

Any suggestions?

Thanks!