Skip to contents

Function to give the 'yyyy' string to a date column (of formats date, int and character)

Usage

year_date(data, date_col, type = c("earliest", "latest"))

Arguments

data

Dataset

date_col

Date, the date column to find the latest/earliest. If this an integer (sk synthetic key) converts to date format

type

String, latest gives the most recent year in the column whilst earliest gives the first

Value

Examples

test <- data.frame(
 date = sample(seq(as.Date('2007/01/01'), as.Date('2021/01/01'), by = "day"), 12)) %>%
 dplyr::mutate(date_sk = as.integer(format(as.Date(date), "%Y%m%d"))) %>%
 dplyr::mutate(charc_date = as.character(date))

year_date(test, date, type = "latest")
#>   year
#> 1 2020
year_date(test, date, type = "earliest")
#>   year
#> 1 2009

year_date(test, date_sk, type = "latest")
#>   year
#> 1 2020
year_date(test, date_sk, type = "earliest")
#>   year
#> 1 2009

year_date(test, charc_date, type = "latest")
#>   year
#> 1 2020
year_date(test, charc_date, type = "earliest")
#>   year
#> 1 2009