Update: From v1.8.3
, you can simply use the inbuilt special .GRP
:
DT[ , i := .GRP, by = key(DT)]
See history for older answers.
More Related Contents:
- How to create a lag variable within each group?
- data.table objects assigned with := from within function not printed
- How to efficiently calculate distance between pair of coordinates using data.table :=
- Sample random rows within each group in a data.table
- Faster way to read fixed-width files
- Select subset of columns in data.table R [duplicate]
- Melt using patterns when variable names contain string information – avoid coercion to numeric
- Error: package or namespace load failed for ggplot2 and for data.table
- How to perform join over date ranges using data.table?
- Split text string in a data.table columns
- R: data.table cross-join not working
- Why is allow.cartesian required at times when when joining data.tables with duplicate keys?
- Update subset of data.table based on join
- Dynamically build call for lookup multiple columns
- Add a row by reference at the end of a data.table object
- knitr gets tricked by data.table `:=` assignment
- Add multiple columns to R data.table in one function call?
- Subsetting data.table using variables with same name as column
- data.table equivalent of tidyr::complete()
- data.table merge based on date ranges
- Reason behind speed of fread in data.table package in R
- best way to transpose data.table
- data.table row-wise sum, mean, min, max like dplyr?
- Select NA in a data.table in R
- How to select R data.table rows based on substring match (a la SQL like)
- Any way to force fread() of data.table not to stop on empty lines?
- R data table: update join
- Split a string column into several dummy variables
- Is there a _fast_ way to run a rolling regression inside data.table?
- R fast single item lookup from list vs data.table vs hash