[R] counting with factors

Marc Schwartz marc_schwartz at comcast.net
Fri Nov 2 17:52:59 CET 2007


On Fri, 2007-11-02 at 12:01 -0400, Bernd Jagla wrote:
> Hi there,

> I have something that appears to be a factor called drug:
> 
> Typeof(drug) => Integer

This is because the underlying data type of a factor is an integer.

> As.numeric(drug) gives a long list

This gives you the integer storage code for each element of the factor

> Levels(drug) gives a long list, too.

This effectively gives you the unique entries in the factor, each of
which has been given a level.

> Now I want something like the summary function does:
> 
> I want to count how often each level occurs in the given vector.
> 
> My problem is that summary gives me a list with counts that is incomplete
> and adds the "Others" category.

> Is there a way to get a count for all levels?

To create a frequency *table* of the occurrences of your factor use:

  table(drug)

See ?table

Also, if you imported this data using one of the read.table() family of
functions, see the 'as.is', 'colClasses' and 'stringsAsFactors'
arguments, which can be set so that character columns are not converted
to factors.

HTH,

Marc Schwartz



More information about the R-help mailing list