Question: 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.

Answer: 
You can use a set of characters in regex for your strikes identifier by specifying them within square brackets like `[ABC]`, which will match any character `A`, `B`, or `C`. This will mean you don't have to write out each category separately. E.g.:

```
grepl("^[CFLMST]{2,}", PITCH_SEQ_TX)
#[1]  TRUE  TRUE FALSE FALSE FALSE
grepl("^B{3,}", PITCH_SEQ_TX)
#[1] FALSE FALSE FALSE  TRUE FALSE

```
Building on [@thelatemail's](https://stackoverflow.com/a/45292835/5269252) answer, combined with [@mt1022's](https://stackoverflow.com/users/3926543/mt1022) comment to use `case_when` and `mutate`, here is a fully worked example, using functions from `stringr` for the text work.

```
library(dplyr)
library(stringr)

# make vector of pitch sequence data
PITCH_SEQ_TX <- c('SSSC', 'FFBB', 'BBSSC', 'BBBSB', 'CBSFFFS')

# make another variable of the number of pitches in each sequence
# this is just so we have two variables for a df
NUM_PITCHES <- stringr::str_count(PITCH_SEQ_TX)

# make a df
BB <- dplyr::tibble(PITCH_SEQ_TX, NUM_PITCHES)

# create the recoded pitch sequence variable - PITCH_SEQ_TX_UPDATED
BB %>% 
    dplyr::mutate(PITCH_SEQ_TX_UPDATED =
               dplyr::case_when(
                   stringr::str_detect(string = PITCH_SEQ_TX, pattern = "^[CFLMST]{2,}") ~ "Two strikes",
                   stringr::str_detect(string = PITCH_SEQ_TX, pattern = "^B{3,}") ~ "Three balls"
                   )
    )

# A tibble: 5 x 3
  PITCH_SEQ_TX NUM_PITCHES PITCH_SEQ_TX_UPDATED
         <chr>       <int>                <chr>
1         SSSC           4          Two strikes
2         FFBB           4          Two strikes
3        BBSSC           5                 <NA>
4        BBBSB           5          Three balls
5      CBSFFFS           7                 <NA>

```