Home > Mobile >  How to change values before text in string using R
How to change values before text in string using R

Time:12-21

I have multiple strings that are similar to the following pattern:

dat<-("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0")

I need to change all 0 values to "." before the first character value within a string. My desired output in this example would be:

"........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0".

I tried using gsub to accomplish this task:

gsub("\\G([^_\\d]*)\\d", ".\\1", dat, perl=T)

Unfortunately it changed all of the 0s to "." instead of the 0s preceding the first "A".

Can someone please help me with this issue?

CodePudding user response:

If you wish to simply replace each leading 0 with a ., you can use

gsub("\\G0", ".", dat, perl=TRUE)

Here, \G0 matches a 0 char at the start of string, and then every time after a successful match. See this regex demo.

If you need to replace each 0 in a string before the first letter you can use

gsub("\\G[^\\p{L}0]*\\K0", ".", dat, perl=TRUE)

Here, \G matches start of string or end of the preceding successful match, [^\p{L}0]* matches zero or more chars other than a letter and 0, then \K omits the matched text, and then 0 matches the 0 char and it is replaced with a .. See this regex demo.

See the R demo online:

dat <- c("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0","102030405000AZD")
gsub("\\G0", ".", dat, perl=TRUE)
## [1] "........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0"
## [2] "102030405000AZD"                                         
gsub("\\G[^\\p{L}0]*\\K0", ".", dat, perl=TRUE)
## [1] "........AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0"
## [2] "1.2.3.4.5...AZD"                                         

CodePudding user response:

This is really hard. So I tried to do it with a custom function:

library(stringr)

dat<-("00000000AAAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0")

Zero_Replacer <- function(x) {
  x <- str_split(x, '[A-Za-z]', 2)
  x[[1]][1] <- str_replace_all(x[[1]][1], "0", ".")
  paste0(x[[1]][1], x[[1]][2])
}

Zero_Replacer(dat)

Output:

[1] "........AAAAAAAAA0AAAAAAAAAA0AAAAAAAAAAAAAAAAAAAAAAAAD0"
  • Related