start_end_to_fill: Function for filling start and end gaps in time series

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/start_end_to_fill.R

Description

This function allows to fill the start and end gaps of a time series by doing repetition of next (for the start) and previous values (for the end)

Usage

1
start_end_to_fill(data, calendar, gap_variable, key_variable, time_variable)

Arguments

data

a R data frame

calendar

a R data frame containing a complete empty calendar (as one can performs with create_calendar_day)

gap_variable

a character. This represents the name of the variable we want to fill the start and end gaps

key_variable

a character. This represents the variable name that refers to the key variable in the panel data (an ID, ...)

time_variable

a character. This represents the time variable name that permits to sort observation on a time scale

Value

a R data frame that contains the original columns and a new one:

Author(s)

Simon CORDE

References

Link to the author's github package repository: https://github.com/Redcart/helda

See Also

create_calendar gap_to_fill

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
library(dplyr)

# We take three countries from 2011 to 2018
fr_sp_ge_pop <- world_countries_pop %>%
filter(country_name %in% c('France', 'Spain', 'Germany')) %>%
filter(year > 2010) %>%
arrange(country_name, year)

# We artificially create some gaps in time series
fr_sp_ge_pop$population[c(1, 5, 11, 12, 24)] <- NA
fr_sp_ge_pop <- na.omit(fr_sp_ge_pop)

data_1 <- create_calendar(data = fr_sp_ge_pop, key_variable = "country_code",
time_variable = "year", start_year = 2011, end_year = 2018)
data_2 <- start_end_to_fill(data = fr_sp_ge_pop, calendar = data_1, gap_variable = "population",
key_variable = "country_code", time_variable = "year")
data_3 <- gap_to_fill(data = data_2, gap_variable = "population_corrected_1",
key_variable = "country_code", time_variable = "year", digits = 1)

Example output

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

Warning message:
no DISPLAY variable so Tk is not available 

helda documentation built on Jan. 6, 2021, 5:09 p.m.