Search code examples
rregexdecimalnumeric

How to check if the value is numeric?


Can someone help me modify the function below to check if a number is numeric?

# handy function that checks if something is numeric
check.numeric <- function(N){
  !length(grep("[^[:digit:]]", as.character(N)))
}

check.numeric(3243)
#TRUE
check.numeric("sdds")
#FALSE
check.numeric(3.14)
#FALSE

I want check.numeric() to return TRUE when it's a decimal like 3.14.


Solution

  • Sounds like you want a function like this:

    f <- function(x) is.numeric(x) & !is.na(x)