简体   繁体   中英

Calculating stretches of consecutive values

I have a df with two columns of interest: Date and Quality. Date is a daily time series. There are three options for quality - Good, Estimated, Missing. With one of these options being associated with a given date.

I would like to retrieve two pieces of information: (1) is a list of consecutive stretches an option has over the time series; and (2) the dates associated with those consecutive records.

For example,

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

So here we for Good we would have a consecutive list of 3,2,1 and I would like to return a date list of 1900-01-01 to 1900-01-03, 1900-01-05 to 1900-01-06 and 1900-01-08 associated with the 3,2,1 list.

You can use rle

Below sections shows the consecutive lengths for Good

encodes <- rle(df$Quality)
encodes$lengths[encodes$values == "Good"]
[1] 3 2 1

Getting the dates can be done directly from the df

Data:

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

Data used

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)]

One dplyr possibility could be:

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

Or:

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

Or:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM