lundi 24 juillet 2017

Alternative to Nested If Else Statements

I'm working with Retrosheet baseball data in R and am trying to create a separate column (using the mutate function in dplyr) that alerts me whether or not a single string from an existing column began with either "two strikes" or "three balls." For example:

PITCH_SEQ_TX <- c('SSSC', 'FFBB', 'BBSSC', 'BBBSB', 'CBSFFFS')

Retrosheet developers list only one character for balls("b"), but several for strikes (i.e. "c", "f", "l", "m", "s", "t"). Thus if I wanted to gather from the above line which sequences began with two strikes or three balls I would use:

PITCH_SEQ_TX_Updated <- mutate(PITCH_SEQ_TX, Cutoff = ifelse(grepl("^BBB", PITCH_SEQ_TX), 
"Three Balls", ifelse(grepl("^SS", updated_PITCH_SEQ_TX), "Two Strikes", 
ifelse(grepl("^FF", PITCH_SEQ_TX), "Two Strikes", "NA"))))

HOWEVER there are too many different combinations of two strike counts (i.e cc, cs, ff, etc.) which have prevented me from using nested if else statements over the entire dataset (I get an Error: contextstack overflow at line 17 warning when going past 50 different combinations). Is there an alternative function that would let me condense my existing code whether using ifelse or some other function that would allow me a way around this problem? Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire