R 语言数据类型
数据类型
在编程中,数据类型是一个重要的概念。
变量可以存储不同类型的数据,不同类型可以做不同的事情。
在 R 中,变量不需要用任何特定类型声明,甚至可以在设置后更改类型:
实例
my_var <- 30 # my_var is type of numericmy_var <- "Sally" # my_var is now of type character (aka string)my_var # print my_var
R 有多种数据类型和对象类。随着你对 R 的不断了解,你会对这些有更多的了解。
基本数据类型
R 中的基本数据类型可分为以下类型:
numeric-(10.55787)integer-(1L、55L、100L,其中字母 "L" 将其声明为整数)complex-(9+3i,其中 "i" 是虚部)character(又称字符串)-("k","R 令人兴奋","FALSE","11.5")logical(又称布尔)-(true 或 false)
我们可以使用 class() 函数来检查变量的数据类型:
实例
# numericx <- 10.5class(x)# integerx <- 1000Lclass(x)# complexx <- 9i + 3class(x)# character/stringx <- "R is exciting"class(x)# logicalx <- TRUEclass(x)
在接下来的章节中,您将了解更多关于各个数据类型的信息。