R – Check if Type of Vector is Logical

To check if type of given vector is logical in R, call is.logical() function and pass the vector as argument to this function. If the given vector is logical, then is.logical() returns TRUE, or else, it returns FALSE.

The syntax to call is.logical() to check if type of vector x is logical is

</>
Copy
is.logical(x)

Return Value

The function returns a logical value.

Examples

In the following program, we take a vector in x, and check if this vector is a logical vector using is.logical() function. We print the value returned by is.logical().

example.R

</>
Copy
x <- c(TRUE, FALSE, NA, FALSE)
result <- is.logical(x)
print(result)

Output

[1] TRUE

Now, let us take a vector x of some type other than logical, and check the value returned by is.logical(x). Since x is not logical, is.logical() must return FALSE.

example.R

</>
Copy
x <- c(25, 84, 10)
result <- is.logical(x)
print(result)

Output

[1] FALSE

Conclusion

In this R Tutorial, we learned how to check if given vector is of type logical, using is.logical() function, with the help of examples.