R编程如何中将字符串转换为整数?–strtoi()函数

2021年4月3日19:39:51 发表评论 1,178 次浏览

strtoi()发挥作用:R语言用于将指定的字符串转换为整数

语法:strtoi(x, base = 0L)参数:x:字符向量base:2到36之间的整数(含2和36), 默认为0

范例1:

# R program to illustrate
# strtoi function
  
# Initializing some string vector
x < - c( "A" , "B" , "C" )
y < - c( "1" , "2" , "3" )
  
# Calling strtoi() function
strtoi(x, 16L )
strtoi(y)

输出:

[1] 10 11 12
[1] 1 2 3

范例2:

# R program to illustrate
# strtoi function
  
# Calling strtoi() function over
# some specified strings
strtoi(c( "0xff" , "023" , "567" ))
strtoi(c( "ffff" , "FFFF" ), 16L )
strtoi(c( "246" , "135" ), 8L )

输出如下:

[1] 255  19 567
[1] 65535 65535
[1] 166  93

木子山

发表评论

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