id_date_sequence: Identify date sequences

View source: R/misc.R

id_date_sequenceR Documentation

Identify date sequences

Description

For correctly plotting country-time period spells

Usage

id_date_sequence(x, pd = NULL)

Arguments

x

a Date sequence

pd

what is the time aggregation period in the data?

Examples

library("ggplot2")
d1 <- as.Date("2018-01-01")
d2 <- as.Date("2025-01-01")
seq1 <- seq(d1, d2, by = "year")
data.frame(seq1, id=id_date_sequence(seq1, "year"))
# With a gap, should be two ids
df <- data.frame(date = seq1[-4], id=id_date_sequence(seq1[-4], "year"), cowcode = 999)
df

# The point is to plot countries with interrupted independence correctly:
df$y <- c(rep(1, 3), rep(2, 4))
df$id <- paste0(df$cowcode, df$id)
df
ggplot(df, aes(x = date, y = y, group = cowcode)) + geom_line()
ggplot(df, aes(x = date, y = y, group = id)) + geom_line()

# Shortcut for integer years:
yr <- c(2002:2005, 2007:2010)
data.frame(year = yr, id = id_date_sequence(yr))

andybega/states documentation built on Sept. 11, 2023, 4:04 p.m.