How to find time-series loopholes?

Asked

Viewed 24 times

2

I have several time series with gaps. I mean, day has observation, spends a few days without, then back to observe and so continues with holes. The problem is that they put these series together in the archive. If a month of daily data is missing, the company that filled in did not leave the days unobserved with NA or -9.9e10. The company joined. That is, in an excel spreadsheet it is as if in the next row the line with the observation for 03/02/1995 had the observation of 03/10/1995. That is, an 8-day scoop.

I then managed a sequence of dates from a base year (1900) through 2018 and wanted to find the gaps in the series and link it to this sequence of dates on a date.frame.

I’m using the function full_join() of dplyr, but you’re coming out all wrong.

I wanted this union to be based on the sequence of dates I created, neither more nor less. In order to have a regular data frame at the end.

Some method to do this?

1 answer

3


It seems to me that the Join that solves your problem is the left_join. See below:

library(dplyr)
library(lubridate)

# criacao dos dias de referencia

dias <- seq.Date(from = dmy("01-01-2001"), 
                 to = dmy("10-01-2001"), 
                 by = "1 day")

# data frame de exemplo

dados <- data.frame(dias, observacoes = 1:10)

# retirando algunas dados dele

missing <- dados[-c(2, 3, 7), ]

missing
#>          dias observacoes
#> 1  2001-01-01           1
#> 4  2001-01-04           4
#> 5  2001-01-05           5
#> 6  2001-01-06           6
#> 8  2001-01-08           8
#> 9  2001-01-09           9
#> 10 2001-01-10          10

# completando o data frame

left_join(as.data.frame(dias), missing)
#> Joining, by = "dias"
#>          dias observacoes
#> 1  2001-01-01           1
#> 2  2001-01-02          NA
#> 3  2001-01-03          NA
#> 4  2001-01-04           4
#> 5  2001-01-05           5
#> 6  2001-01-06           6
#> 7  2001-01-07          NA
#> 8  2001-01-08           8
#> 9  2001-01-09           9
#> 10 2001-01-10          10

Created on 2020-04-24 by the reprex package (v0.3.0)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.