Inner_join输出灰烬dataframe

发布于 2025-01-25 01:25:09 字数 1667 浏览 2 评论 0原文

我有两个数据帧,我试图将其合并在一起。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

药祭#氼 2025-02-01 01:25:09

您需要在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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文