to() converts x to the type of .to, dispatching on the class of .to
to the appropriate to_*() function.
Usage
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'character'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'double'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'Date'
to(
x,
.to,
...,
tz = resolve_tz(.to),
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'data.frame'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'factor'
to(
x,
.to,
...,
levels = NULL,
ordered = is.ordered(x) || is.ordered(.to),
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class '`function`'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'integer'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'logical'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'list'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'array'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class '`NULL`'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'hms'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'Period'
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'POSIXct'
to(
x,
.to,
...,
tz = resolve_tz(.to),
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)
# Default S3 method
to(
x,
.to,
...,
x_arg = caller_arg(x),
to_arg = caller_arg(.to),
call = caller_env(),
x_class = object_type(x)
)Arguments
- x
The object to stabilize.
- .to
A prototype that determines the target type (e.g.,
integer(),factor(levels = c("a", "b"))).- ...
Arguments passed to methods and on to
to_*()functions.- x_arg
(
character(1)) The name of the object being stabilized to use in error messages. The automatic value will work in most cases, or pass it through from higher-level functions to make error messages clearer in unexported functions.- to_arg
(
character(1)orNULL) The name of the object thatxis being converted to match, for use in error messages. WhenNULL, error messages describe onlyx. When supplied, conversion failure messages name bothx_argandto_arg.- call
(environment)The execution environment to mention as the source of error messages.- x_class
(
character(1)) The class name of the object being stabilized to use in error messages. Use this if you remove a special class from the object before checking its conversion, but want the error message to match the original class.- tz
(
character(1)) The time zone to use when.tois a base::POSIXct or base::Date. Must be a value fromOlsonNames()or""(your local time zone). Defaults to the time zone of.to, or your local time zone if.todoesn't name one (seeresolve_tz()).- levels
(character)The desired factor levels. For factors, a vector'slevelsplay the same role thatallowed_valuesplays for other types: they restrictxto a fixed set of permitted values.- ordered
(
logical(1)) Should the result be an ordered factor?
Value
x converted to the type of .to, or an error condition with
classes <stbl-error>, <stbl-condition>, <rlang_error>, <error>,
<condition>, and a specific class by failure mode:
The failure classes documented for the
to_*()function corresponding to the class of.to(for exampleto_int()for.to = integer()).<stbl-error-coerce-*>when the class of.tohas no correspondingto_*()function andvctrs::vec_cast()cannot convertedxto.to.
Details
When the class of .to does not have a corresponding to_*() function,
to() falls back to vctrs::vec_cast(). Arguments in ... are passed to
to_*() functions but are ignored by the vctrs::vec_cast() fallback.
See also
Other character functions:
are_chr_ish(),
specify_chr(),
stabilize_chr(),
stabilize_chr_scalar(),
to_chr(),
to_chr_scalar()
Other double functions:
are_dbl_ish(),
specify_dbl(),
stabilize_dbl(),
stabilize_dbl_scalar(),
to_dbl(),
to_dbl_scalar()
Other integer functions:
are_int_ish(),
specify_int(),
stabilize_int(),
stabilize_int_scalar(),
to_int(),
to_int_scalar()
Other logical functions:
are_lgl_ish(),
specify_lgl(),
stabilize_lgl(),
stabilize_lgl_scalar(),
to_lgl(),
to_lgl_scalar()
Other factor functions:
are_fct_ish(),
specify_fct(),
stabilize_fct(),
stabilize_fct_scalar(),
to_fct(),
to_fct_scalar()
Other function functions:
are_fn_ish(),
to_fn()
Other list functions:
assert_present(),
specify_lst(),
stabilize_lst(),
to_lst()
Other data frame functions:
specify_df(),
stabilize_df(),
to_df()
Other date functions:
locale_datetime_formats(),
specify_date(),
stabilize_date(),
stabilize_date_scalar(),
to_date(),
to_date_scalar()
Other datetime functions:
locale_datetime_formats(),
resolve_tz(),
specify_dttm(),
stabilize_dttm(),
stabilize_dttm_scalar(),
to_dttm(),
to_dttm_scalar()
Other time functions:
specify_time(),
stabilize_time(),
stabilize_time_scalar(),
to_time(),
to_time_scalar()
Other duration functions:
specify_dur(),
stabilize_dur(),
stabilize_dur_scalar(),
to_dur(),
to_dur_scalar()
Examples
to(1L, double())
#> [1] 1
to(1.0, integer())
#> [1] 1
to(TRUE, character())
#> [1] "TRUE"
to("1", integer())
#> [1] 1
to(c("a", "b"), factor(levels = c("a", "b", "c")))
#> [1] a b
#> Levels: a b c
to("mean", mean)
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x555ee4255528>
#> <environment: namespace:base>
to("2024-01-01", as.Date("2024-01-01"))
#> [1] "2024-01-01"
to("2024-01-01T12:00:00Z", as.POSIXct("2024-01-01", tz = "UTC"))
#> [1] "2024-01-01 12:00:00 UTC"