R语言 获取或设置一个对象的类型 – mode()函数
R语言中的 mode() 函数用于获取或设置一个对象的类型或存储模式。
语法:
mode(x)
mode(x) <- value
这里 “value “是对象的期望模式或 “存储模式”(类型)。
参数:
x: R对象
例1 :
# R program to illustrate
# mode function
# Initializing some values
x <- 3
y <- "a"
# Calling the mode() function
mode(x)
mode(y)
输出
[1] "numeric"
[1] "character"
例2 :
# R program to illustrate
# mode function
# Initializing some values
x <- 3
y <- "a"
# Calling mode() function to
# set the storage mode of the object
mode(x) <- "character"
mode(y) <- "numeric"
# Getting the assigned storage mode
mode(x)
mode(y)
输出
[1] "character"
[1] "numeric"