计算连续值的拉伸

时间:2019-05-16 17:12:37

标签: r

我有一个感兴趣的DF,其中包含两列:日期和质量。日期是每日时间序列。质量有三个选项-良好,估计,缺失。这些选项之一与给定日期相关联。

我想检索两条信息:(1)是一个选项在时间序列上具有的连续拉伸的列表;和(2)与这些连续记录相关的日期。

例如,

1900-01-01  Good
1900-01-02  Good
1900-01-03  Good
1900-01-04  Estimated
1900-01-05  Good
1900-01-06  Good
1900-01-07  Estimated
1900-01-08  Good

因此,在这里,我们永远有一个连续的3,2,1列表,我想将日期列表从1900-01-01返回到1900-01-03,从1900-01-05返回到1900- 01-06和1900-01-08与3,2,1列表相关。

3 个答案:

答案 0 :(得分:1)

您可以使用rle

以下部分显示了Good

的连续长度
encodes <- rle(df$Quality)
encodes$lengths[encodes$values == "Good"]
[1] 3 2 1

可以直接从df

获取日期

数据:

df <- read.table(text = "Date Quality
1900-01-01  Good
1900-01-02  Good
                 1900-01-03  Good
                 1900-01-04  Estimated
                 1900-01-05  Good
                 1900-01-06  Good
                 1900-01-07  Estimated
                 1900-01-08  Good", header = T, stringsAsFactors = F)

答案 1 :(得分:1)

library(data.table)
setDT(df)

out <- 
  df[order(Date), .(start = Date[1], end = Date[.N], .N), 
     by = .(Quality, id = rleid(Quality))][, -'id']

out[Quality == 'Good']
#    Quality      start        end N
# 1:    Good 1900-01-01 1900-01-03 3
# 2:    Good 1900-01-05 1900-01-06 2
# 3:    Good 1900-01-08 1900-01-08 1

使用的数据

df <- fread('
Date  Quality
1900-01-01  Good
1900-01-02  Good
1900-01-03  Good
1900-01-04  Estimated
1900-01-05  Good
1900-01-06  Good
1900-01-07  Estimated
1900-01-08  Good
')

df[, Date := as.Date(Date)]

答案 2 :(得分:1)

一种dplyr可能是:

df %>%
 mutate(rleid = with(rle(V2), rep(seq_along(lengths), lengths)),
        V1 = as.Date(V1, format = "%Y-%m-%d")) %>%
 group_by(rleid, V2) %>%
 summarise(res = paste0(min(V1), ":", max(V1)))

  rleid V2        res                  
  <int> <chr>     <chr>                
1     1 Good      1900-01-01:1900-01-03
2     2 Estimated 1900-01-04:1900-01-04
3     3 Good      1900-01-05:1900-01-06
4     4 Estimated 1900-01-07:1900-01-07
5     5 Good      1900-01-08:1900-01-08

或者:

df %>%
 mutate(rleid = with(rle(V2), rep(seq_along(lengths), lengths)),
        V1 = as.Date(V1, format = "%Y-%m-%d")) %>%
 group_by(rleid, V2) %>%
 summarise(res = paste0(min(V1), ":", max(V1))) %>%
 group_by(V2) %>%
 mutate(rleid = seq_along(rleid)) %>%
 arrange(V2, rleid)

  rleid V2        res                  
  <int> <chr>     <chr>                
1     1 Estimated 1900-01-04:1900-01-04
2     2 Estimated 1900-01-07:1900-01-07
3     1 Good      1900-01-01:1900-01-03
4     2 Good      1900-01-05:1900-01-06
5     3 Good      1900-01-08:1900-01-08

或者:

df %>%
 mutate(rleid = with(rle(V2), rep(seq_along(lengths), lengths)),
        V1 = as.Date(V1, format = "%Y-%m-%d")) %>%
 group_by(rleid, V2) %>%
 summarise(res = paste0(min(V1), ":", max(V1)),
           n = n()) %>%
 group_by(V2) %>%
 mutate(rleid = seq_along(rleid)) %>%
 arrange(V2, rleid)

  rleid V2        res                       n
  <int> <chr>     <chr>                 <int>
1     1 Estimated 1900-01-04:1900-01-04     1
2     2 Estimated 1900-01-07:1900-01-07     1
3     1 Good      1900-01-01:1900-01-03     3
4     2 Good      1900-01-05:1900-01-06     2
5     3 Good      1900-01-08:1900-01-08     1