[R] add factor to dataframe given ranges

Marc Schwartz (via MN) mschwartz at mn.rr.com
Thu Dec 22 17:08:08 CET 2005


On Thu, 2005-12-22 at 16:58 +0100, Albert Vilella wrote:
> Hi all,
> 
> I would like to factorize the entries in a dataframe given some
> groupings. E.g:
> 
> mydf = data.frame(
>   a = rnorm(100,10),
>   b = rnorm(100,10),
>   c = rgamma(100, 1, scale=1))
> 
> group = hist(mydf$c, breaks="FD")
> group$breaks
> 
> The idea is to create a factor "mydf$d" with levels corresponding to
> the ranges in group$breaks.
> 
> There must be an easy way to do this that I haven't found out.
> 
> Thanks in advance,
> 
>     Albert.

See ?cut

Then:

 myfac.c <- cut(mydf$c, breaks = group$breaks)

Take note of the additional arguments in cut() relative to the nature of
the interval cutpoints (ie. 'right' and 'include.lowest').

HTH,

Marc Schwartz




More information about the R-help mailing list