Hello I am very new to R. I have an R task to complete that I have not been able to find a straightforward answer to as of yet. I have a list of values. I would like to count the number of values that are in one bin, the number that fall in the next bin, etc. For example My input file is: 123 48 342 442 43 232 32 129 191 147 I would like the output to be similar to: 0-100 3 100-200 4 200-3001 300-400 1 400-5001 Thus far I have tried working with hist, cut and split, but have not gotten very far. Thanks Kat [[alternative HTML version deleted]]
Hi Kat, Something along the lines of the following might work: x <- c(123, 48, 342, 442, 43, 232, 32, 129, 191, 147) table(cut(x, c(0, 100, 200, 300, 400, 500))) See ?cut and ?table for more details. Best, Jorge.- On Mon, Jun 11, 2012 at 5:22 PM, Kathryn B Walters-Conte <> wrote:> Hello > > I am very new to R. I have an R task to complete that I have not been > able to find a straightforward answer to as of yet. I have a list of > values. I would like to count the number of values that are in one bin, the > number that fall in the next bin, etc. > > For example > > My input file is: 123 48 342 442 43 232 32 129 191 147 > > I would like the output to be similar to: > > 0-100 3 > 100-200 4 > 200-3001 > 300-400 1 > 400-5001 > > > Thus far I have tried working with hist, cut and split, but have not > gotten very far. > > Thanks > Kat > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > >[[alternative HTML version deleted]]
Hi Kat, Try this: ?x1<-c(123, 48, 342, 442, 43, 232, 32, 129, 191, 147) ? res<-cut(x1,breaks=seq(0,500,by=100)) library(reshape) ?res2<-melt(table(res)) ?colnames(res2)<-c("bin","count")> res2??????? bin count 1?? (0,100]???? 3 2 (100,200]???? 4 3 (200,300]???? 1 4 (300,400]???? 1 5 (400,500]???? 1 I am trying to figure out how to get rid of the brackets.? Otherwise, the results looks fine. A.K. ----- Original Message ----- From: Kathryn B Walters-Conte <kbw1123 at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Monday, June 11, 2012 5:22 PM Subject: [R] Simple Binning of Values Hello I am very new to R. ?I have an R task to complete that I have not been able to find a straightforward answer to as of yet. ?I have a list of values. I would like to count the number of values that are in one bin, the number that fall in the next bin, etc. For example My input file is: ?123 48 342 442 43 232 32 129 191 147 I would like the output to be similar to:? 0-100 3 100-200?4 200-3001 300-400?1 400-5001 Thus far I have tried working with hist, cut and split, but have not gotten very far. ? Thanks Kat ??? [[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.
Hi Kat, I was able to replace the brackets. I guess this will be the results you wanted. ?x1<-c(123, 48, 342, 442, 43, 232, 32, 129, 191, 147) ? res<-cut(x1,breaks=seq(0,500,by=100)) library(reshape) ?res2<-melt(table(res)) ?colnames(res2)<-c("bin","count") res3<-as.matrix(res2) ?res3<-apply(res3,2,function(x) gsub("\\(|\\]","",x)) ?res3<-data.frame(apply(res3,2,function(x)gsub("\\,","-",x))) ?res3 ????? bin count 1?? 0-100???? 3 2 100-200???? 4 3 200-300???? 1 4 300-400???? 1 5 400-500???? 1 A.K. ----- Original Message ----- From: arun <smartpink111 at yahoo.com> To: Kathryn B Walters-Conte <kbw1123 at yahoo.com> Cc: R help <r-help at r-project.org> Sent: Monday, June 11, 2012 10:04 PM Subject: Re: [R] Simple Binning of Values Hi Kat, Try this: ?x1<-c(123, 48, 342, 442, 43, 232, 32, 129, 191, 147) ? res<-cut(x1,breaks=seq(0,500,by=100)) library(reshape) ?res2<-melt(table(res)) ?colnames(res2)<-c("bin","count")> res2??????? bin count 1?? (0,100]???? 3 2 (100,200]???? 4 3 (200,300]???? 1 4 (300,400]???? 1 5 (400,500]???? 1 I am trying to figure out how to get rid of the brackets.? Otherwise, the results looks fine. A.K. ----- Original Message ----- From: Kathryn B Walters-Conte <kbw1123 at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Monday, June 11, 2012 5:22 PM Subject: [R] Simple Binning of Values Hello I am very new to R. ?I have an R task to complete that I have not been able to find a straightforward answer to as of yet. ?I have a list of values. I would like to count the number of values that are in one bin, the number that fall in the next bin, etc. For example My input file is: ?123 48 342 442 43 232 32 129 191 147 I would like the output to be similar to:? 0-100 3 100-200?4 200-3001 300-400?1 400-5001 Thus far I have tried working with hist, cut and split, but have not gotten very far. ? Thanks Kat ??? [[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.