vendredi 5 avril 2019

R: Trouble using ifelse to run multiple logical tests across dataframes to create new column

I'm trying to using the retrosheet baseball game data name the catcher for any given pitch pulled during the pitfx era. I've been using ifelse to run a series of tests to find the starting catcher. The code and errors are below -- also I intend to after getting this to work instead of printing "no" should it fail have similar ifelse tests nested in this until I work through from HmBat1Pos to HmBat9Pos...etc

atbat$starting_catcher_retro = ifelse(((atbat$date = retro_games$Date)                                       
                         & (atbat$pitcher_name = retro_games$HmStPchNm) 
                         & (atbat$num = 1) 
                         & (atbat$inning_side = "top")
                         & (retro_games$HmBat1Pos = 2)), 
                         retro_games$HmBat1ID, "no")

errors

Error in `$<-.data.frame`(`*tmp*`, date, value = c(13963, 13964, 13968,  : 
  replacement has 26726 rows, data has 2146373

I then tried to take out some of the code testing the date and still got errors. So I ran this instead

atbat$starting_catcher_retro = ifelse(((atbat$num = 1) 
                                         & (atbat$inning_side = "top")
                                         &(retro_games$HmBat1Pos = 2)), 
                                         retro_games$HmBat1ID, "no")

and got these other errors

Error in (atbat$num = 1) & (atbat$inning_side = "top") : 
  operations are possible only for numeric, logical or complex types

Aucun commentaire:

Enregistrer un commentaire