Replacing Pipes to T and A

Dear Sir Madam
A bit of urgency to replace pipes || in the appointment_key with T and A = desired outcome shown in the result column
Repex is here.
I hope you will help me in this.
Thank you very much in advance.
G

pipe <- tibble::tribble(
          ~referral_key, ~appointment_key,        ~result,
            "OP3349021", "4289||10410||1", "4289T10410A1",
            "OP3359764", "4289||10410||2", "4289T10410A2"
          )
pipe
#> # A tibble: 2 x 3
#>   referral_key appointment_key result      
#>   <chr>        <chr>           <chr>       
#> 1 OP3349021    4289||10410||1  4289T10410A1
#> 2 OP3359764    4289||10410||2  4289T10410A2

Created on 2024-09-09 with reprex v2.1.1

Hi @genghiskhan, thank you for your question.

In the below I've done two sets of str_replace() on the double pipes ||, where the first instance is replaced with T and the second with A.

library(tidyverse)
# create tibble
pipe <- tibble::tribble(
  ~referral_key, ~appointment_key,        ~result,
  "OP3349021", "4289||10410||1", "4289T10410A1",
  "OP3359764", "4289||10410||2", "4289T10410A2"
)
# replace pipes
pipe <- pipe |> 
  mutate(
    # first instance of ||
    appointment_key = str_replace(
      string = appointment_key,
      pattern = '\\|\\|',
      replacement = 'T'
    ),
    # second instance of ||
    appointment_key = str_replace(
      string = appointment_key,
      pattern = '\\|\\|',
      replacement = 'A'
    ),
    # test for equivalence
    equivalent = appointment_key == result
  )
# see the result
pipe
#> # A tibble: 2 × 4
#>   referral_key appointment_key result       equivalent
#>   <chr>        <chr>           <chr>        <lgl>     
#> 1 OP3349021    4289T10410A1    4289T10410A1 TRUE      
#> 2 OP3359764    4289T10410A2    4289T10410A2 TRUE

Created on 2024-09-09 with reprex v2.1.0

1 Like

@craig.parylo thank you so much for your prompt response. appreciated. have a nice evening. G

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.