[R] Creating a vector of categories

Jim Lemon jim at bitwrit.com.au
Fri Mar 26 12:02:27 CET 2010


On 03/26/2010 08:41 PM, Christoffer Karlsson wrote:
> Hi,
>
> I have a column in a data frame looking something like:
>
> $sex $language $count
> male  english  0
> male  english  0
> female  english  32
> male  spanish  154
> female  english  11
> female  norweigan 7
>
> and so on.
> What I want to do is to order these in to categories, for instance one
> category where count>=0&  count<10 and so on..
>
> I want my data to turn out looking something like:
>
> male english 0-10 1324
> male english 11-20 756
> .....
> male spanish 0-10 354
> ...
> female english 0-10 1557
> ...
>
> and so on, where the right hand is the count of the number of people in each
> category.
> Up until now I've been subsetting the data frame into each category, and
> then counting number of rows in each subset. However I now have a large
> amount of different factor combinations which makes this process tedious.
>
> Any help would be appreciated!

Hi Chris,
As luck would have it, I have been working on a very similar problem, 
that of graphically representing multi-level summaries. What you could 
do is to create a new factor variable with the "cut" function (say, 
"countcut"), then call the "by" function like this:

by(mydf$sex,list(mydf$language,mydf$countcut),sum)

You will not get the format you have specified, but you will get the 
numbers that can be reformatted.

Jim



More information about the R-help mailing list