24

我希望在每次相等值的运行中创建一个序列号,例如出现计数器,一旦当前行中的值与前一行不同,它就会重新启动。

请在下面找到输入和预期输出的示例。

dataset <- data.frame(input = c("a","b","b","a","a","c","a","a","a","a","b","c"))
dataset$counter <- c(1,1,2,1,2,1,1,2,3,4,1,1)
dataset

#    input counter
# 1      a       1
# 2      b       1
# 3      b       2
# 4      a       1
# 5      a       2
# 6      c       1
# 7      a       1
# 8      a       2
# 9      a       3
# 10     a       4
# 11     b       1
# 12     c       1

我的问题与这个问题非常相似:Cumulative sequence of occurrences of values

4

3 回答 3

45

您需要使用sequencerle

> sequence(rle(as.character(dataset$input))$lengths)
 [1] 1 1 2 1 2 1 1 2 3 4 1 1
于 2013-11-15T10:27:03.903 回答
25

从 v1.9.8(新闻第 16 条)开始,使用rowidwithrleid

dataset[, counter := rowid(rleid(input))]

计时码:

set.seed(1L)
library(data.table)
DT <- data.table(input=sample(letters, 1e6, TRUE))
DT1 <- copy(DT)

bench::mark(DT[, counter := seq_len(.N), by=rleid(input)], 
    DT1[, counter := rowid(rleid(input))])

时间:

  expression                                              min  median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time
  <bch:expr>                                          <bch:t> <bch:t>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm>
1 DT[, `:=`(counter, seq_len(.N)), by = rleid(input)] 613.8ms 613.8ms      1.63    18.8MB     8.15     1     5      614ms
2 DT1[, `:=`(counter, rowid(rleid(input)))]            60.5ms  71.4ms     12.7     26.4MB    14.5      7     8      553ms

现在在 data.table 包中提供了下面编写的函数的一个有效且更直接的版本,称为rleid. 使用它,它只是:

setDT(dataset)[, counter := seq_len(.N), by=rleid(input)]

有关?rleid用法和示例的更多信息,请参阅。感谢@Henrik 提出更新这篇文章的建议。


rle绝对是最方便的方法(+1 @Ananda's)。但是在更大的数据上可以做得更好(在速度方面)。您可以使用duplistvecseq函数(未导出),data.table如下所示:

require(data.table)
arun <- function(y) {
    w = data.table:::duplist(list(y))
    w = c(diff(w), length(y)-tail(w,1L)+1L)
    data.table:::vecseq(rep(1L, length(w)), w, length(y))
}

x <- c("a","b","b","a","a","c","a","a","a","a","b","c")
arun(x)
# [1] 1 1 2 1 2 1 1 2 3 4 1 1

大数据基准测试:

set.seed(1)
x <- sample(letters, 1e6, TRUE)
# rle solution
ananda <- function(y) {
    sequence(rle(y)$lengths)
}

require(microbenchmark)
microbenchmark(a1 <- arun(x), a2<-ananda(x), times=100)
Unit: milliseconds
            expr       min        lq    median       uq       max neval
   a1 <- arun(x)  123.2827  132.6777  163.3844  185.439  563.5825   100
 a2 <- ananda(x) 1382.1752 1899.2517 2066.4185 2247.233 3764.0040   100

identical(a1, a2) # [1] TRUE
于 2013-11-15T10:48:59.507 回答
4

运行器有专门的解决方案来计算需要什么。streak_run是最快的解决方案并接受向量作为输入。

library(microbenchmark)
library(runner)

x      <- sample(letters, 1e6, TRUE)
ananda <- function(y) sequence(rle(y)$lengths)

microbenchmark(
  a2 <- ananda(x), 
  runner <- streak_run(x), 
  times=100
)

#Unit: milliseconds
#                expr     min      lq     mean  median       uq      max neval
#     a2 <- ananda(x) 580.744 718.117 1059.676 944.073 1399.649 1699.293    10
#run <- streak_run(x)  37.682  39.568   42.277  40.591   43.947   52.917    10

identical(a2, run)
#[1] TRUE
于 2018-09-18T10:31:50.363 回答