Re: [R] add factor to dataframe given ranges

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Fri 23 Dec 2005 - 03:13:58 EST

On Thu, 2005-12-22 at 10:08 -0600, Marc Schwartz (via MN) wrote:
> 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)

One quick update here which is more clearly based upon your comment above:

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

Which will add the resultant factor as a new column 'd' to your existing mydf.

Marc



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Dec 23 08:04:37 2005

This archive was generated by hypermail 2.1.8 : Fri 23 Dec 2005 - 09:43:17 EST