Hi, this is probably quite simple but I can't seem to do it correctly. I have a data frame of counts of infections in different ages; something like: count=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 7, 8, 8, 9, 9, 10, 11, 15, 17, 17, 17, 17, 19, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 25, 27, 31, 33 ) age=c(3, 8, 9, 7, 56, 58, 10, 13, 53, 55, 11, 12, 14, 51, 54, 15, 50, 52, 18, 49, 48, 47, 20, 16, 17, 25, 45, 29, 33, 36, 41, 35, 43, 46, 21, 27, 31, 28, 32, 38, 42, 22, 34, 37, 40, 44, 24, 39, 30, 26, 19, 23) frame=data.frame(count, age) But the data are too grainy and I would like to bin them in age groups (that I chose) to be something like: age<10 ...... count=5 10=<age <20 ..... count=8 etc... What is the easiest and quickest way to do this? Thanks -- View this message in context: http://www.nabble.com/Binning-groups-tp17399482p17399482.html Sent from the R help mailing list archive at Nabble.com.
Is this what you want:> count=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 7, 8, 8, 9, 9,+ 10, 11, 15, 17, 17, 17, 17, 19, 19, 19, 19, 20, 20, 20, 21, 21, + 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 25, 27, 31, 33 + )> age=c(3, 8, 9, 7, 56, 58, 10, 13, 53, 55, 11, 12, 14, 51, 54,+ 15, 50, 52, 18, 49, 48, 47, 20, 16, 17, 25, 45, 29, 33, 36, 41, + 35, 43, 46, 21, 27, 31, 28, 32, 38, 42, 22, 34, 37, 40, 44, 24, + 39, 30, 26, 19, 23)> # create a vector with each age separately for its count > bins <- rep(age, times=count) > # setup the groups for the bins > groups <- seq(0,100, 10) > tapply(bins, cut(bins, groups), length)(0,10] (10,20] (20,30] (30,40] (40,50] (50,60] (60,70] (70,80] (80,90] (90,100] 8 111 232 216 159 26 NA NA NA NA> > >On Thu, May 22, 2008 at 4:19 AM, francogrex <francogrex@mail.com> wrote:> > Hi, this is probably quite simple but I can't seem to do it correctly. I > have > a data frame of counts of infections in different ages; something like: > count=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 7, 8, 8, 9, 9, > 10, 11, 15, 17, 17, 17, 17, 19, 19, 19, 19, 20, 20, 20, 21, 21, > 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 25, 27, 31, 33 > ) > age=c(3, 8, 9, 7, 56, 58, 10, 13, 53, 55, 11, 12, 14, 51, 54, > 15, 50, 52, 18, 49, 48, 47, 20, 16, 17, 25, 45, 29, 33, 36, 41, > 35, 43, 46, 21, 27, 31, 28, 32, 38, 42, 22, 34, 37, 40, 44, 24, > 39, 30, 26, 19, 23) > > frame=data.frame(count, age) > > But the data are too grainy and I would like to bin them in age groups > (that > I chose) to be something like: > age<10 ...... count=5 > 10=<age <20 ..... count=8 > etc... > What is the easiest and quickest way to do this? Thanks > -- > View this message in context: > http://www.nabble.com/Binning-groups-tp17399482p17399482.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<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? [[alternative HTML version deleted]]
If you just want to bin the current ages:> count=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 7, 8, 8, 9, 9,+ 10, 11, 15, 17, 17, 17, 17, 19, 19, 19, 19, 20, 20, 20, 21, 21, + 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 25, 27, 31, 33 + )> age=c(3, 8, 9, 7, 56, 58, 10, 13, 53, 55, 11, 12, 14, 51, 54,+ 15, 50, 52, 18, 49, 48, 47, 20, 16, 17, 25, 45, 29, 33, 36, 41, + 35, 43, 46, 21, 27, 31, 28, 32, 38, 42, 22, 34, 37, 40, 44, 24, + 39, 30, 26, 19, 23)> # create a vector with each age separately for its count > bins <- rep(age, times=count) > # setup the groups for the bins > groups <- seq(0,100, 10) - 1 > tapply(bins, cut(bins, groups), length)(-1,9] (9,19] (19,29] (29,39] (39,49] (49,59] (59,69] (69,79] (79,89] (89,99] 5 99 222 218 174 34 NA NA NA NA> # or just the counts > table(cut(age, groups))(-1,9] (9,19] (19,29] (29,39] (39,49] (49,59] (59,69] (69,79] (79,89] (89,99] 4 10 10 10 10 8 0 0 0 0> >On Thu, May 22, 2008 at 4:19 AM, francogrex <francogrex@mail.com> wrote:> > Hi, this is probably quite simple but I can't seem to do it correctly. I > have > a data frame of counts of infections in different ages; something like: > count=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 7, 8, 8, 9, 9, > 10, 11, 15, 17, 17, 17, 17, 19, 19, 19, 19, 20, 20, 20, 21, 21, > 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 25, 27, 31, 33 > ) > age=c(3, 8, 9, 7, 56, 58, 10, 13, 53, 55, 11, 12, 14, 51, 54, > 15, 50, 52, 18, 49, 48, 47, 20, 16, 17, 25, 45, 29, 33, 36, 41, > 35, 43, 46, 21, 27, 31, 28, 32, 38, 42, 22, 34, 37, 40, 44, 24, > 39, 30, 26, 19, 23) > > frame=data.frame(count, age) > > But the data are too grainy and I would like to bin them in age groups > (that > I chose) to be something like: > age<10 ...... count=5 > 10=<age <20 ..... count=8 > etc... > What is the easiest and quickest way to do this? Thanks > -- > View this message in context: > http://www.nabble.com/Binning-groups-tp17399482p17399482.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<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? [[alternative HTML version deleted]]
On Thu, 22 May 2008, francogrex wrote: f > f > Hi, this is probably quite simple but I can't seem to do it correctly. I have f > a data frame of counts of infections in different ages; something like: f > count=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 7, 8, 8, 9, 9, f > 10, 11, 15, 17, 17, 17, 17, 19, 19, 19, 19, 20, 20, 20, 21, 21, f > 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 25, 27, 31, 33 f > ) f > age=c(3, 8, 9, 7, 56, 58, 10, 13, 53, 55, 11, 12, 14, 51, 54, f > 15, 50, 52, 18, 49, 48, 47, 20, 16, 17, 25, 45, 29, 33, 36, 41, f > 35, 43, 46, 21, 27, 31, 28, 32, 38, 42, 22, 34, 37, 40, 44, 24, f > 39, 30, 26, 19, 23) f > f > frame=data.frame(count, age) f > f > But the data are too grainy and I would like to bin them in age groups (that f > I chose) to be something like: f > age<10 ...... count=5 f > 10=<age <20 ..... count=8 f > etc... One method is to define a new variable, say age.cut and then to table it: frame$age.cut = cutframe$age, c(0,11,20,35,60),right=FALSE) #check frame$age.cut to see that boundaries are really as #you wanted them frame$age.cut table(frame$age.cut)
see ?findIntervals francogrex wrote:> Hi, this is probably quite simple but I can't seem to do it correctly. I have > a data frame of counts of infections in different ages; something like: > count=c(1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 7, 8, 8, 9, 9, > 10, 11, 15, 17, 17, 17, 17, 19, 19, 19, 19, 20, 20, 20, 21, 21, > 21, 22, 22, 22, 22, 23, 23, 23, 23, 23, 24, 24, 25, 27, 31, 33 > ) > age=c(3, 8, 9, 7, 56, 58, 10, 13, 53, 55, 11, 12, 14, 51, 54, > 15, 50, 52, 18, 49, 48, 47, 20, 16, 17, 25, 45, 29, 33, 36, 41, > 35, 43, 46, 21, 27, 31, 28, 32, 38, 42, 22, 34, 37, 40, 44, 24, > 39, 30, 26, 19, 23) > > frame=data.frame(count, age) > > But the data are too grainy and I would like to bin them in age groups (that > I chose) to be something like: > age<10 ...... count=5 > 10=<age <20 ..... count=8 > etc... > What is the easiest and quickest way to do this? Thanks