is.numeric()
is.numeric(x) Returns:
logical · Updated March 13, 2026 · Base Functions numeric type-checking base
The is.numeric() function tests whether an object is numeric. This includes both integer and double (floating-point) types in R.
Syntax
is.numeric(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | any object | — | Object to test for numeric type |
Examples
Basic usage
# Numeric values
is.numeric(42)
# [1] TRUE
is.numeric(3.14)
# [1] TRUE
is.numeric(1:5) # integer sequence
# [1] TRUE
Non-numeric types
# Character
is.numeric("42")
# [1] FALSE
# Logical
is.numeric(TRUE)
# [1] FALSE
# Factor (even if numeric-looking)
is.numeric(factor(1:5))
# [1] FALSE
Numeric coercion
# as.numeric() converts to numeric
is.numeric(as.numeric(c("1", "2", "3")))
# [1] TRUE
# Warning: NAs introduced by coercion
x <- c("1", "2", "hello", "4")
is.numeric(x)
# [1] FALSE
as.numeric(x)
# [1] 1 2 NA 4
# Warning message:
# NAs introduced by coercion
Common Patterns
Type-safe operations
# Only operate on numeric data
safe_divide <- function(a, b) {
if (!is.numeric(a) || !is.numeric(b)) {
stop("Arguments must be numeric")
}
a / b
}
safe_divide(10, 2)
# [1] 5
safe_divide("10", 2)
# Error in safe_divide("10", 2) : Arguments must be numeric
Checking data types in data frames
df <- data.frame(
id = 1:3,
name = c("Alice", "Bob", "Charlie"),
score = c(85.5, 92.0, 78.5)
)
# Check column types
sapply(df, is.numeric)
# id name score
# TRUE FALSE TRUE
Numeric validation
# Validate numeric input
is_valid_numeric <- function(x) {
is.numeric(x) && all(!is.na(x))
}
is_valid_numeric(c(1, 2, 3))
# [1] TRUE
is_valid_numeric(c(1, NA, 3))
# [1] FALSE
is_valid_numeric("1")
# [1] FALSE