在 R 中按组向数据帧添加索引(或计数器)

Add an index (or counter) to a dataframe by group in R

我有一个df喜欢

ProjectID Dist
  1        x
  1        y
  2        z
  2        x
  2        h
  3        k
  ....     ....

我想添加第三列,以便每个项目 ID 都有一个递增计数器:

ProjectID Dist counter
  1        x     1
  1        y     2
  2        z     1
  2        x     2
  2        h     3
  1        k     3
  ....     ....

我查看了 seq rank 和其他一些内容,特别是想看看我是否可以使用 ddply 来提供帮助:

df$counter <- ddply(df,.(projectID), function(x).....? )

我想我可以调整这个答案 How to create a counter/numeration by group? but would prefer something using something like ddply (I can't find an equivalent of cumsum but I think that's the same principle here: Create ascending series of integers by group in Pandas)。那会让我在列表中索引出现(例如合并)。

一个dplyr解决方案很简单:

library(dplyr)

df %>% group_by(ProjectID) %>% mutate(counter = row_number(ProjectID))


#  ProjectID Dist counter
#1         1    x       1
#2         1    y       2
#3         2    z       1
#4         2    x       2
#5         2    h       3
#6         1    k       3