complete_time: similar to expand grid - but in expand.grid the algo does not...

View source: R/complete_time.R

complete_timeR Documentation

similar to expand grid - but in expand.grid the algo does not infer missing dates from the timeseries our function does !!

Description

similar to expand grid - but in expand.grid the algo does not infer missing dates from the timeseries our function does !!

Usage

complete_time(df)

complete_time_factors(df)

Arguments

df

The dataframe or tibble to operate on

Value

An expanded data.frame of all time, and optionally all factor permutations

Functions

  • complete_time_factors: Apply every combination of variable factors for complete time series.

Examples


require(ggplot2)
require(tibble)
require(dplyr)

# time series
 ts <-  as.Date("2022-01-03"):as.Date(Sys.Date())
 ts <- sort(
   as.Date(
     ts[sample(c(TRUE,FALSE),size = 101,replace = TRUE,prob = c(0.7,0.3))],
     origin='1970-01-01')
   )

 df <- tibble::tibble(time=as.Date(ts),
              col1=sample(replace=TRUE,letters[c(1:5)],size=length(ts)),
              col2=sample(replace=TRUE,1:26,size=length(ts)),
              )

 time_name <- sapply(df,class)[sapply(df,class)=='Date']%>%names()
 
 new_df <- complete_time_factors(df)

 
#similar to expand grid - but in expand.grid the 
#algo does not infer missing dates from the timeseries
# our function does !!
 
 df%>%
   dplyr::count(time,wt=col2)%>%
   dplyr::mutate(n-lag(n,1))%>%head(10)
 
 new_df%>%
   dplyr::count(time,wt=col2)%>%
   dplyr::mutate(n-lag(n,1))%>%head(10)
 
 #compare BEFORE and
 
 ggplot2::ggplot(df)+
 ggplot2::geom_line(ggplot2::aes(time,col2,col=col1))+
 ggplot2::facet_wrap(~col1)+
 ggplot2::theme_minimal()
 
 #... and after
 
 ggplot2::ggplot(new_df)+
 ggplot2::geom_line(ggplot2::aes(time,col2,col=col1))+
 ggplot2::facet_wrap(~col1)+
 ggplot2::theme_minimal()



aarong1/RshinyHelpers documentation built on June 15, 2022, 4:52 a.m.