Splite from: How to check missing days in datasets in R
I have one more question.
There are missing days intermittently. How to find the longest consecutive time length in the whole time period of this data frame? Thanks.
Here's a reprex setting up the format of the data used,
library(dplyr)
library(lubridate)
library(tsibble)
df.xlsx1 <- data.frame(
Year = c(1985, 1985, 1985, 1985, 1985),
Month = c(1, 1, 1, 1, 1),
Day = c(1, 2, 5, 6, 7),
Value = c(10, 12, 11.8, 15, 21)
)
1 Like
In case you can't manage to make it work, this would be a manual way of subsetting the longest continuous time period
library(tidyverse)
library(lubridate)
library(tsibble)
df.xlsx1 <- data.frame(
Year = c(1985, 1985, 1985, 1985, 1985),
Month = c(1, 1, 1, 1, 1),
Day = c(1, 2, 5, 6, 7),
Value = c(10, 12, 11.8, 15, 21)
)
df.xlsx1 %>%
mutate(Date = make_date(year = Year, month = Month, day = Day)) %>%
as_tsibble(index = Date) %>%
fill_gaps() %>%
mutate(group = if_else(!is.na(Value) & is.na(lead(Value)), row_number(), NA_integer_)) %>%
filter(!is.na(Value)) %>%
fill(group, .direction = "up") %>%
group_by(group) %>%
add_count() %>%
ungroup() %>%
filter(n == max(n)) %>%
select(-group, -n)
#> # A tibble: 3 x 5
#> Year Month Day Value Date
#> <dbl> <dbl> <dbl> <dbl> <date>
#> 1 1985 1 5 11.8 1985-01-05
#> 2 1985 1 6 15 1985-01-06
#> 3 1985 1 7 21 1985-01-07
system
Closed
December 16, 2019, 5:42pm
3
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.