Hi, Is there a function which counts the frequencies of the occurence of a number within an interval? for example I have this vector: x <- c(1, 3, 1.2, 5, 5.9) and I want a vector that gives me the frequencies within an interval of 2, beginning at 0 (so the intervals are 0-2, 2-4, 4-6 and so on) so I get these frequencies: 2, 1, 2 Which functions do I have to use for this purpose?
2008/10/16 J?rg Gro? <joerg at licht-malerei.de>:> Hi, > > > Is there a function which counts the frequencies of the occurence of a > number within an interval? > > for example I have this vector: > > x <- c(1, 3, 1.2, 5, 5.9) > > and I want a vector that gives me the frequencies within an interval of 2, > beginning at 0 > (so the intervals are 0-2, 2-4, 4-6 and so on) > > so I get these frequencies: > > 2, 1, 2 > > > Which functions do I have to use for this purpose? >'cut' cuts your vector into chunks, and 'table' counts the number in each chunk: > x [1] 1.0 3.0 1.2 5.0 5.9 > table(cut(x,c(0,2,4,6))) (0,2] (2,4] (4,6] 2 1 2 Barry
?cut or ?findInterval and ?table -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of J?rg Gro? > Sent: Thursday, October 16, 2008 10:47 AM > To: r-help at r-project.org > Subject: [R] counting the frequencies of a vector > > Hi, > > > Is there a function which counts the frequencies of the occurence of a > number within an interval? > > for example I have this vector: > > x <- c(1, 3, 1.2, 5, 5.9) > > and I want a vector that gives me the frequencies within an interval > of 2, beginning at 0 > (so the intervals are 0-2, 2-4, 4-6 and so on) > > so I get these frequencies: > > 2, 1, 2 > > > Which functions do I have to use for this purpose? > > ______________________________________________ > 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.
Dear Jörg, See ?cut and ?table. Is this what you want? x <- c(1, 3, 1.2, 5, 5.9) table(cut(x,breaks=c(0,2,4,6))) (0,2] (2,4] (4,6] 2 1 2 HTH, Jorge On Thu, Oct 16, 2008 at 12:46 PM, Jörg Groß <joerg@licht-malerei.de> wrote:> Hi, > > > Is there a function which counts the frequencies of the occurence of a > number within an interval? > > for example I have this vector: > > x <- c(1, 3, 1.2, 5, 5.9) > > and I want a vector that gives me the frequencies within an interval of 2, > beginning at 0 > (so the intervals are 0-2, 2-4, 4-6 and so on) > > so I get these frequencies: > > 2, 1, 2 > > > Which functions do I have to use for this purpose? > > ______________________________________________ > 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]]
On Oct 16, 2008, at 12:55 PM, Jorge Ivan Velez wrote:> Dear J?rg, > See ?cut and ?table. Is this what you want? > > x <- c(1, 3, 1.2, 5, 5.9) > table(cut(x,breaks=c(0,2,4,6))) > (0,2] (2,4] (4,6] > 2 1 2Perhaps even greater future efficiency could be had by also adding ?seq table(cut(x, breaks=seq(0, 6, by=2))) A couple more keysrokes in this toy example but scales up much better to the requested purpose of evenly divided interval. The OP should also note carefully the open aspect on the left of these intervals. That can be controlled with option in cut: > x <- c(1, 3, 1.2, 5, 5.9, 6) > table(cut(x, breaks=seq(0,6,by=2))) (0,2] (2,4] (4,6] 2 1 3 > x <- c(1, 3, 1.2, 4, 5, 5.9, 6) > table(cut(x, breaks=seq(0,6,by=2))) (0,2] (2,4] (4,6] 2 2 3 > table(cut(x, breaks=seq(0,6,by=2), right=FALSE)) [0,2) [2,4) [4,6) 2 1 3 -- David Winsemius Heritage Labs> > > HTH, > > Jorge > > > On Thu, Oct 16, 2008 at 12:46 PM, J?rg Gro? <joerg at licht-malerei.de> > wrote: > >> Hi, >> >> >> Is there a function which counts the frequencies of the occurence >> of a >> number within an interval? >> >> for example I have this vector: >> >> x <- c(1, 3, 1.2, 5, 5.9) >> >> and I want a vector that gives me the frequencies within an >> interval of 2, >> beginning at 0 >> (so the intervals are 0-2, 2-4, 4-6 and so on) >> >> so I get these frequencies: >> >> 2, 1, 2 >> >> >> Which functions do I have to use for this purpose? >> >> ______________________________________________ >> 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. >> > > [[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.