R语言 数据框架列表

R语言 数据框架列表

DataFrames是R的通用数据对象,用于存储表格数据。它们是二维的、异质的数据结构。而R语言中的列表由元素、向量、数据框、变量或列表组成,可能属于不同的数据类型。在这篇文章中,我们将研究如何创建一个由数据帧组成的列表,以及如何访问、修改和删除这些数据帧的列表。 R中的 list() 函数创建了一个指定参数的列表。在这个函数中指定为参数的数据帧可能有不同的长度。
可以对数据帧的列表进行的操作有:

  • 创建一个数据帧的列表
  • 访问数据帧列表中的组件
  • 修改数据帧列表中的组件
  • 串联数据帧的列表
  • 删除数据帧列表中的组件

创建一个数据帧列表

为了创建一个数据框列表,我们使用R中的 list() 函数,然后将你创建的每个数据框作为参数传递给该函数。

示例:

# R program to create list of data frames
 
# Create dataframe
df1 = data.frame(
  y1 = c(1, 2, 3),
  y2 = c(4, 5, 6)
)
 
# Create another dataframe
df2 = data.frame(
  y1 = c(7, 8, 9),
  y2 = c(1, 4, 6)
)
 
# Create list of data frame using list()
listOfDataframe = list(df1, df2)
print(listOfDataframe)
R

输出:

  [[1]]
  y1 y2
1  1  4
2  2  5
3  3  6

[[2]]
  y1 y2
1  7  1
2  8  4
3  9  6
R

访问数据框架列表中的组件

我们可以通过两种方式访问数据框列表中的组件。

  • 通过名字访问组件: 数据框架列表中的所有组件都可以被命名,我们可以使用这些名字,用美元命令访问列表中的组件。

例子:

# R program to access components
# of a list of data frames
 
# Create dataframe
df1 = data.frame(
  y1 = c(1, 2, 3),
  y2 = c(4, 5, 6)
)
 
# Create another dataframe
df2 = data.frame(
  y1 = c(7, 8, 9),
  y2 = c(1, 4, 6)
)
 
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
  "Dataframe1" = df1,
  "Dataframe2" = df2
)
print(listOfDataframe)
 
# Accessing components by names
cat("Accessing Dataframe2 using command\n")
print(listOfDataframeDataframe2)
R
  • 输出:
$Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6

$Dataframe2
  y1 y2
1  7  1
2  8  4
3  9  6

Accessing Dataframe2 using $ command
  y1 y2
1  7  1
2  8  4
3  9  6
R
  • 通过索引访问组件: 我们也可以使用索引访问数据框架列表的组件。要访问数据框架列表的顶层组件,我们必须使用双切片操作符”[[]]”,即两个方括号,如果我们想访问列表的低层或内层组件,我们必须使用另一个方括号”[]”和双切片操作符”[[]]”。

例:

# R program to access components
# of a list of data frames
 
# Create dataframe
df1 = data.frame(
  y1 = c(1, 2, 3),
  y2 = c(4, 5, 6)
)
 
# Create another dataframe
df2 = data.frame(
  y1 = c(7, 8, 9),
  y2 = c(1, 4, 6)
)
 
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
  "Dataframe1" = df1,
  "Dataframe2" = df2
)
print(listOfDataframe)
 
# Accessing a top level components by indices
cat("Accessing Dataframe2 using indices\n")
print(listOfDataframe[[2]])
 
# Accessing a inner level components by indices
cat("Accessing second column from Dataframe1 using indices\n")
print(listOfDataframe[[1]][2])
 
# Accessing another inner level components by indices
cat("Accessing 4 from Dataframe2 using indices\n")
# Here [2, 2] represents that I want
# to access element from second row and second column i.e 4 here
print(listOfDataframe[[2]][2, 2])
R
  • 输出:
$Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6

$Dataframe2
  y1 y2
1  7  1
2  8  4
3  9  6

Accessing Dataframe2 using indices
  y1 y2
1  7  1
2  8  4
3  9  6

Accessing second column from Dataframe1 using indices
  y2
1  4
2  5
3  6

Accessing 4 from Dataframe2 using indices
[1] 4
R

修改数据帧列表的组件

一个数据框架列表也可以通过访问组件并将其替换为你想要的组件来进行修改。

例子:

# R program to modify components
# of a list of data frames
 
# Create dataframe
df1 = data.frame(
  y1 = c(1, 2, 3),
  y2 = c(4, 5, 6)
)
 
# Create another dataframe
df2 = data.frame(
  y1 = c(7, 8, 9),
  y2 = c(1, 4, 6)
)
 
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
  "Dataframe1" = df1,
  "Dataframe2" = df2
)
cat("Before modifying the list of data frame\n")
print(listOfDataframe)
 
 
# Modifying the dataframe2
listOfDataframe$Dataframe2 = data.frame(
  y1 = c(70, 80, 9),
  y2 = c(14, 41, 63)
)
 
# Modifying second column from Dataframe1
listOfDataframe[[1]][2] = c(23, 45, 67)
 
# Modifying element 2 from dataframe1
listOfDataframe[[1]][2, 1] = 15
 
cat("After modified the list of data frame\n")
print(listOfDataframe)
R

输出:

Before modifying the list of data frame
Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6Dataframe2
  y1 y2
1  7  1
2  8  4
3  9  6

After modified the list of data frame
Dataframe1
  y1 y2
1  1 23
2 15 45
3  3 67Dataframe2
  y1 y2
1 70 14
2 80 41
3  9 63
R

数据框架列表的连接

两个数据框架列表可以使用连接函数进行连接。因此,当我们要连接两个数据框架列表时,必须使用连接运算符。

语法:

list = c(list, list1)

list = 数据框的原始列表

list1 = 数据框的新列表

示例:

# R program concatenation 
# of lists of data frames
 
# Create dataframe
df1 = data.frame(
  y1 = c(1, 2, 3),
  y2 = c(4, 5, 6)
)
 
# Create another dataframe
df2 = data.frame(
  y1 = c(7, 8, 9),
  y2 = c(1, 4, 6)
)
 
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
  "Dataframe1" = df1,
  "Dataframe2" = df2
)
cat("Before concatenation of the new list of data frame\n")
print(listOfDataframe)
 
# Creating another one list of data frame
df3 = data.frame(
  y1 = c(7, 8, 98),
  y2 = c(10, 44, 6)
)
newListOfDataframe = list(
  "Dataframe3" = df3
  )
 
# Concatenation of list of data frames
# using concatenation operator
listOfDataframe = c(listOfDataframe, newListOfDataframe)
 
cat("After concatenation of the new list of data frame\n")
print(listOfDataframe)
R

输出:

Before concatenation of the new list of data frame
Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6Dataframe2
  y1 y2
1  7  1
2  8  4
3  9  6

After concatenation of the new list of data frame
Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6Dataframe2
  y1 y2
1  7  1
2  8  4
3  9  6

$Dataframe3
  y1 y2
1  7 10
2  8 44
3 98  6
R

从数据框架列表中删除组件

要删除数据框架列表中的组件,首先,我们需要访问这些组件,然后在这些组件前插入一个负号。它表示我们必须删除该组件。

例子:

# R program to delete components 
# of a list of data frames
 
# Create dataframe
df1 = data.frame(
  y1 = c(1, 2, 3),
  y2 = c(4, 5, 6)
)
 
# Create another dataframe
df2 = data.frame(
  y1 = c(7, 8, 9),
  y2 = c(1, 4, 6)
)
 
# Creating a list of data frames
# by naming all its components
listOfDataframe = list(
  "Dataframe1" = df1,
  "Dataframe2" = df2
)
cat("Before deletion the list is\n")
print(listOfDataframe)
 
# Deleting a top level components
cat("After Deleting Dataframe1\n")
print(listOfDataframe[[-1]])
 
# Deleting a inner level components
cat("After Deleting first column from Dataframe2\n")
print(listOfDataframe[[2]][-1])
R

输出:

Before deletion the list is
Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6Dataframe2
  y1 y2
1  7  1
2  8  4
3  9  6

After Deleting Dataframe1
  y1 y2
1  7  1
2  8  4
3  9  6
After Deleting first column from Dataframe2
  y2
1  1
2  4
3  6
R

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册