不确定如何在两个数据集中将两组具有通用名称的数据组合在一起

时间:2019-02-25 00:04:31

标签: r rbind

我有一个标记为xPR的数据集,该数据集具有3列Player,Team和xPR,还有另一个标记为yPR的数据集,该数据集具有3个列,Player,Team和yPR。我要合并这两个数据集,以便每个玩家的xPR和yPR对齐,并且如果它们在这些类别之一中没有值,则将其标记为NA。

我尝试使用rbind,但最终没有成功

代码如下:

xPlayer <- x2017_CBB_Pitch$Player
xTeam <- x2017_CBB_Pitch$Team
xER <- x2017_CBB_Pitch$ERA
xIP <- x2017_CBB_Pitch$IP
xBB <- x2017_CBB_Pitch$BB
xSO <- x2017_CBB_Pitch$SO
xWP <- x2017_CBB_Pitch$WP
xHBP <- x2017_CBB_Pitch$HP

xPR.df <- data.frame(xPlayer,xTeam, xPR)

yPlayer <- y2018_CBB_Pitch$Player
yTeam <- y2018_CBB_Pitch$Team
yER <- y2018_CBB_Pitch$ERA
yIP <- y2018_CBB_Pitch$IP
yBB <- y2018_CBB_Pitch$BB
ySO <- y2018_CBB_Pitch$SO
yWP <- y2018_CBB_Pitch$WP
yHBP <- y2018_CBB_Pitch$HP

yPR.df <- data.frame(yPlayer, yTeam, yPR)

    > head(xPR.df)
             xPlayer          xTeam    xPR
    1  Luke Heimlich   Oregon State 33.428
    2 Clarke Schmidt South Carolina 27.388
    3    Beau Sulser      Dartmouth 20.460
    4   Andrew Crane           Troy 27.348
    5 Steven Gingery     Texas Tech 33.108
    6   Miguel Ausua   Oral Roberts 34.096
    > head(yPR.df)
             yPlayer                 yTeam    yPR
    1   Nick Sandlin         Southern Miss 24.528
    2    John Rooney               Hofstra 33.240
    3    Carter Love College of Charleston 30.616
    4  Ryan Campbell      Illinois-Chicago 36.580
    5   Frank German         North Florida 28.708
    6 Andre Pallante             UC Irvine 31.188

1 个答案:

答案 0 :(得分:0)

最基本的方法是使用外部合并。

我想您想基于Team和Player进行合并。

master.pitch.df <- merge(xPR.df,yPR.df, by = c("Player","Team"), all = True)