Fix city abbreviations using for loop in R

68 Views Asked by At

I'm working with NBA data and have a column of city codes that is incorrect. (CHO instead of CHA, BRK instead of BKN, etc).

I'm looking to change the incorrect abbreviations to the correct one. I am very new at this.

for (x in most_recent$TM){
  if (x == "CHO"){
    x == "CHA"
  }
}
1

There are 1 best solutions below

2
On

Are you looking for something like this?

team_abb_long2short = c('Atlanta Hawks' = 'ATL',
                        'Boston Celtics' = 'BOS',
                        'Brooklyn Nets' = 'BKN',
                        'Charlotte Bobcats' = 'CHA',
                        'Chicago Bulls' = 'CHI',
                        'Cleveland Cavaliers' = 'CLE',
                        'Dallas Mavericks' = 'DAL',
                        'Denver Nuggets' = 'DEN',
                        'Detroit Pistons' = 'DET',
                        'Golden State Warriors' = 'GSW',
                        'Houston Rockets' = 'HOU',
                        'Indiana Pacers' = 'IND',
                        'Los Angeles Clippers' = 'LAC',
                        'Los Angeles Lakers' = 'LAL',
                        'Memphis Grizzlies' = 'MEM',
                        'Miami Heat' = 'MIA',
                        'Milwaukee Bucks' = 'MIL',
                        'Minnesota Timberwolves' = 'MIN',
                        'New Orleans Hornets' = 'NOH',
                        'New York Knicks' = 'NYK',
                        'Oklahoma City Thunder' = 'OKC',
                        'Orlando Magic' = 'ORL',
                        'Philadelphia 76ers' = 'PHI',
                        'Phoenix Suns' = 'PHX',
                        'Phoenix Suns' = 'PHO',
                        'Portland Trail Blazers' = 'POR',
                        'Sacramento Kings' = 'SAC',
                        'San Antonio Spurs' = 'SAS',
                        'Toronto Raptors' = 'TOR',
                        'Utah Jazz' = 'UTA',
                        'Washington Wizards' = 'WAS')
flip_named_vector = function(x){
  ret = names(x)
  names(ret) = x
  ret
}
team_abb_short2long = flip_named_vector(team_abb_long2short)

team_abb_short2long
                     ATL                      BOS                      BKN                      CHA                      CHI 
         "Atlanta Hawks"         "Boston Celtics"          "Brooklyn Nets"      "Charlotte Bobcats"          "Chicago Bulls" 
                     CLE                      DAL                      DEN                      DET                      GSW 
   "Cleveland Cavaliers"       "Dallas Mavericks"         "Denver Nuggets"        "Detroit Pistons"  "Golden State Warriors" 
                     HOU                      IND                      LAC                      LAL                      MEM 
       "Houston Rockets"         "Indiana Pacers"   "Los Angeles Clippers"     "Los Angeles Lakers"      "Memphis Grizzlies" 
                     MIA                      MIL                      MIN                      NOH                      NYK 
            "Miami Heat"        "Milwaukee Bucks" "Minnesota Timberwolves"    "New Orleans Hornets"        "New York Knicks" 
                     OKC                      ORL                      PHI                      PHX                      PHO 
 "Oklahoma City Thunder"          "Orlando Magic"     "Philadelphia 76ers"           "Phoenix Suns"           "Phoenix Suns" 
                     POR                      SAC                      SAS                      TOR                      UTA 
"Portland Trail Blazers"       "Sacramento Kings"      "San Antonio Spurs"        "Toronto Raptors"              "Utah Jazz" 
                     WAS 
    "Washington Wizards" 

team_abb_long2short
         Atlanta Hawks         Boston Celtics          Brooklyn Nets      Charlotte Bobcats          Chicago Bulls 
                 "ATL"                  "BOS"                  "BKN"                  "CHA"                  "CHI" 
   Cleveland Cavaliers       Dallas Mavericks         Denver Nuggets        Detroit Pistons  Golden State Warriors 
                 "CLE"                  "DAL"                  "DEN"                  "DET"                  "GSW" 
       Houston Rockets         Indiana Pacers   Los Angeles Clippers     Los Angeles Lakers      Memphis Grizzlies 
                 "HOU"                  "IND"                  "LAC"                  "LAL"                  "MEM" 
            Miami Heat        Milwaukee Bucks Minnesota Timberwolves    New Orleans Hornets        New York Knicks 
                 "MIA"                  "MIL"                  "MIN"                  "NOH"                  "NYK" 
 Oklahoma City Thunder          Orlando Magic     Philadelphia 76ers           Phoenix Suns           Phoenix Suns 
                 "OKC"                  "ORL"                  "PHI"                  "PHX"                  "PHO" 
Portland Trail Blazers       Sacramento Kings      San Antonio Spurs        Toronto Raptors              Utah Jazz 
                 "POR"                  "SAC"                  "SAS"                  "TOR"                  "UTA" 
    Washington Wizards 
                 "WAS"