is.numeric in R: Check If Object is Numeric in R

All the decimal values are numerical values in R. That means if you assign any decimal value to that variable in R, it will become numeric. To check whether the argument is numeric or a data.frame with numeric columns in R, use the is.numeric() function.

is.numeric in R

The is.numeric in R is a built-in function used to check if the object passed to it as an argument is of numeric type. For example, the is.numeric() method returns TRUE if its argument is of mode “numeric” (which is either “double” or “integer“).

Syntax

is.numeric(obj)

Argument

obj: It is an R Object to test whether it is numeric or not.

Example

In this example, we will check 1, 0, negative, double, positive values, and complex values.

is.numeric(1)
is.numeric(0)
is.numeric(2.1)
is.numeric(-1.9)
is.numeric(1 + 6i)

Output

[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE

If we pass integer(negative or positive) or double values to the is.numeric() method, it returns TRUE, but if we pass the complex or factor value, it returns FALSE.

The double is the name of the type, and a numeric is the name of the mode and the implicit class.

Checking if a matrix is numeric

To create a matrix in R, use the matrix() function. After creating a matrix, pass the matrix to the is.numeric() function.

mat <- matrix(c(1:16), 4, 4)

is.numeric(mat)

Output

[1] TRUE

That’s it for this tutorial.

See also

as.numeric in R

as.factor in R

as.vector in R

NA in R

NaN in R

Leave a Comment