为列的连续值创建一个标志

时间:2019-05-07 14:42:10

标签: r

我正在尝试使用R来获取按品牌,区域,日期分组的数量列中连续的零的数量计数。我如何获得计数?

我已经尝试过rle,rleid函数,但是无法解决问题

实际数据示例:

structure(list(Brand = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("B1", "B2"), class = "factor"), 
        Area = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
        2L, 2L, 2L, 2L, 2L), .Label = c("A1", "A2"), class = "factor"), 
        Date = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 8L, 1L, 2L, 3L, 
        4L, 5L, 6L, 7L, 8L), .Label = c("4/1/2019", "4/2/2019", "4/3/2019", 
        "4/4/2019", "4/5/2019", "4/6/2019", "4/7/2019", "4/8/2019"
        ), class = "factor"), QTY = c(345L, 3455L, 34L, 23L, 0L, 
        0L, 677L, 883L, 383L, 43L, 3L, 4L, 0L, 0L, 0L)), .Names = c("Brand", 
    "Area", "Date", "QTY"), row.names = c(NA, -15L), class = c("data.table", 
    "data.frame"))

所需的输出 2

谢谢

1 个答案:

答案 0 :(得分:0)

获得所需输出的简单方法是:

require(dplyr)
data %>% group_by(Brand, Area) %>%
  mutate(flag=ifelse(QTY==0 & (lag(QTY)==0 | lead(QTY)==0),1,0),
    zcount=ifelse(QTY==0,sum(flag),NA))

结果:

# A tibble: 15 x 6
# Groups:   Brand, Area [2]
   Brand Area  Date       QTY  flag zcount
   <fct> <fct> <fct>    <int> <dbl>  <dbl>
 1 B1    A1    4/1/2019   345     0     NA
 2 B1    A1    4/2/2019  3455     0     NA
 3 B1    A1    4/3/2019    34     0     NA
 4 B1    A1    4/4/2019    23     0     NA
 5 B1    A1    4/5/2019     0     1      2
 6 B1    A1    4/6/2019     0     1      2
 7 B1    A1    4/8/2019   677     0     NA
 8 B2    A2    4/1/2019   883     0     NA
 9 B2    A2    4/2/2019   383     0     NA
10 B2    A2    4/3/2019    43     0     NA
11 B2    A2    4/4/2019     3     0     NA
12 B2    A2    4/5/2019     4     0     NA
13 B2    A2    4/6/2019     0     1      3
14 B2    A2    4/7/2019     0     1      3
15 B2    A2    4/8/2019     0     1      3