簡體   English   中英

計算連續值的延伸

[英]Calculating stretches of consecutive values

我有一個有兩個感興趣的列的df:日期和質量。 日期是每日時間序列。 質量有三種選擇 - Good,Estimated,Missing。 其中一個選項與給定日期相關聯。

我想檢索兩條信息:(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

所以在這里我們為Good我們將有一個連續的3,2,1列表,我想將1900-01-01的日期列表返回到1900-01-03,1900-01-05到1900-01-06和1900-01-08相關的3,2,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)
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)]

一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM