Coerce an object to a parttime object
Arguments
- x
an object for coersion
- ...
Additional arguments passed to
format
when a function is provided.- format
a
function
orcharacter
value. If afunction
, it should accept a character vector and return a matrix of parttime components. If acharacter
it should provide a regular exprssion which contains capture groups for each of the parttime components. See parse_to_parttime_matrix'sregex
parameter for more details.- on.na
a
function
used to signal a condition for newNA
values introduced by coercion, acharacter
value among"error"
,"warning"
or"suppress"
(for silencing messages) orNULL
equivalent to"suppress"
.
Value
parttime
vector. See the Details section of parttime for
further information.
Examples
as.parttime(c("1985-10-18", "1991-08-23", "1996-09-26"))
#> <partial_time<YMDhms+tz>[3]>
#> [1] "1985-10-18" "1991-08-23" "1996-09-26"
# <partial_time<YMDhmsZ>[3]>
# [1] "1985-10-18" "1991-08-23" "1996-09-26"
as.parttime(c("1234", "5678"), format = "(?<year>\\d{4})")
#> <partial_time<YMDhms+tz>[2]>
#> [1] "1234" "5678"
# <partial_time<YMDhmsZ>[2]>
# [1] "1234" "5678"
# format function that returns a matrix of components
utf8_str <- function(x) intToUtf8(utf8ToInt(x) - 16)
as.parttime(c("B@", "B@A@"), format = function(x) cbind(year = sapply(x, utf8_str)))
#> <partial_time<YMDhms+tz>[2]>
#> [1] "0020" "2010"
# <partial_time<YMDhmsZ>[2]>
# [1] "2000" "2010"
# format function that returns a parttime object by first pre-processing input
as.parttime("B@BB", format = function(x) as.parttime(utf8_str(x)))
#> <partial_time<YMDhms+tz>[1]>
#> [1] "2022"
# <partial_time<YMDhmsZ>[1]>
# [1] "2022"
# format function that returns a parttime object by manual construction
as.parttime("AIII", format = function(x) parttime(year = as.numeric(utf8_str(x))))
#> <partial_time<YMDhms+tz>[1]>
#> [1] "1999"
# <partial_time<YMDhmsZ>[1]>
# [1] "1999"