简体   繁体   中英

Cumulative count within group using dplyr

I am trying to create a column that contains a cumulative count of another column.

My data:

df <- data.frame(brand = c("A","B","C","A","A","B","A","A","B","C"))

And this is my expected output:

    |Brand |  Count  |
    |:-----|--------:|
    |A     |        1|
    |B     |        1|
    |C     |        1|
    |A     |        2|
    |A     |        3|
    |B     |        2|
    |A     |        4|
    |A     |        5|
    |B     |        3|
    |C     |        2|

I have tried cumsum but it doesn't accept strings or factors:

df %>%
  group_by(Brand) %>%
  mutate(Count = cumsum(Brand))

Edit: For bonus points it would be great if the solution could be used on database tables also (SQL Server)

We can create the column with rowid of 'brand'

library(dplyr)
library(data.table)
 df %>%
    mutate(Count = rowid(brand))

Or use a row_number after grouping by 'brand'

df %>%
    group_by(brand) %>%
    mutate(Count = row_number())

Or using data.table

library(data.table)
setDT(df)[, Count := rowid(brand)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM