📜  R中的数据框列表

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

R中的数据框列表

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

  • 创建数据框列表
  • 访问数据框列表的组件
  • 修改数据框列表的组件
  • 数据框列表的串联
  • 删除数据框列表的组件

创建数据框列表

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

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


Python3
# 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(listOfDataframe$Dataframe2)


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


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


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


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


输出:

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

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

访问数据框列表的组件

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

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

Python3

# 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(listOfDataframe$Dataframe2)
  • 输出:
$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
  • 通过索引访问组件:我们还可以使用索引访问数据框列表的组件。要访问数据帧列表的顶级组件,我们必须使用双切片运算符“[[ ]]”,它是两个方括号,如果我们想要访问列表的较低或内层组件,我们必须使用另一个方括号“[ ]”以及双切片运算符“[[ ]]”。
    例子:

Python3

# 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])
  • 输出:
$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

修改数据框列表的组件

也可以通过访问组件并将它们替换为您想要的组件来修改数据框列表。
例子:

Python3

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

输出:

Before modifying the list of data frame
$Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6

$Dataframe2
  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 67

$Dataframe2
  y1 y2
1 70 14
2 80 41
3  9 63

数据框列表的串联

可以使用连接函数连接两个数据帧列表。因此,当我们想要连接两个数据框列表时,我们必须使用连接运算符。
句法:

例子:

Python3

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

输出:

Before concatenation of the new list of data frame
$Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6

$Dataframe2
  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  6

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

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

从数据框列表中删除组件

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

Python3

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

输出:

Before deletion the list is
$Dataframe1
  y1 y2
1  1  4
2  2  5
3  3  6

$Dataframe2
  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