[R] binned column in a data.frame

jim holtman jholtman at gmail.com
Fri Jul 20 14:56:28 CEST 2007


You can also use 'cut' to break the bins:

> x <- c(1,2,6,8,13,0,5,10, runif(10) * 100)
> x.bins <- seq(0, max(x)+5, 5)
> x.cut <- cut(x, breaks=x.bins, include.lowest=TRUE)
> x.names <- paste(head(x.bins, -1), tail(x.bins, -1), sep='-')
> data.frame(x, bins=x.names[x.cut])
          x  bins
1   1.00000   0-5
2   2.00000   0-5
3   6.00000  5-10
4   8.00000  5-10
5  13.00000 10-15
6   0.00000   0-5
7   5.00000   0-5
8  10.00000  5-10
9  75.85256 75-80
10 38.20424 35-40
11 77.30647 75-80
12 62.02278 60-65
13 73.42095 70-75
14 78.69244 75-80
15 66.52972 65-70
16 61.64897 60-65
17 23.99252 20-25
18 42.08632 40-45


On 7/20/07, João Fadista <Joao.Fadista at agrsci.dk> wrote:
> Dear all,
>
> I would like to know how can I create a binned column in a data.frame. The output that I would like is something like this:
>
> Start  Binned_Start
> 1        0-5
> 2        0-5
> 6        5-10
> 8        5-10
> 13      10-15
> ...
>
>
>
>
> Best regards
>
> João Fadista
> Ph.d. student
>
>
>
>         UNIVERSITY OF AARHUS
> Faculty of Agricultural Sciences
> Dept. of Genetics and Biotechnology
> Blichers Allé 20, P.O. BOX 50
> DK-8830 Tjele
>
> Phone:   +45 8999 1900
> Direct:  +45 8999 1900
> E-mail:  Joao.Fadista at agrsci.dk <mailto:Joao.Fadista at agrsci.dk>
> Web:     www.agrsci.org <http://www.agrsci.org/>
> ________________________________
>
> News and news media <http://www.agrsci.org/navigation/nyheder_og_presse> .
>
> This email may contain information that is confidential. Any use or publication of this email without written permission from Faculty of Agricultural Sciences is not allowed. If you are not the intended recipient, please notify Faculty of Agricultural Sciences immediately and delete this email.
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list