R语言 数据结构

R语言 数据结构

数据结构是在计算机中组织数据的一种特殊方式,以便能够有效地使用它。这个想法是为了减少不同任务的空间和时间的复杂性。R编程中的数据结构是用于保存多个值的工具。

R的基础数据结构通常按其维度(1D、2D或nD)以及它们是同质的(所有元素必须是相同的类型)还是异质的(元素通常有各种类型)来组织。这就产生了在数据分析中最经常使用的六种数据类型。

R中使用的最基本的数据结构包括。

  • 向量
  • 列表
  • 数据框架
  • 矩阵
  • 数组
  • 因子

矢量

向量是给定长度的基本数据类型的有序集合。这里唯一的关键是一个向量的所有元素必须是相同的数据类型,如同质数据结构。矢量是一维的数据结构。

例子

# R program to illustrate Vector
 
# Vectors(ordered collection of same data type)
X = c(1, 3, 5, 7, 8)
 
# Printing those elements in console
print(X)

输出

[1] 1 3 5 7 8

列表

列表是一个由有序的对象集合组成的通用对象。列表是异质性的数据结构。这些也是一维的数据结构。一个列表可以是向量列表、矩阵列表、字符列表和函数列表等等。

例子

# R program to illustrate a List
 
# The first attributes is a numeric vector
# containing the employee IDs which is
# created using the 'c' command here
empId = c(1, 2, 3, 4)
 
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
 
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
 
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
 
print(empList)

输出

[[1]]
[1] 1 2 3 4

[[2]]
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

[[3]]
[1] 4

数据框架

数据框是R的通用数据对象,用于存储表格数据。数据框是R编程中最受欢迎的数据对象,因为我们很愿意看到表格形式的数据。它们是二维的、异质的数据结构。这些是等长的向量列表。

数据框架有以下的限制。

  • 一个数据框架必须有列名,每一行都应该有一个唯一的名字。
  • 每一列必须有相同数量的项目。
  • 单一列中的每个项目必须具有相同的数据类型。
  • 不同的列可以有不同的数据类型。

为了创建一个数据框,我们使用data.frame()函数。

例子

# R program to illustrate dataframe
 
# A vector which is a character vector
Name = c("Amiya", "Raj", "Asish")
 
# A vector which is a character vector
Language = c("R", "Python", "Java")
 
# A vector which is a numeric vector
Age = c(22, 25, 45)
 
# To create dataframe use data.frame command
# and then pass each of the vectors
# we have created as arguments
# to the function data.frame()
df = data.frame(Name, Language, Age)
 
print(df)

输出

   Name Language Age
1 Amiya        R  22
2   Raj   Python  25
3 Asish     Java  45

矩阵

矩阵是一个由行和列组成的矩形数字排列。在一个矩阵中,我们知道行是水平方向的,列是垂直方向的。矩阵是二维的、同质的数据结构。

现在,让我们看看如何在R中创建一个矩阵。要在R中创建一个矩阵,你需要使用称为矩阵的函数。这个matrix()的参数是向量中元素的集合。你必须传递你希望在你的矩阵中拥有多少行和多少列,这是重要的一点,你必须记住,默认情况下,矩阵是按列顺序排列的。

例子

# R program to illustrate a matrix
 
A = matrix(
    # Taking sequence of elements
    c(1, 2, 3, 4, 5, 6, 7, 8, 9),
     
    # No of rows and columns
    nrow = 3, ncol = 3, 
 
    # By default matrices are
    # in column-wise order
    # So this parameter decides
    # how to arrange the matrix         
    byrow = TRUE                            
)
 
print(A)

输出

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

数组

数组是R的数据对象,它以两个以上的维度存储数据。数组是n维的数据结构。例如,如果我们创建一个维度为(2,3,3)的数组,那么它将创建3个矩形矩阵,每个矩阵有2行和3列。它们是同质的数据结构。

现在,让我们看看如何在R中创建数组。要在R中创建一个数组,你需要使用名为array()的函数。这个array()的参数是向量中的元素集合,你必须传递一个包含数组尺寸的向量。

例子

# R program to illustrate an array
 
A = array(
    # Taking sequence of elements
    c(1, 2, 3, 4, 5, 6, 7, 8),
 
    # Creating two rectangular matrices
    # each with two rows and two columns
    dim = c(2, 2, 2)                       
)
 
print(A)

输出

, , 1

     [,1] [,2]
[1,]    1    3
[2,]    2    4

, , 2

     [,1] [,2]
[1,]    5    7
[2,]    6    8

因素

因素是数据对象,用于对数据进行分类并将其存储为等级。它们对于存储分类数据非常有用。它们可以存储字符串和整数。它们对于分类列中的唯一值很有用,如 “TRUE “或 “FALSE”,或 “MALE “或 “FEMALE”,等等。它们在统计建模的数据分析中很有用。

现在,让我们看看如何在R中创建因子。要在R中创建一个因子,你需要使用名为 factor() 的函数。这个factor()的参数是向量。

例子

# R program to illustrate factors
 
# Creating factor using factor()
fac = factor(c("Male", "Female", "Male",
               "Male", "Female", "Male", "Female"))
 
print(fac)

输出

[1] Male   Female Male   Male   Female Male   Female
Levels: Female Male

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程