rdplyr

How can I use IfElse or Case_When to solve an R problem where output needs to be put into 2 columns?


Description

I am trying to extract three digits from each value in the Pitcnt column. (See Output table). The first value (1 or 2 digits) is at the start of the string, the second value is before the dash, and the third value is after the dash. The second and third values are always just one digit. I have tried to find a solution (using dplyr) in StackOverflow that uses either the IF statement or the Case_When, but couldn't. If one exists, I would appreciate its URL.

Output

enter image description here

Dput Code

structure(list(Pitcnt = c("12 (3-2)", "7 (0-1)", "3 (0-2)", 
"8 (3-2)", "5 (2-2)", "7 (3-2)", "1 (0-0)", "2 (0-1)", "5 (1-2)", 
"10 (3-2)")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-10L))

Code I've Tried

I tried to get it to work using both IfElse and Case_When, but could not get either to do what I want because, each result of the IF statement requires that data be put into two different columns (X and Y) at the same time.

The code below shows how it can be done without using either IfElse or Case_When.

pc8 <- pc %>%
  filter(nchar(Pitcnt) == 8) %>%
  mutate(x = as.numeric(substr(Pitcnt,5,5))) %>%
  mutate(y = as.numeric(substr(Pitcnt,7,7)))

pc7 <- pc %>%   
  filter(nchar(Pitcnt) == 7) %>%
  mutate(x = as.numeric(substr(Pitcnt,4,4))) %>%
  mutate(y = as.numeric(substr(Pitcnt,6,6)))
pc7

This pseudocode shows what I want to do: If nchar(Pitcnt) = 8 then put the digit before the dash in X and the digit after the dash into Y, else if nchar(Pitcnt) = 7 then put the digit before the dash in X and the digit after the dash into Y.


Solution

  • library(tidyverse)
    
    df %>%
       separate(Pitcnt, c("Pitches", "X", "Y"), remove = FALSE,
                convert = TRUE, extra = "drop")
    
    # A tibble: 10 × 4
       Pitcnt   Pitches     X     Y
       <chr>      <int> <int> <int>
     1 12 (3-2)      12     3     2
     2 7 (0-1)        7     0     1
     3 3 (0-2)        3     0     2
     4 8 (3-2)        8     3     2
     5 5 (2-2)        5     2     2
     6 7 (3-2)        7     3     2
     7 1 (0-0)        1     0     0
     8 2 (0-1)        2     0     1
     9 5 (1-2)        5     1     2
    10 10 (3-2)      10     3     2
    

    Base R:

    cbind(df, read.table(text = gsub("\\D+", " ", df$Pitcnt), 
         col.names = c("Pitches", "X", "Y")))
    
         Pitcnt Pitches X Y
    1  12 (3-2)      12 3 2
    2   7 (0-1)       7 0 1
    3   3 (0-2)       3 0 2
    4   8 (3-2)       8 3 2
    5   5 (2-2)       5 2 2
    6   7 (3-2)       7 3 2
    7   1 (0-0)       1 0 0
    8   2 (0-1)       2 0 1
    9   5 (1-2)       5 1 2
    10 10 (3-2)      10 3 2