Home > Mobile >  inner_join outputs an empy dataframe
inner_join outputs an empy dataframe

Time:04-29

I have two data frames that I am trying to merge together.

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

While using inner join the output is an empty dataframe.

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)

What I am trying to achieve is something that looks like 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

CodePudding user response:

You need to rearrange names in 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
  • Related