Using data.table
, which has the function rleid()
:
require(data.table) # v1.9.5+
rleid(x)
# [1] 1 2 3 4 5 5 6 7 7 8 8 8 9
More Related Contents:
- Is the “*apply” family really not vectorized?
- Speed up the loop operation in R
- Is there an R function for finding the index of an element in a vector?
- Fastest way to replace NAs in a large data.table
- Loop in R to read many files
- Read all files in directory and apply multiple functions to each data frame [duplicate]
- Merge several data.frames into one data.frame with a loop
- Fast pairwise simple linear regression between variables in a data frame
- Append an object to a list in R in amortized constant time, O(1)?
- Remove/collapse consecutive duplicate values in sequence
- How can I remove an element from a list?
- How to Loop/Repeat a Linear Regression in R
- Merge by Range in R – Applying Loops
- Comparing two vectors in an if statement
- Cumulative sum until maximum reached, then repeat from zero in the next row
- Sort a data.table fast by Ascending/Descending order
- Faster weighted sampling without replacement
- Getting the last n elements of a vector. Is there a better way than using the length() function?
- Get row and column indices of matches using `which()`
- Efficiently computing a linear combination of data.table columns
- Looping through t.tests for data frame subsets in r
- R assigning ggplot objects to list in loop
- What methods can we use to reshape VERY large data sets?
- Breaking loop when “warnings()” appear in R
- Keyed lookup on data.table without ‘with’
- How to load data quickly into R?
- Count number of non-NA values by group
- How to read specific rows of CSV file with fread function
- Efficient string similarity grouping
- How to use data.table within functions and loops?