R语言 把一个对象的值转换成逻辑向量 – as.logical() 函数

R语言 把一个对象的值转换成逻辑向量 – as.logical() 函数

R语言中的 as.logical() 函数用于将一个对象转换为一个逻辑向量。

语法: as.logical(x)

参数:

x: 数字或字符对象

R – as.logical() 函数示例

例1:R语言中as.logical()函数的基本例子

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

输出:

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

例子2:在R中使用as.logical()函数的多个对象。

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

输出:

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程