Home > Net >  A way to strsplit and replace all of one character with several variations of alternate strings?
A way to strsplit and replace all of one character with several variations of alternate strings?

Time:03-12

I am sure there is a simple solution and I am just getting too frustrated to work through it but here is the issue, simplified:

I have a string, ex: AB^AB^AB^^BAAA^^BABA^ I want to replace the ^s (so, 7 characters in the string), but iterate through many variants and be able to retain them all as strings

for example:

  • replacement 1: CCDCDCD to get: ABCABCABDCBAAADCBABAD

  • replacement 2: DDDCCCD to get: ABDABDABDCBAAACCBABAD

I imagine strsplit is the way, and I would like to do it in a for loop, any help would be appreciated!

CodePudding user response:

The positions of the "^" can be found using gregexpr, see tmp

x <- "AB^AB^AB^^BAAA^^BABA^"
y <- c("CCDCDCD", "DDDCCCD")
tmp <- gregexpr(pattern = "^", text = x, fixed = TRUE)

You can then split the 'replacements' character by character using strsplit, this gives a list. Finally, iterate over that list and replace the "^" with the characters from your replacements one after the other.

sapply(strsplit(y, split = ""), function(i) {
  `regmatches<-`("AB^AB^AB^^BAAA^^BABA^", m = tmp, value = i)
})

Result

# [1] "ABCABCABCCBAAACCBABAC" "ABDABDABDDBAAADDBABAD"

CodePudding user response:

You don't really need a for loop. You can strplit your string and pattern, and then replace the "^" with the vector.

str <- unlist(strsplit(str, ""))
pat <- unlist(strsplit("CCDCDCD", ""))

str[str == "^"] <- pat
paste(str, collapse = "")
# [1] "ABCABCABDCBAAADCBABAD"

CodePudding user response:

An option is also with gsubfn

f1 <- Vectorize(function(str1, str2) {
     p <- proto(fun = function(this, x)  substr(str2, count, count))
     gsubfn::gsubfn("\\^", p, str1)         
         
     })

-testing

> unname(f1(x, y))
[1] "ABCABCABDCBAAADCBABAD" "ABDABDABDCBAAACCBABAD"

data

x <- "AB^AB^AB^^BAAA^^BABA^"
y <- c("CCDCDCD", "DDDCCCD")

CodePudding user response:

Given x <- "AB^AB^AB^^BAAA^^BABA^" and y <- c("CCDCDCD", "DDDCCCD"), we can try utf8ToInt intToUtf8 replace like below

sapply(
  y,
  function(s) {
    intToUtf8(
      replace(
        u <- utf8ToInt(x),
        u == utf8ToInt("^"),
        utf8ToInt(s)
      )
    )
  }
)

which gives

                CCDCDCD                 DDDCCCD 
"ABCABCABDCBAAADCBABAD" "ABDABDABDCBAAACCBABAD"
  • Related