Skip to content
Back to formatted view

Raw Message

Message-ID: <B998A44C8986644EA8029CFE6396A9248408D5@exqld2-bne.qld.csiro.au>
Date: 2006-12-03T03:57:56Z
From: Bill Venables
Subject: Force "square" crosstabulation

Use factors with specified levels.

> lev <- letters[1:4]
> table(factor(letters[1:4], levels = lev),
	   factor(letters[c(1:3,3)], levels = lev)) 
   
    a b c d
  a 1 0 0 0
  b 0 1 0 0
  c 0 0 1 0
  d 0 0 1 0

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Manuel Morales
Sent: Sunday, 3 December 2006 11:27 AM
To: r-help at stat.math.ethz.ch
Subject: [R] 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!
-- 
Manuel A. Morales
http://mutualism.williams.edu