Re: [R] Multidimensional contingency tables

From: Giovanni Petris <GPetris_at_uark.edu>
Date: Wed, 23 Apr 2008 15:28:27 -0500 (CDT)

It seems to me that a combination of ftable and xtabs works fine:

> prob1<- data.frame(victim=c(rep('white',4),rep('black',4)),
+ perp=c(rep('white',2),rep('black',2),rep('white',2),rep('black',2)), + death=rep(c('yes','no'),4), count=c(19,132,11,52,0,9,6,97))
> prob1

  victim perp death count
1 white white yes 19
2 white white no 132
3 white black yes 11
4 white black no 52

5  black white   yes     0
6  black white    no     9
7  black black   yes     6

8 black black no 97
> ftable(xtabs(count ~ victim + perp + death, data = prob1))
             death  no yes
victim perp               
black  black        97   6
       white         9   0
white  black        52  11
       white       132  19
 

Best,
Giovanni

> Date: Tue, 22 Apr 2008 08:24:36 -0500
> From: hadley wickham <h.wickham_at_gmail.com>
> Sender: r-help-bounces_at_r-project.org
> Cc: R-help_at_stat.math.ethz.ch
> Precedence: list
>
> On Mon, Apr 21, 2008 at 9:46 PM, Robert A. LaBudde <ral_at_lcfltd.com> wrote:
> > How does one ideally handle and display multidimenstional contingency
> > tables in R v. 2.6.2?
> >
> > E.g.:
> >
> > > prob1<- data.frame(victim=c(rep('white',4),rep('black',4)),
> > + perp=c(rep('white',2),rep('black',2),rep('white',2),rep('black',2)),
> > + death=rep(c('yes','no'),4), count=c(19,132,11,52,0,9,6,97))
> > > prob1
> > victim perp death count
> > 1 white white yes 19
> > 2 white white no 132
> > 3 white black yes 11
> > 4 white black no 52
> > 5 black white yes 0
> > 6 black white no 9
> > 7 black black yes 6
> > 8 black black no 97
> >
> > The xtabs() function doesn't seem appropriate, as it has no means of
> > using 'count'.
> >
> > This must be a common problem.
>
> You can also use the reshape package (http://had.co.nz/reshape)
>
> cast(prob1, victim ~ perp, sum, value="count")
> cast(prob1, victim ~ perp ~ death, sum, value="count")
> cast(prob1, death + victim ~ perp, sum, value="count")
>
> etc.
>
> Hadley
> --
> http://had.co.nz/
>
> ______________________________________________
> R-help_at_r-project.org 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.
>
>

-- 

Giovanni Petris  <GPetris_at_uark.edu>
Associate Professor
Department of Mathematical Sciences
University of Arkansas - Fayetteville, AR 72701
Ph: (479) 575-6324, 575-8630 (fax)
http://definetti.uark.edu/~gpetris/

______________________________________________
R-help_at_r-project.org 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.
Received on Wed 23 Apr 2008 - 21:00:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Apr 2008 - 21:30:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive