我有兩個資料框,我試圖將它們合并在一起。
df1
PlayerName Date playerid Opp Team HomeAway IP H R ER HR BB IBB WP BK SO exLI
1: Sandy Alcantara 2022-04-26 18684 @WSN MIA A 6.0 6 1 1 0 3 0 0 0 5 1.0304
2: Sandy Alcantara 2022-04-20 18684 STL MIA H 8.0 4 0 0 0 1 0 0 0 6 2.2004
3: Sandy Alcantara 2022-04-14 18684 PHI MIA H 6.1 7 2 2 0 1 0 0 0 5 1.1064
4: Sandy Alcantara 2022-04-08 18684 @SFG MIA A 5.0 3 3 2 1 5 0 0 0 4 0.2488
df2
Date PlayerName GIDP
1 2022-04-14 Alcantara, Sandy 1
2 2022-04-20 Alcantara, Sandy 1
3 2022-04-26 Alcantara, Sandy 2
使用內連接時,輸出是一個空資料框。
game_logs <- inner_join(df1, df2, by = c("PlayerName", "Date"))
[1] Date PlayerName GIDP playerid Opp Team HomeAway IP H R ER HR BB IBB WP BK SO exLI
<0 rows> (or 0-length row.names)
我想要實作的是看起來像 df3
df1
PlayerName Date playerid Opp Team HomeAway IP H R ER HR BB IBB WP BK SO exLI GIDP
1: Sandy Alcantara 2022-04-26 18684 @WSN MIA A 6.0 6 1 1 0 3 0 0 0 5 1.0304 2
2: Sandy Alcantara 2022-04-20 18684 STL MIA H 8.0 4 0 0 0 1 0 0 0 6 2.2004 1
3: Sandy Alcantara 2022-04-14 18684 PHI MIA H 6.1 7 2 2 0 1 0 0 0 5 1.1064 1
4: Sandy Alcantara 2022-04-08 18684 @SFG MIA A 5.0 3 3 2 1 5 0 0 0 4 0.2488 NA
uj5u.com熱心網友回復:
您需要重新排列 df2 中的名稱:
df1 %>%
inner_join(mutate(df2, PlayerName = sub('(\\w ), (\\w )', "\\2 \\1",PlayerName)),
by = c("PlayerName", "Date"))
PlayerName Date playerid Opp Team HomeAway IP H R ER HR BB IBB WP BK SO exLI GIDP
1 Sandy Alcantara 2022-04-26 18684 @WSN MIA A 6.0 6 1 1 0 3 0 0 0 5 1.0304 2
2 Sandy Alcantara 2022-04-20 18684 STL MIA H 8.0 4 0 0 0 1 0 0 0 6 2.2004 1
3 Sandy Alcantara 2022-04-14 18684 PHI MIA H 6.1 7 2 2 0 1 0 0 0 5 1.1064 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467211.html
