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

checkCoercion

Check if values can be safely coerced without introduction of missing values


Description

This might be named "coercesSafely" or such. If values cannot be coerced into class specified, then values must be incorrect.

Usage

checkCoercion(value, targetclass, na.strings = c("\\.", "", "\\s+",
  "N/A"))

Arguments

value

Character vector of values, such as value_new or value_old for one variable in a key.

targetclass

R class name

na.strings

Values that should be interpreted as R NA. These are ignored in the coercion check.

Value

either TRUE, or a vector of values which are not successfully coerced

Author(s)

Paul Johnson <pauljohn@ku.edu>

Examples

x1 <- c("TRUE", "FALSE", FALSE, TRUE, NA, ".", "N/A", " ", "")
checkCoercion(x1, "logical")
x1 <- c(x1, "TRUE.FALSE", "Has a space")
## Should fail:
checkCoercion(x1, "logical")
x2 <- c(4, 5, 6, 9.2, ".", " ")
## Should fail
checkCoercion(x2, "logical")
x3 <- factor(c("bob", "emily", "bob", "jane", "N/A", " ", NA, "NA"))
checkCoercion(x3, "ordered")
checkCoercion(x3, "integer")
## Should fail:
checkCoercion(x3, "logical")

kutils

Project Management Tools

v1.70
GPL-2
Authors
Paul Johnson [aut, cre], Benjamin Kite [aut], Charles Redmon [aut], Jared Harpole [ctb], Kenna Whitley [ctb], Po-Yi Chen [ctb], Shadi Pirhosseinloo [ctb]
Initial release
2020-04-28

We don't support your browser anymore

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