[R] contingency table on data frame
Karin Lagesen
karin.lagesen at medisin.uio.no
Tue Jan 22 13:12:29 CET 2008
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
More information about the R-help
mailing list