J. Doe
J. Doe

Reputation: 199

Remove recurrent pattern in a string, but only the last time observed in each row in R

I have a dataframe somehow like this:

df <- ("test1/a/x/w/e/a/adfsadfsfads
        test2/w/s/f/x/a/saffakwfkwlwe
        test3/a/e/c/o/a/saljsfadswwoe")

The structure is always like testX/0/0/0/0/a/randomstuff while 0 is a random letter. Now I want to change the letter "a" behind the 4 random letters to a "z" in every row.

I tried a regex, but it didn't work because when I choose "/a/" as the pattern and "/z/" as the replacement, it would also replace the two "a"s at the beginning of test1 and test3.

So what I need is a function that replaces only the last pattern that is observed in each line. Is there anything that can do this?

Upvotes: 0

Views: 56

Answers (1)

s_baldur
s_baldur

Reputation: 33488

I believe this is what you are looking for:

data <- c(
  "test1/a/x/w/e/a/adfsadfsfads",
  "test2/w/s/f/x/a/saffakwfkwlwe",
  "test3/a/e/c/o/a/saljsfadswwoe"
)


gsub("a/([a-z]+)$", "z/\\1", data)
[1] "test1/a/x/w/e/z/adfsadfsfads"  "test2/w/s/f/x/z/saffakwfkwlwe"
[3] "test3/a/e/c/o/z/saljsfadswwoe"

And if you don't like regex you could use strsplit().

library(magrittr)
data %>%
  strsplit("/") %>%
  lapply(function(x) {x[6] <- "z"; x}) %>%
  sapply(paste, collapse = "/")

Upvotes: 2

Related Questions