📜  R数据类型

📅  最后修改于: 2021-01-08 09:24:47             🧑  作者: Mango

R编程中的数据类型

在编程语言中,我们需要使用各种变量来存储各种信息。变量是用于存储值的保留内存位置。在程序中创建变量时,内存中会保留一些空间。

在R中,有几种数据类型,例如整数,字符串等。操作系统根据变量的数据类型分配内存,并确定可以在保留的内存中存储什么。

R编程中使用以下数据类型:

Data type Example Description
Logical True, False It is a special data type for data with only two possible values which can be construed as true/false.
Numeric 12,32,112,5432 Decimal value is called numeric in R, and it is the default computational data type.
Integer 3L, 66L, 2346L Here, L tells R to store the value as an integer,
Complex Z=1+2i, t=7+3i A complex value in R is defined as the pure imaginary value i.
Character ‘a’, ‘”good'”, “TRUE”, ‘35.4’ In R programming, a character is used to represent string values. We convert objects into character values with the help ofas.character() function.
Raw A raw data type is used to holds raw bytes.

让我们看一个示例,以更好地理解数据类型:

#Logical Data type
variable_logical<- TRUE
cat(variable_logical,"\n")
cat("The data type of variable_logical is ",class(variable_logical),"\n\n")

#Numeric Data type
variable_numeric<- 3532
cat(variable_numeric,"\n")   
cat("The data type of variable_numeric is ",class(variable_numeric),"\n\n")

#Integer Data type
variable_integer<- 133L
cat(variable_integer,"\n") 
cat("The data type of variable_integer is ",class(variable_integer),"\n\n")

#Complex Data type
variable_complex<- 3+2i
cat(variable_complex,"\n")
cat("The data type of variable_complex is ",class(variable_complex),"\n\n")

#Character Data type
variable_char<- "Learning r programming"
cat(variable_char,"\n")
cat("The data type of variable_char is ",class(variable_char),"\n\n")

#Raw Data type
variable_raw<- charToRaw("Learning r programming")
cat(variable_raw,"\n")
cat("The data type of variable_char is ",class(variable_raw),"\n\n")

当我们执行以下程序时,它将提供以下输出: