杭州电子商务研究院

在 R 中合并 DataFrames

2020-10-27 08:00飞浪

介绍

合并或连接数据框是将两个或多个数据框中的列组合在一起的过程。这是编程中众所周知的操作。在 R 中,我们可以使用两个函数执行连接:基础包的merge()和 dplyr 包的join()。在介绍这一点之前,本指南将介绍连接的类型。

连接类型

主要有四种类型的连接:

左外连接

假设您要连接两个表 A 和 B,其中 A 是左表,B 是右表。当您对 A 和 B 执行左外连接时,它将返回 A 中的所有行以及 B 中匹配的行。将返回 A 和 B 中的所有列,但 B 中不匹配的行的B 列值为NA。

右外连接

右外连接与左外连接类似。它将在左表中返回右表的所有匹配行。两个表的所有列都会返回,左表中不匹配的行将具有NA值。

内连接

内连接将返回两个表中所有匹配的行。如果两个表之间存在多个匹配项,则将返回所有组合。

完全加入

完全连接将返回两个表的所有行和列的值,无论它们是否匹配。

使用merge()连接数据框

merge ()函数属于 R 的基础包。您无需安装任何其他包即可使用merge()函数。merge ()函数的参数以及传入这些参数的默认值如下所示。

      # Syntax for merge function
merge(x, y, by = intersect(names(x), names(y)),
      by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all)
    
  • 前两个参数x和y是需要连接的数据框的名称。
  • 接下来的三个参数by、by.x和by.y决定了用于连接数据框的列。如果连接所需的列名称相同,则无需传递任何名称。如果它们不同,则必须传递by.x和by.y中的名称。
  • 接下来的三个参数决定了merge()执行的连接类型。默认值将执行内连接。如果all.x设置为TRUE,则它将执行左外连接。如果all.y设置为TRUE,则它将执行右外连接。如果两者都设置为TRUE,则它将执行全外连接。

本例中使用的数据框是band_members和band_instruments。列详细信息如下所示。

      # Loading dplyr function to use the datasets present in the package
library(dplyr)
data(band_members)
data(band_instruments)

# Columns in band_instruments
colnames(band_instruments)
[1] "name"  "plays"

#Columns in band_members
colnames(band_members)
[1] "name" "band"

# Lets look at the data
view(band_instruments)
  name  plays 
  <chr> <chr> 
1 John  guitar
2 Paul  bass  
3 Keith guitar

view(band_members)
  name  band   
  <chr> <chr>  
1 Mick  Stones 
2 John  Beatles
3 Paul  Beatles
    

下一个示例中的代码将使用上述数据框和merge()函数执行所有四种类型的连接。

      # Performing Left outer join
merge(band_members, band_instruments,  all.x = TRUE)
  name    band  plays
1 John Beatles guitar
2 Mick  Stones   <NA>
3 Paul Beatles   bass

# Performing Right outer join
merge(band_members, band_instruments,  all.y = TRUE)
   name    band  plays
1  John Beatles guitar
2 Keith    <NA> guitar
3  Paul Beatles   bass

# Performing Inner join
merge(band_members, band_instruments,  all.y = TRUE, all.x = TRUE)
   name    band  plays
1  John Beatles guitar
2 Keith    <NA> guitar
3  Mick  Stones   <NA>
4  Paul Beatles   bass

# Performing Full outer join
merge(band_members, band_instruments)
  name    band  plays
1 John Beatles guitar
2 Paul Beatles   bass
    

在连接的输出中,您可以看到,如果不存在匹配值,则它们将被分配为<NA>。 在内连接的情况下,它仅显示两个数据框中的匹配值。

使用 dplyr::join 函数连接数据框

与merge()函数相比,dplyr有四种不同的函数用于不同类型的连接。它避免了混淆,因为您不必设置参数的值。连接函数如下:

  • inner_join()
  • 左连接()
  • right_join()
  • 完整连接()

此示例将使用上述函数执行所有四种类型的连接。

      # Performing Inner join
inner_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 John  Beatles guitar
2 Paul  Beatles bass

# Performing Left outer join  
left_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 Mick  Stones  NA    
2 John  Beatles guitar
3 Paul  Beatles bass 

# Performing Right outer join 
 right_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 John  Beatles guitar
2 Paul  Beatles bass  
3 Keith NA      guitar

# Performing Full outer join
full_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 Mick  Stones  NA    
2 John  Beatles guitar
3 Paul  Beatles bass  
4 Keith NA      guitar
    

结论

当我们开始处理存储在不同表或源中的数据时,我们将开始探索它们之间的关系。在此过程中,我们将合并数据集以获得清晰的视图。此操作发生在每个围绕数据开展工作的项目中。

曝光28190浏览1529