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

B_m*_*man 1 merge r dataframe

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

我的合并声明-

merge(RB,LB,by.x ="team")
Run Code Online (Sandbox Code Playgroud)

我收到的错误是-

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
     )
Run Code Online (Sandbox Code Playgroud)

sla*_*hut 6

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

df <- merge(RB,LB, by.x="team", by.y="team")
df <- merge(RB,LB, by="team")
Run Code Online (Sandbox Code Playgroud)

来自参考:

默认情况下,数据框在具有它们都具有的名称的列上合并,但列的单独规范可以由 by.x 和 by.y 给出。

如果不使用,则默认使用等于的by.y输入。因为只有一列和- 两列(即它们具有不同的长度),所以函数终止。byintersect(names(x), names(y))by.xby.y