Does anybody know a way to determine the longest run of a number in a vector of numbers? For example if I have the following numbers: 0 0 1 1 1 1 0 1 1 0 0 0 1 1; how can I determine that the longest run of 1's = 4 and longest run of 0's = 3? Thanks, Mark
I believe you're looking for the rle() function. Cheers, Michael On Fri, Nov 2, 2012 at 9:20 PM, Mark Del Cerro <Mark.DelCerro at powerint.com> wrote:> Does anybody know a way to determine the longest run of a number in a > vector of numbers? > For example if I have the following numbers: 0 0 1 1 1 1 0 1 1 0 0 0 1 > 1; how can I determine that the longest run of 1's = 4 and longest run > of 0's = 3? > > Thanks, > Mark > > ______________________________________________ > 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, Try this: vec1<-c(0,0,1,1,1,1,0,1,1,0,0,0,1,1) tapply(rle(vec1)$lengths,rle(vec1)$values,FUN=max) #0 1 #3 4 A.K. ----- Original Message ----- From: Mark Del Cerro <Mark.DelCerro at powerint.com> To: r-help at r-project.org Cc: Sent: Friday, November 2, 2012 5:20 PM Subject: [R] Determine longest run of number Does anybody know a way to determine the longest run of a number in a vector of numbers? For example if I have the following numbers: 0 0 1 1 1 1 0 1 1 0 0 0 1 1; how can I determine that the longest run of 1's = 4 and longest run of 0's = 3? Thanks, Mark ______________________________________________ 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.
Seemingly Similar Threads
- A function for random test based on longest run (UNCLASSI FIED)
- Determine the Length of the Longest Word in a String
- Need help locating the longest series of consecutive numbers in a matrix
- Help me replace a for loop with an "apply" function
- Extracting the longest entry