I have a list of random strings like kzyFw4hw8EOC/655
.
I want to look for /
character in a string and when there is a match add a "
character before and after the character /
Something like this:
Before = 'kzyFw4hw8EOC/655'
After = 'kzyFw4hw8EOC"/"655'
I have tried:
gsub(pattern = "/",replacement = ""/"",x = Before)
getting Error in ""/"" : non-numeric argument to binary operator.
I modified the code to include the strings in single quotes. (R requires single or double quotes for strings. I used single quotes so as not to have to escape the double quotes.)
Before <- 'kzyFw4hw8EOC/655'
After <- 'kzyFw4hw8EOC"/"655'
Using base R:
gsub.method <- gsub('/', '"/"', Before)
gsub.method == After
# [1] TRUE
or using the package stringr
from the tidydverse
:
library(stringr)
stringr.method <- str_replace(Before, '/', '"/"')
stringr.method == After
# [1] TRUE