Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

coerceValue

Coerce a character string to the same type as a target value


Description

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.

Usage

coerceValue(val, old)

Arguments

val

A character string.

old

An old value, whose type is the target type of val.

Details

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.

Value

A value of the same data type as old if possible.

Examples

library(DT)
coerceValue("100", 1L)
coerceValue("1.23", 3.1416)
coerceValue("2018-02-14", Sys.Date())
coerceValue("2018-02-14T22:18:52Z", Sys.time())
coerceValue("setosa", iris$Species)
coerceValue("setosa2", iris$Species)  # NA
coerceValue("FALSE", TRUE)  # not supported

DT

A Wrapper of the JavaScript Library 'DataTables'

v0.18
GPL-3 | file LICENSE
Authors
Yihui Xie [aut, cre], Joe Cheng [aut], Xianying Tan [aut], JJ Allaire [ctb], Maximilian Girlich [ctb], Greg Freedman Ellis [ctb], Johannes Rauh [ctb], jQuery contributors [ctb, cph] (jQuery in htmlwidgets/lib), SpryMedia Limited [ctb, cph] (DataTables in htmlwidgets/lib), Brian Reavis [ctb, cph] (selectize.js in htmlwidgets/lib), Leon Gersen [ctb, cph] (noUiSlider in htmlwidgets/lib), Bartek Szopka [ctb, cph] (jquery.highlight.js in htmlwidgets/lib), RStudio, PBC [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.