查找R中条纹的第一个和最后一个日期

时间:2018-03-31 08:30:42

标签: r date dataframe

这是我的数据框:

date <- as.Date(c("1993-09-21", "1994-02-12", "1994-02-23", "1994-05-14", "1994-08-18", "1994-08-25", "1994-08-29", "1994-09-17", "1994-10-16", "1994-10-16", "1994-10-22", "1994-10-26", "1994-12-26", "1995-04-12", "1995-05-04", "1995-06-20", "1995-07-11", "1995-07-27", "1995-08-14", "1995-08-15", "1995-08-22", "1995-08-27", "1995-08-27", "1995-08-28", "1995-08-30", "1995-08-30", "1995-09-03", "1995-09-03", "1995-09-03", "1995-09-15"))

value <- c(2, 1, 1, 1, 2, 1, 2, 4, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 2, 1, 1)

df <- data.frame(date, value)

df$value.equals.1 <- df$value == 1

我需要两件事:(1)每个连续条纹的第一个和最后一个日期值为1(2)每个连续条纹的长度为1。

我已根据需要对数据框进行了注释。我怎样才能在R?

中实现这一目标

enter image description here

1 个答案:

答案 0 :(得分:1)

我们可以使用rleid中的data.table执行此操作。使用rleid在&#39; value.equals.1&#39;上创建分组变量,将&#39; date&#39;基于&#39; value.equals.1&#39;并提取第一个和最后一个日期&#39;按&#39; grp&#39;

分组
library(data.table)
setDT(df)[, date[value.equals.1], .(grp  = rleid(value.equals.1))
      ][, .(date  = c(V1[1], V1[.N]), n = .N), by = grp][, grp := NULL][]
#          date n
# 1: 1994-02-12 3
# 2: 1994-05-14 3
# 3: 1994-08-25 1
# 4: 1994-08-25 1
# 5: 1994-10-22 1
# 6: 1994-10-22 1
# 7: 1994-12-26 8
# 8: 1995-08-15 8
# 9: 1995-08-27 3
#10: 1995-08-30 3
#11: 1995-09-03 2
#12: 1995-09-15 2

或者可以使用tidyverse

完成此操作
library(dplyr)
df %>%
   group_by(grp = rleid(value.equals.1)) %>%
   filter(all(value.equals.1)) %>%
   mutate(n = n()) %>%
   slice(c(1, n())) %>%
   ungroup %>% 
   select(date, n)
# A tibble: 12 x 2
#   date           n
#   <date>     <int>
# 1 1994-02-12     3
# 2 1994-05-14     3
# 3 1994-08-25     1
# 4 1994-08-25     1
# 5 1994-10-22     1
# 6 1994-10-22     1
# 7 1994-12-26     8
# 8 1995-08-15     8
# 9 1995-08-27     3
#10 1995-08-30     3
#11 1995-09-03     2
#12 1995-09-15     2

或者使用rle中的base R来创建群组

grp <- inverse.rle(within.list(rle(df$value.equals.1), values <- seq_along(values)))
do.call(c, lapply(with(df, split(date[value.equals.1], 
        grp[value.equals.1])), function(x) c(x[1], x[length(x)])))