Create a new value from a character string based on an old value, e.g., if
the old value is an integer, call as.integer() to coerce the string to
an integer.
coerceValue(val, old)
| val | A character string. |
|---|---|
| old | An old value, whose type is the target type of |
A value of the same data type as old if possible.
This function only works with integer, double, date, time (POSIXlt or
POSIXct), and factor values. The date must be of the format
%Y-%m-%dT%H:%M:%SZ. The factor value must be in the levels of
old, otherwise it will be coerced to NA.
#> [1] 100coerceValue("1.23", 3.1416)#> [1] 1.23#> [1] "2018-02-14"#> [1] "2018-02-14 22:18:52 UTC"coerceValue("setosa", iris$Species)#> [1] "setosa"coerceValue("setosa2", iris$Species) # NA#> Warning: New value(s) "setosa2" not in the original factor levels: "setosa, versicolor, virginica"; will be coerced to NA.#> [1] NAcoerceValue("FALSE", TRUE) # not supported#> Warning: The data type is not supported: logical#> [1] "FALSE"