为什么合并两个数据框时出现此错误?

时间:2019-10-06 18:33:50

标签: r dataframe merge

我正在尝试通过称为“团队”的列名称合并两个数据框。

我的合并语句-

merge(RB,LB,by.x ="team")

我得到的错误是-

  

merge.data.frame(RB,LB,by.x =“ team”)中的错误:'by.x'和   “ by.y”    指定不同的列数。

#Create a data frame to store set of Right-Backs
      RB=data.frame(
       team=c("Liverpool",
     "Manchester United",
     "Chelsea","Atletico Madrid",
     "Juventus",
     "Real Madrid"),
     players=c("Trent-Alexandre Arnold",
        "Diogo Dalot",
        "Cesar Azpilicueta",
        "Keiran Trippier",
        "Danilo","Carvajal")
      ,stringsAsFactors = FALSE)

   #Create a data frame to store set of Left-Backs
    LB=data.frame(
    team=c("Manchester United",
     "Real Madrid",
     "Liverpool",
     "Chelsea",
     "Juventus",
     "Atletico Madrid"
     ),
     players=c("Luke Shaw","Marcelo","Andrew Robertson","Marcos Alonso","Alex Sandro", "Renan Lodi" ),
    stringsAsFactors = FALSE
     )

2 个答案:

答案 0 :(得分:3)

您必须同时提供by.xby.y,或者只使用by

df <- merge(RB,LB, by.x="team", by.y="team")
df <- merge(RB,LB, by="team")

从参考文献:

  

默认情况下,数据框在列上合并,其名称为   都有,但是可以通过   by.x和by.y。

如果您不使用by.y,则默认使用by等于intersect(names(x), names(y))的输入。由于by.x仅具有一列,而by.y-仅具有两列(即,它们具有不同的长度),因此函数终止。

答案 1 :(得分:0)

> merge(RB, LB, by = "team")
           team              players.x        players.y
1   Atletico Madrid        Keiran Trippier       Renan Lodi
2           Chelsea      Cesar Azpilicueta    Marcos Alonso
3          Juventus                 Danilo      Alex Sandro
4         Liverpool Trent-Alexandre Arnold Andrew Robertson
5 Manchester United            Diogo Dalot        Luke Shaw
6       Real Madrid               Carvajal          Marcelo

或者您可以使用dplyr软件包中的left_join()来获得相同的结果。

> left_join(RB,LB, by = "team")
           team              players.x        players.y
1         Liverpool Trent-Alexandre Arnold Andrew Robertson
2 Manchester United            Diogo Dalot        Luke Shaw
3           Chelsea      Cesar Azpilicueta    Marcos Alonso
4   Atletico Madrid        Keiran Trippier       Renan Lodi
5          Juventus                 Danilo      Alex Sandro
6       Real Madrid               Carvajal          Marcelo