Re: [R] contingency table on data frame

From: John Kane <jrkrideau_at_yahoo.ca>
Date: Tue, 22 Jan 2008 09:12:04 -0500 (EST)


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])  

>
> 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!



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 - 14:13:50 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 - 15: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