[R] contingency table on data frame

From: Karin Lagesen <karin.lagesen_at_medisin.uio.no>
Date: Tue, 22 Jan 2008 13:12:29 +0100

I am sorry if this is a faq or tutorial somewhere, but I am unable to solve this one.

What I am looking for is a count of how many different categories(numbers in this case) that appears for a given factor.

Example:

> l <- c("Yes", "No", "Perhaps")
> x <- factor( sample(l, 10, replace=T), levels=l )
> m <- c(1:5)
> y <- factor( sample(m, 10, replace=T), levels=m )
> z = c(1:10)
> my_df = data.frame("Z" = z, "Y"= y, "X" = x)
> my_df

    Z Y       X
1   1 4     Yes
2   2 1      No
3   3 2 Perhaps
4   4 3     Yes
5   5 4      No
6   6 5      No
7   7 1     Yes
8   8 4 Perhaps
9   9 4     Yes

10 10 2 Perhaps
>

I am now looking for a table that will give me this:

Yes      3       # Yes has these ys: 4,3,1,4, two are the same, ergo 3
No       3       # No has these ys: 1,4,5
Perhaps  2       # Perhaps has these ys: 2,4,2

My dataframe has lots of other colums too, but I only want this information out.

Thankyou for your help!

Karin

-- 
Karin Lagesen, PhD student
karin.lagesen_at_medisin.uio.no
http://folk.uio.no/karinlag

______________________________________________
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 Tue 22 Jan 2008 - 12:22:46 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 Tue 22 Jan 2008 - 14:30:07 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