Hi, I would like to be able to count a sequence of numbers. For example, given a vector of the following integers (1,1,1,2,2,2,2,3,3,3,3,3,3,1,1,1,1, 3,3) the function would return (3, 4, 6, 4,2) Does anyone have any cool ideas to solve this? Any help appreciated. Regards, Sam. [[alternative HTML version deleted]]
On Thu, 15 Feb 2007, Samuel Kemp wrote:> Hi, > > I would like to be able to count a sequence of numbers. For example, given a > vector of the following integers > > (1,1,1,2,2,2,2,3,3,3,3,3,3,1,1,1,1, 3,3) > > the function would return > > (3, 4, 6, 4,2)?rle> x <- c(1,1,1,2,2,2,2,3,3,3,3,3,3,1,1,1,1, 3,3) > rle(x)$lengths[1] 3 4 6 4 2> > Does anyone have any cool ideas to solve this? > > Any help appreciated. > > Regards, > > Sam. > > [[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. >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
Samuel Kemp wrote:> Hi, > > I would like to be able to count a sequence of numbers. For example, given a > vector of the following integers > > (1,1,1,2,2,2,2,3,3,3,3,3,3,1,1,1,1, 3,3) > > the function would return > > (3, 4, 6, 4,2) > > Does anyone have any cool ideas to solve this? > >Maybe not the most efficient way, but > count <- function (x) diff(c(0,which(diff(x)!=0),length(x))) should work (for integer sequences!): > x<-c(1,1,1,2,2,2,2,3,3,3,3,3,3,1,1,1,1,3,3) > count(x) [1] 3 4 6 4 2 Regards, Martin> Any help appreciated. > > Regards, > > Sam. > > [[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. >
The function "rle" will give you what you are looking for. Hope this helps, Matt Wiener -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Martin Becker Sent: Thursday, February 15, 2007 5:52 AM To: Samuel Kemp Cc: r-help at stat.math.ethz.ch Subject: Re: [R] count sequence of integers [Broadcast] Samuel Kemp wrote:> Hi, > > I would like to be able to count a sequence of numbers. For example,given a> vector of the following integers > > (1,1,1,2,2,2,2,3,3,3,3,3,3,1,1,1,1, 3,3) > > the function would return > > (3, 4, 6, 4,2) > > Does anyone have any cool ideas to solve this? > >Maybe not the most efficient way, but > count <- function (x) diff(c(0,which(diff(x)!=0),length(x))) should work (for integer sequences!): > x<-c(1,1,1,2,2,2,2,3,3,3,3,3,3,1,1,1,1,3,3) > count(x) [1] 3 4 6 4 2 Regards, Martin> Any help appreciated. > > Regards, > > Sam. > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >______________________________________________ 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. ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}