Open In App

Convert values of an Object to Logical Vector in R Programming – as.logical() Function

Last Updated : 08 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

as.logical() function in R Language is used to convert an object to a logical vector.

Syntax: as.logical(x)
Parameters: 
x: Numeric or character object

R – as.logical() Function Example

Example 1: Basic example of as.logical() Function in R Programming Language.

R




# R Program to convert
# an object to logical vector
 
# Creating a vector
x <- c(1, 2, 3, 0, 1.4, NA)
 
# Calling as.logical() function
as.logical(T)
as.logical("F")
as.logical(2)
as.logical(x)


Output: 

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

Example 2: Multiple objects with as.logical() Function in R.

R




# R Program to convert
# an object to logical vector
 
# Creating matrix
x1 <- matrix(c(1:4), 2, 2)
x2 <- matrix(c(2, 0, 1, 1.3), 2, 2)
 
# Calling as.logical() function
as.logical(x1)
as.logical(x2)


Output: 

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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads