在 R 编程中将字符对象转换为整数 - as.integer()函数
R 语言中的as.integer()
函数用于将字符对象转换为整数对象。
Syntax: as.integer(x)
Parameters:
x: Character Object
示例 1:
# R program to convert a character object
# to an integer object
# Calling as.integer() function
as.integer("4")
as.integer("1.6")
as.integer("-3.2")
as.integer("0x400")
输出:
[1] 4
[1] 1
[1] -3
[1] 1024
示例 2:
# R program to convert a character object
# to an integer object
# Creating a vector
x <- c(1, 2, 3, 4, 5)
# Check if the vector is an integer
is.integer(x)
# Convert to integer
x1 <- as.integer(x)
x1
# Calling is.integer() function
is.integer(x1)
输出:
[1] FALSE
[1] 1 2 3 4 5
[1] TRUE