[R] Controlling the Output from table()

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Oct 7 09:55:36 CEST 2007


On Sun, 7 Oct 2007, Öhagen Patrik wrote:

> Sorry for the vaugness.
>
> I have text data (exam grades, say) for different courses and I want to 
> table them. In my table I want the frequency for the lowest grade first 
> and the other frequencies in increaseing (grade) order. Some frequencies 
> might be zero but I want to include those empty cells in my table.

table() does not omit empty cells.  What is likely to be happening is that 
you have missing levels in the factor you supply or table() creates.  The 
way do this is to make your own ordered factor.  E.g.

> marks <- c(1,5,3,4,5,6)
> table(ordered(marks, levels=1:6))

1 2 3 4 5 6
1 0 1 1 2 1

>
> I hope that made sense?
>
> Thank you in advance!
>
>
> Cheer, Patrik
>
> -----Ursprungligt meddelande-----
> Från: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
> Skickat: den 7 oktober 2007 09:11
> Till: Öhagen Patrik
> Kopia: r-help at stat.math.ethz.ch
> Ämne: Re: [R] Controlling the Output from table()
>
> On Sun, 7 Oct 2007, Öhagen Patrik wrote:
>
>>
>>
>>
>> Dear All,
>>
>> I would like to have some controll over the output from the function
>> table(). I want to controll the order of the cells and I want to include
>> empty cells (if any).
>>
>> Example: I have ordinal data wich takes the values 1,2,3....N but I want
>> the output from table to put the frequencies in a different order, say,
>> 3,7,1,4.... and I want to include categories with zero frequency.
>>
>> How is that done?
>
> See ?tabulate.  You can reorder the result via indexing: I don't follow
> what you want and you did not give us an example.  (For example, is
> 'ordinal data' represented by an ordered factor or by an integer vector?)
>
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list