0

I am using the Lahman baseball package in R, and trying to get a players first in last name into a column of a data frame. The head(master) df where I am getting the data from:

  nameFirst  nameLast retroID
1     Bobby    Abreu abreb001
2    Dustin   Ackley ackld001  
3      Ryan    Adams adamr003
4    Jeremy  Affeldt affej001
5   Eliezer  Alfonzo alfoe002
6   Brandon    Allen alleb001

and the head(playerIndex) df where I am trying to get the name:

     data2011.BAT_ID       data2011.RUNXDIFF
1        abreb001              9.11
2        ackld001              8.63
3        adamr003             -3.40
4        affej001             -0.11
5        alfoe002             -2.52
6        alleb001             -4.66

I am currently using this code to retrieve the combined firstName and lastName from the master and add it as a new column in playerIndex:

getname <- function(id){
  playerline <- subset(master, master$retroID==id)
  name <- as.character(paste(playerline$nameFirst,playerline$nameLast))
  paste(name)
}
playerIndex$Name <- getname(playerIndex$data2011.BAT_ID)

However, this just results in

   data2011.BAT_ID  data2011.RUNXDIFF    Name
1        abreb001              9.11 Casey McGehee
2        ackld001              8.63 Casey McGehee
3        adamr003             -3.40 Casey McGehee
4        affej001             -0.11 Casey McGehee
5        alfoe002             -2.52 Casey McGehee
6        alleb001             -4.66 Casey McGehee

with "Casey McGehee" going all the way down the column (the correct data2011.BAT_ID to get this name would be mcgec001). Not sure why this is happening. Any help would be appreciated!

Ben
  • 37
  • 4
  • 2
    An easier approach would be to join on the `retroID` and `data2011.BAT_ID` columns, using either `merge` or a join function from `dplyr`. It would help people writing answers if there were some matching rows in your example datasets. – neilfws Jan 21 '22 at 02:40
  • 1
    I agree fully. Literally `merge(playerIndex, master, by.x = "data2011.BAT_ID", by.y = "retroUD", all.x = TRUE)` should work. We cannot demonstrate this, though, since your sample data has no overlaps. If that `merge` command works for you, then I suggest you read https://stackoverflow.com/q/1299871/3358272 and https://stackoverflow.com/q/5706437/3358272 about the concepts of merges/joins, since I think it is exactly what your goal here requires. (And it is by far the most efficient/robust way to do things like this.) – r2evans Jan 21 '22 at 02:53
  • `master$retroID==id` works for one id (scalar), not a vector of ids as you are giving – Karl Forner Jan 21 '22 at 10:06

0 Answers0