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@agrsci.dk <mailto:Joao.Fadista@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]]
Try something like this: x <- c(1,2,6,8,13,0,5,10, runif(10) * 100) tmp <- x %/% 5 + 1 bin.names <- paste((0:19) * 5, (1:20) * 5, sep="-") data.frame(Start = x, Binned_Start = bin.names[tmp]) This assumes you want (eg.) 5 in the 5-10 bin, not the 0-5 bin. You may also want to make Binned_Start into a factor rather than a character vector. HTH, Mike. Jo?o Fadista 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. > >-- View this message in context: http://www.nabble.com/binned-column-in-a-data.frame-tf4115787.html#a11705240 Sent from the R help mailing list archive at Nabble.com.
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?
Would "cut" meet your needs? Jo?o Fadista 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. > >-- View this message in context: http://www.nabble.com/binned-column-in-a-data.frame-tf4115787.html#a11717127 Sent from the R help mailing list archive at Nabble.com.