Skip to content
Related Articles

Related Articles

Improve Article
Convert values of an Object to Logical Vector in R Programming – as.logical() Function
  • Last Updated : 16 Jun, 2020

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

Syntax: as.logical(x)

Parameters:
x: Numeric or character object

Example 1:




# 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:




# 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
My Personal Notes arrow_drop_up
Recommended Articles
Page :