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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

[1] TRUE TRUE TRUE TRUE
[1]  TRUE FALSE  TRUE  TRUE
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.