R:在单个列中按组统计连续出现的值 [英] R: count consecutive occurrences of values in a single column and by group

查看:83
本文介绍了R:在单个列中按组统计连续出现的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建相等数量的连续数,即出现次数。但是,即使行保持连续,我也希望在引入新ID后重新设置计数。

I am trying to create a sequential number of equal values, a count of occurrences. However, I want the count to reset once a new ID is introduced even if the the row remains sequential.

我的数据示例如下:

dataset <- data.frame(ID = 
c("a","a","a","a","a","a","a","b","b","b","b","b","b","b")
dataset$YesNO <- c(1,1,0,0,0,1,1,1,1,1,0,0,0,0)

所以我想创建一个新列,其结果如下:

So I want to create a new column with the results in:

c(1,2,1,2,3,1,2,1,2,3,1,2,3,4)

我使用了在该论坛上找到的这段代码:

I've used this code that I've found on this forum:

dataset$Counter <- sequence(rle(as.character(dataset$YesNo))$lengths)

但是,这没有t重置新ID号的计数。相反,连续计数继续,结果输出为:

However, this doesn't reset the count for the new ID number. Instead the sequential count continues and the resulting output is:

c(1,2,1,2,3,1,2,3,4,5,1,2,3,4)

我缺少根据ID进行重置的步骤。

What step am I missing to have it reset based on the ID.

推荐答案

使用 rleid (来自data.table包) )以获取分组变量,然后使用 ave 在该分组的通用值内应用 seq_along

Use rleid (from the data.table package) to get a grouping variable and then use ave to apply seq_along within common values of that grouping:

library(data.table)
transform(dataset, Counter = ave(YesNO, rleid(ID, YesNO), FUN = seq_along))

给予:

   ID YesNO Counter
1   a     1       1
2   a     1       2
3   a     0       1
4   a     0       2
5   a     0       3
6   a     1       1
7   a     1       2
8   b     1       1
9   b     1       2
10  b     1       3
11  b     0       1
12  b     0       2
13  b     0       3
14  b     0       4

这篇关于R:在单个列中按组统计连续出现的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆