我正在尝试通过称为“团队”的列名称合并两个数据框。
我的合并语句-
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
)
您必须同时提供by.x
和by.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
-两列(即,它们具有不同的长度),所以函数终止。
> 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