[R] Force square crosstabulation

2006-12-02 Thread Manuel Morales
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


signature.asc
Description: This is a digitally signed message part
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Force square crosstabulation

2006-12-02 Thread Duncan Murdoch
On 12/2/2006 8:26 PM, Manuel Morales wrote:
 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?

Force the categories to be factors.

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

 a b c d
   a 1 0 0 0
   b 0 1 0 0
   c 0 0 1 0
   d 0 0 1 0

The idea is that when you take subsets of factors, the levels stay the 
same, and table uses those levels for its categories.

Duncan Murdoch

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Force square crosstabulation

2006-12-02 Thread Bill.Venables
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Manuel Morales
Sent: Sunday, 3 December 2006 11:27 AM
To: r-help@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

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.