Stable lifecycle

Create or coerce using yearmonth().

yearmonth(x)

is_yearmonth(x)

Arguments

x

Other object.

Value

year-month (yearmonth) objects.

See also

Other index functions: yearquarter(), yearweek()

Examples

# coerce POSIXct/Dates to yearmonth x <- seq(as.Date("2016-01-01"), as.Date("2016-12-31"), by = "1 month") yearmonth(x)
#> <yearmonth[12]> #> [1] "2016 Jan" "2016 Feb" "2016 Mar" "2016 Apr" "2016 May" "2016 Jun" #> [7] "2016 Jul" "2016 Aug" "2016 Sep" "2016 Oct" "2016 Nov" "2016 Dec"
# parse characters yearmonth(c("2018 Jan", "2018-01", "2018 January"))
#> <yearmonth[3]> #> [1] "2018 Jan" "2018 Jan" "2018 Jan"
# creat an empty yearmonth container yearmonth()
#> <yearmonth[0]>
# seq() and arithmetic mth <- yearmonth("2017-11") seq(mth, length.out = 10, by = 1) # by 1 month
#> <yearmonth[10]> #> [1] "2017 Nov" "2017 Dec" "2018 Jan" "2018 Feb" "2018 Mar" "2018 Apr" #> [7] "2018 May" "2018 Jun" "2018 Jul" "2018 Aug"
mth + 0:9
#> <yearmonth[10]> #> [1] "2017 Nov" "2017 Dec" "2018 Jan" "2018 Feb" "2018 Mar" "2018 Apr" #> [7] "2018 May" "2018 Jun" "2018 Jul" "2018 Aug"
# display formats format(mth, format = "%y %m")
#> [1] "17 11"
# units since 1970 Jan as.double(yearmonth("1969 Jan") + 0:24)
#> [1] -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 #> [20] 7 8 9 10 11 12