R:在单个列中按组统计值的连续出现

时间:2019-02-07 20:05:50

标签: r count sequence

我正在尝试创建一个连续的相等值,包括出现次数。但是,即使行保持连续,我也希望一旦引入新的ID后就可以重置计数。

我的数据示例如下:

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)

所以我想用以下结果创建一个新列:

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

我使用了在该论坛上找到的以下代码:

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

但是,这不会重置新ID号的计数。取而代之的是连续计数,结果输出为:

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

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

谢谢!

3 个答案:

答案 0 :(得分:0)

您可以这样做:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@1.3.0/dist/vuetify.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@1.3.0/dist/vuetify.min.css">
<div id="app" class="content" data-app>
  <v-container class="test-container" grid-list-md text-xs-center fluid>
    <v-layout row wrap>
      <v-flex xl12>
        <v-card dark color="primary">
          <v-card-text class="px-0">Test</v-card-text>
        </v-card>
      </v-flex>
    </v-layout>

    <v-layout row justify-space-around>
      <v-flex xs4>
        <v-card dark color="primary">
          <v-card-text>Test</v-card-text>
        </v-card>
      </v-flex>
      <v-flex xs4>
        <v-card dark color="accent">
          <v-card-text>Test</v-card-text>
        </v-card>
      </v-flex>
    </v-layout>

    <v-layout row justify-space-between>
      <v-flex xs4>
        <v-card dark color="primary">
          <v-card-text>Test</v-card-text>
        </v-card>
      </v-flex>
      <v-flex xs4>
        <v-card dark color="accent">
          <v-card-text>Test</v-card-text>
        </v-card>
      </v-flex>
    </v-layout>

    <v-layout row wrap>
      <v-flex xl12>
        <v-text-field box label="Test" type="text"></v-text-field>
      </v-flex>
    </v-layout>
  </v-container>
</div>

输出:

dataset$Counter <- with(dataset,
                        ave(YesNO, ID, FUN = function(x) sequence(rle(as.character(x))$lengths)))

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

还有一种dplyr可能性:

dataset %>%
 group_by(ID, grp = {grp = rle(YesNO); rep(seq_along(grp$lengths), grp$lengths)}) %>%
 mutate(Counter = seq_along(grp)) %>%
 ungroup() %>%
 select(-grp)

   ID    YesNO Counter
   <fct> <dbl>   <int>
 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