Re: [R] Keeping factors with zero occurrences in "table" output

From: Achim Zeileis <Achim.Zeileis_at_wu-wien.ac.at>
Date: Thu 21 Apr 2005 - 02:12:49 EST

On Wed, 20 Apr 2005 11:38:13 -0400 Ravi Varadhan wrote:

> Dear R group,
>
>
>
> I have a data frame which contains data on preferences on 7 items
> (ranks 1 through 7) listed by each participant. I would like to
> tabulate this in a 7x7 table where the rows would be the items and the
> columns would be the number of times that item received a particular
> rank.
>
> I tried doing this by creating a matrix by "rbind"ing each vector
> obtained using "table" on each item. This, however, does not work
> because there are some items which didn't receive some of the ranks,
> so "table" does not list that item. Is there a way to force it to
> list the item and give a "0" to it? Or, is there a simpler way to
> solve this problem?

I think the cleanest way is to store the preferences as a "factor" and tell R at the creation of that factor what the levels are, i.e.   preference <- factor(preference, levels = 1:7) Then calling
  table(preference)
will also report categories with 0 observations. Z  

>
>
> Thanks for any help,
>
> Ravi.
>
>
>
> ---------------------------------------------------------------------
> -----
>
> Ravi Varadhan, Ph.D.
>
> Assistant Professor, The Center on Aging and Health
>
> Division of Geriatric Medicine and Gerontology
>
> Johns Hopkins University
>
> Ph: (410) 502-2619
>
> Fax: (410) 614-9625
>
> Email: <mailto:rvaradhan@jhmi.edu> rvaradhan@jhmi.edu
>
> ---------------------------------------------------------------------
> -----
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Apr 21 03:26:59 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:18 EST