📜  R – 数据类型

📅  最后修改于: 2022-05-13 01:54:56.131000             🧑  作者: Mango

R – 数据类型

R 中的每个变量都有一个关联的数据类型。每种数据类型需要不同数量的内存,并且有一些可以在其上执行的特定操作。 R 编程语言具有以下基本数据类型,下表显示了数据类型和每种数据类型可以采用的值。

R 编程语言中的数据类型:

Basic Data TypesValues
NumericSet of all real numbers
IntegerSet of all integers, Z
LogicalTRUE and FALSE
ComplexSet of complex numbers
Character“a”, “b”, “c”, …, “@”, “#”, “$”, …., “1”, “2”, …etc

数值数据类型

十进制值在 R 中称为数字。它是 R 中数字的默认数据类型。如果您为变量 x 分配十进制值,如下所示,x 将是数字类型。

R
# 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))


R
# 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))


R
# 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))


R
# 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))


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())


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))


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))


R
# 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"))


R
# 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))


R
# 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] "numeric"
[1] "double"

即使将整数分配给变量 y,它仍被保存为数值。

R

# 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()函数来确认。

R

# 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”符号作为后缀来表示特定值是整数数据类型。

R

# 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 具有逻辑数据类型,其值为真或假。逻辑值通常是通过变量之间的比较来创建的。

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 支持由所有复数组成的复数数据类型。复杂数据类型是存储带有虚部的数字。

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 中表示一个值是字符类型的最简单方法是将值包含在单引号或双引号内。

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)

例子:

R

# 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)

例子:

R

# 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”值。

例子:

R

# 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