Skip to content

数据类型

常用

R 语言使用 typeof 来判断数据类型。

typeof(1)
typeof(1.5)
typeof(1L)
typeof('1')

typeof(1 + 2i)
typeof(TRUE)
[1] "double"
[1] "double"
[1] "integer"
[1] "character"

[1] "complex"
[1] "logical"

对于数字,R 语言默认是浮点数,如果需要整数,需要加大写 L 作为后缀。

类型转换

# 文本
as.character(123)

# 复数
as.complex(1)

# 浮点数
as.double('1')

# 整数
as.integer(1.5)

# 逻辑值
as.logical(1)

# 浮点数
as.numeric('1')
[1] "123"

[1] 1+0i

[1] 1

[1] 1

[1] TRUE

[1] 1

手动输入

#文本
'123'

# 空文本
''

#复数
1+2i

#浮点数
1

#整数
1L

#逻辑值
TRUE
FALSE
[1] "123"

[1] ""

[1] 1+2i

[1] 1

[1] 1

[1] TRUE

[1] FALSE

特殊

正无穷 Inf 。

1 / 0
[1] Inf

负无穷 -Inf 。

-1 / 0
[1] -Inf

NaN 。

0 / 0
[1] NaN

缺失值 NA ,Not Available 的缩写。

空对象 NULL 。

联系 math@baima.site