本文介绍了在R中按组添加索引(或计数器)到数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个df喜欢

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

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

I want to add a third column such that we have an incrementing counter for each ProjectID:

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

我看过 seq rank 以及其他几项,特别是要查看我是否可以使用 ddply 帮助:

I've had a look at seq rank and a couple of other bits particularly looking to see if I could use ddply to help:

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

我想我可以调整这个答案但更喜欢使用类似ddply的东西(我找不到相当于的cumsum但我认为这里的原理相同:)。这让我在列表中对事件进行索引(例如合并)。

I think I could adapt this answer 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 ). That'd let me index occurrences in a list (and e.g. merge on this).

推荐答案

A dplyr 解决方案非常简单:

A dplyr solution is quite simple:

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

这篇关于在R中按组添加索引(或计数器)到数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:19