[R] Odp: Creating a vector of categories

Petr PIKAL petr.pikal at precheza.cz
Fri Mar 26 12:05:13 CET 2010


Hi

r-help-bounces at r-project.org napsal dne 26.03.2010 10:41:29:

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

Break your counts into desired levels, 
see ?cut
cut(1:100, breaks=10)


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

aggregate your data

with(your.data, aggregate(count, list(sex, language, cutted.count), 
length))

Regards
Petr


> 
> 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!
> Chris
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.



More information about the R-help mailing list