count number of observations in grouping

Scott Prevost

2019/03/25

Create a column that has the number of observations (count of rows) when performing a group with by.


dt %<>%
  .[, .(col1 = sum(col2, na.rm = TRUE),
        counter = .N),
    by = .(grp_col)]

See this Stack Overflow question