R语言 数据类型
R中的每个变量都有一个相关的数据类型。每种数据类型需要不同数量的内存,并有一些可以对其进行的特定操作。R编程语言有以下基本数据类型,下表显示了数据类型和每个数据类型可以采取的值。
R编程语言中的数据类型
基本数据类型 | 数值 |
---|---|
数值 | 所有实数的集合 |
整数 | 所有整数的集合,Z |
逻辑的 | 真值和假值 |
复数 | 复数的集合 |
字符 | “a”, “b”, “c”, …, “@”, “#”, “$”, …., “1”, “2”, … 等 |
数字数据类型
十进制值在R中被称为数值,它是R中数字的默认数据类型。如果你像下面这样给一个变量x分配一个十进制值,x将是数值类型的。
# A simple R program
# to illustrate Numeric data type
# Assign a decimal value to x
x = 5.6
# print the class name of variable
print(class(x))
# print the type of variable
print(typeof(x))
输出
[1] "numeric"
[1] "double"
即使一个整数被分配给一个变量y,它仍然被保存为一个数字值。
# A simple R program
# to illustrate Numeric data type
# Assign an integer value to y
y = 5
# print the class name of variable
print(class(y))
# print the type of variable
print(typeof(y))
输出
[1] "numeric"
[1] "double"
当R将一个数字存储在一个变量中时,它会将这个数字转换成一个 “双倍 “值或一个至少有两个小数位的小数类型。这意味着,这里的 “5 “这个值被存储为5.00,类型为双数,类别为数字。还有,这里的y不是一个整数,可以用 is.integer() 函数来确认。
# A simple R program
# to illustrate Numeric data type
# Assign a integer value to y
y = 5
# is y an integer?
print(is.integer(y))
输出
[1] FALSE
整数数据类型
R支持整数数据类型,这是所有整数的集合。你可以使用 as.integer() 函数来创建以及将一个值转换成一个整数类型。你也可以使用大写的 “L “符号作为后缀来表示一个特定的值是整数数据类型。
# A simple R program
# to illustrate integer data type
# Create an integer value
x = as.integer(5)
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))
# Declare an integer by appending an L suffix.
y = 5L
# print the class name of y
print(class(y))
# print the type of y
print(typeof(y))
输出
[1] "integer"
[1] "integer"
[1] "integer"
[1] "integer"
逻辑数据类型
R有逻辑数据类型,其值为真或假。一个逻辑值通常是通过变量之间的比较来创建的。
# A simple R program
# to illustrate logical data type
# Sample values
x = 4
y = 3
# Comparing two values
z = x > y
# print the logical value
print(z)
# print the class name of z
print(class(z))
# print the type of z
print(typeof())
输出
[1] TRUE
[1] "logical"
[1] "logical"
复数数据类型
R支持复合数据类型,它是所有复合数的集合。复数数据类型是用来存储具有虚数成分的数字。
# A simple R program
# to illustrate complex data type
# Assign a complex value to x
x = 4 + 3i
# print the class name of x
print(class(x))
# print the type of x
print(typeof(x))
输出
[1] "complex"
[1] "complex"
字符数据类型
R支持字符数据类型,你可以拥有所有的字母和特殊字符。它存储字符值或字符串。R语言中的字符串可以包含字母、数字和符号。在R中表示一个值是字符类型的最简单的方法是将该值包在单引号或双引号中。
# A simple R program
# to illustrate character data type
# Assign a character value to char
char = "Geeksforgeeks"
# print the class name of char
print(class(char))
# print the type of char
print(typeof(char))
输出
[1] "character"
[1] "character"
有几个任务可以使用数据类型来完成。让我们来了解每个任务的操作,以及执行任务的语法,同时用R代码来说明这个任务。
查找一个对象的数据类型
要找到一个对象的数据类型,你必须使用 class() 函数。这样做的语法是,你需要将对象作为参数传递给函数 class() ,以查找对象的数据类型。
语法
class(object)
例子
# A simple R program
# to find data type of an object
# Logical
print(class(TRUE))
# Integer
print(class(3L))
# Numeric
print(class(10.5))
# Complex
print(class(1+2i))
# Character
print(class("12-04-2020"))
输出
[1] "logical"
[1] "integer"
[1] "numeric"
[1] "complex"
[1] "character"
类型验证
要做到这一点,你需要在数据类型之前使用前缀 “is. “作为命令。其语法是,你要验证的对象的 is.data_type() 。
语法
is.data_type(object)
例子
# A simple R program
# Verify if an object is of a certain datatype
# Logical
print(is.logical(TRUE))
# Integer
print(is.integer(3L))
# Numeric
print(is.numeric(10.5))
# Complex
print(is.complex(1+2i))
# Character
print(is.character("12-04-2020"))
print(is.integer("a"))
print(is.numeric(2+3i))
输出
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
胁迫或转换一个对象的数据类型到另一个对象
这个任务很有意思,你可以改变或转换一个对象的数据类型到另一个对象。为了执行这个动作,你必须在数据类型之前使用前缀 “as. “作为命令。这样做的语法是你想强制执行的对象的 as.data_type() 。
语法
as.data_type(object)
注意: 所有的胁迫都是不可能的,如果尝试,将返回一个 “NA “值。
例子
# A simple R program
# convert data type of an object to another
# Logical
print(as.numeric(TRUE))
# Integer
print(as.complex(3L))
# Numeric
print(as.logical(10.5))
# Complex
print(as.character(1+2i))
# Can't possible
print(as.numeric("12-04-2020"))
输出
[1] 1
[1] 3+0i
[1] TRUE
[1] "1+2i"
[1] NA
Warning message:
In print(as.numeric("12-04-2020")) : NAs introduced by coercion