R编程中如何将字符对象转换为整数?–as.integer()函数

2021年4月3日19:26:19 发表评论 1,800 次浏览

as.integer()发挥作用R语言用于将字符对象转换为整数对象。

语法:as.integer(x)
参数:x:字符对象

范例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

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: