[R] contingency table on data frame
John Kane
jrkrideau at yahoo.ca
Tue Jan 22 15:12:04 CET 2008
It is not a good idea to use "sample' when building an
example like this! Running the code does not give the
example dataframe.
This is crude but it will do what you want.
x <- " Z Y X
1 4 Yes
2 1 No
3 2 Perhaps
4 3 Yes
5 4 No
6 5 No
7 1 Yes
8 4 Perhaps
9 4 Yes
10 2 Perhaps "
xx <- read.table(textConnection(x), header=TRUE,
as.is=TRUE); xx
df1 <- data.frame(xx[,3],xx[,2]) ; df1
df2 <- unique(df1)
table(df2[,1])
--- Karin Lagesen <karin.lagesen at medisin.uio.no>
wrote:
>
> 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.
Looking for the perfect gift? Give the gift of Flickr!
More information about the R-help
mailing list