3

I am trying to merge two dataframes in r, and this error message keeps coming up even though the variable types all should be correct.

Here is my code:

team_info <- baseballr::mlb_teams(season = 2022)
team_info_mlb <- subset(team_info, sport_name == 'Major League Baseball')
tim2 <- team_info_mlb %>% 
     rename('home_team' = club_name)
tim3 <- subset(tim2, select = c('team_full_name', 'home_team'))
new_pf <- baseballr::fg_park(yr = 2022)
new_pf <- subset(new_pf, select = c('home_team', '1yr'))
info_pf <- merge(tim3, new_pf, by = 'home_team')

The final line is where the problems happen. Let me know if anyone has advice.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
sproff22
  • 31
  • 2

1 Answers1

2

The problem is that the data have some fancy class attributes.

> class(tim3)
[1] "baseballr_data" "tbl_df"         "tbl"            "data.table"     "data.frame"    
> class(new_pf)
[1] "baseballr_data" "tbl_df"         "tbl"            "data.table"     "data.frame"   

Just wrap them in as.data.frame(). Since both data sets have the same by variable you may omit explicit specification.

info_pf <- merge(as.data.frame(tim3), as.data.frame(new_pf))
info_pf
#       home_team        team_full_name 1yr
# 1        Angels    Los Angeles Angels 102
# 2        Astros        Houston Astros  99
# 3     Athletics     Oakland Athletics  94
# 4     Blue Jays     Toronto Blue Jays 106
# 5        Braves        Atlanta Braves 105
# 6       Brewers     Milwaukee Brewers 102
# 7     Cardinals   St. Louis Cardinals  92
# 8          Cubs          Chicago Cubs 103
# 9  Diamondbacks  Arizona Diamondbacks 103
# 10      Dodgers   Los Angeles Dodgers  98
# 11       Giants  San Francisco Giants  99
# 12    Guardians   Cleveland Guardians  97
# 13     Mariners      Seattle Mariners  94
# 14      Marlins         Miami Marlins  97
# 15         Mets         New York Mets  91
# 16    Nationals  Washington Nationals  97
# 17      Orioles     Baltimore Orioles 108
# 18       Padres      San Diego Padres  96
# 19     Phillies Philadelphia Phillies  98
# 20      Pirates    Pittsburgh Pirates 101
# 21      Rangers         Texas Rangers  98
# 22         Rays        Tampa Bay Rays  89
# 23      Red Sox        Boston Red Sox 111
# 24         Reds       Cincinnati Reds 112
# 25      Rockies      Colorado Rockies 112
# 26       Royals    Kansas City Royals 108
# 27       Tigers        Detroit Tigers  94
# 28        Twins       Minnesota Twins  99
# 29    White Sox     Chicago White Sox 100
# 30      Yankees      New York Yankees  99
jay.sf
  • 60,139
  • 8
  • 53
  • 110