Skip to content

Instantly share code, notes, and snippets.

Created July 6, 2021 04:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
An `as.numeric()` in R that warns about non-numeric values
as.numeric.verbose = function(x) {
# this code will parse german-style comma-as-decimal numbers or
# english-style dot-as-decimal numbers.
x = ifelse(grepl('^\\d+,\\d+$', x, perl=T), sub(',', '.', x, fixed=T), x)
x.num = as.numeric(x)
# Find which values got coerced to NA
coerced = which(! &
if (length(coerced) > 0) {
cat("Following values coerced to NA:\n")
print(paste(coerced, x[coerced]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment