I am trying to replace characters in all elements of a string vector, where the characters are different, but always a certain distance from the beginning or the end of the string. I can use substr successfully to replace the characters from the beginning of the string. I am trying to use str_sub from the package stringr to replace the characters from the end of the sting (because it allows counting backwards with negative numbers). It replaces the character, but for all elements after the first one, it replaces everything to the right of the character with the end of the string from the first element:
> require(stringr)
> x <- c("A'B'C","E!FG@H","I$JKL&M")
> substr(x,2,2) <- ":"
> x
[1] "A:B'C" "E:FG@H" "I:JKL&M"
> str_sub(x,-2,-2) <- ":"
> x
[1] "A:B:C" "E:FG:C" "I:JKL:C"
Try this:
The reason it behaves like this is because
str_sub<-passes the results ofstr_sub(x, start, end)through str_c which is where the collapsing is going wrong for you.The function in the source code is:
So we are effectively passing three arguments to the
str_cfunction, one or more character vectors, an insertion string, and a collapse parameter (the ifelse bit). If the results of running juststr_subwithout using the assignment function are (if we already ran the first str_sub:So first we’re saving everything to the left of the symbol you want to replace, and then we’re setting a collapse parameter. The collapse parameter is kind of interesting, if you look at the docs for str_c, you’ll find that it says
So that’s exactly what’s happening here, when we replace our strings, it is adding the collapse parameter to the end of each of our strings.
But actually, this would work if the ifelse function wasn’t used, because without the ifelse,
str_sub(string, start = end + 1L)would return[1] "C" "H" "M"instead of just taking the first index, “C”.So this is why when we add a start and end value of c(-2, -2, -2) instead we can get the right answer: