Conditionally convert x to yearmon if the conversion is unique, retaining x as names.
as.yearmon2.Rd
Convert x
to class "yearmon"
. If duplicate months are
found, return x
. Otherwise, return the conversion with names =
x
.
Arguments
- x
object suitable for
as.yearmon
- ...
additional argument(s) (e.g., a format) passed to
as.yearmon
.
Details
Dates for some monthly data include the day of the month on which the
data were published. For many purposes, one would like to have the
data as a zoo
object with a yearmon
index, while still
retaining the full date for other purposes.
If the yearmon
form of the input is not unique,
as.yearmon2
returns the input unchanged with a warning.
Otherwise, it returns the yearmon
conversion with the input as
names.
Examples
x1 <- as.Date(c("2000-01-01", "2000-01-01"))
as.yearmon2(x1)
#> Warning: 1 duplicate months found in 'x'; returning 'x' unchanged
#> [1] "2000-01-01" "2000-01-01"
#Warning message:
#In as.yearmon2(x1) :
# 1 duplicate months found in 'x'; returning 'x' unchanged
x2 <- as.Date(c("2000-01-01", "2000-02-01"))
as.yearmon2(x2)
#> 2000-01-01 2000-02-01
#> "Jan 2000" "Feb 2000"
# month of x2 with names x2