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!