Re: [R] String replace

2019-04-03 Thread Ivan Krylov
On Wed, 3 Apr 2019 15:01:37 +0100
Graham Leask via R-help  wrote:

Suppose that `BHC$Date` contains a string "M_24".

You do:

> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_2" ,
> "01-04-2017"))

before you have a chance to do:

> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date ,
> "M_24" , "01-02-2019"))

So now it has "01-04-20174" because the first expression had a
successful match and already made a replacement.

help() for stringi::stringi-search-regex says that look-around
expressions are supported, so one of the ways to prevent this would be
to modify your patterns to look like e.g. 'M_2(?!\\d)' to match
'M_2' that is *not* followed by a digit.

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] String replace

2019-04-03 Thread Sarah Goslee
Try reversing the order, or extending the to-replace bit.

"M_1" is finding and replacing "M_10", "M_11", etc.
"M_2" is finding and replacing "M_20", "M_21", etc.

So M_14 becomes "01-03-20174"

In the future, a toy dataset would help with the reproducible example
aspect, and make your question easier to answer.

Sarah

On Wed, Apr 3, 2019 at 1:24 PM Graham Leask via R-help
 wrote:
>
>
> I’m attempting to replace a string variable that normally works fine. However 
> when trying to
> do this with a string in the form of a date the output becomes corrupted.
>
> See below:
>
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_1" , 
> "01-03-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_2" , 
> "01-04-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_3" , 
> "01-05-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_4" , 
> "01-06-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_5" , 
> "01-07-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_6" , 
> "01-08-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_7" , 
> "01-09-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_8" , 
> "01-10-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_9" , 
> "01-11-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_10" , 
> "01-12-2017"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_11" , 
> "01-01-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_12" , 
> "01-02-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_13" , 
> "01-03-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_14" , 
> "01-04-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_15" , 
> "01-05-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_16" , 
> "01-06-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_17" , 
> "01-07-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_18" , 
> "01-08-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_19" , 
> "01-09-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_20" , 
> "01-10-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_21" , 
> "01-11-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_22" , 
> "01-12-2018"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_23" , 
> "01-01-2019"))
> BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_24" , 
> "01-02-2019"))
>
> The first line works fine but by the end the output is corrupted to
>
> "01-03-20174"
>
>
> Any thoughts what I’m doing wrong here?
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Sarah Goslee (she/her)
http://www.numberwright.com

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] String replace

2019-04-03 Thread Graham Leask via R-help


I’m attempting to replace a string variable that normally works fine. However 
when trying to
do this with a string in the form of a date the output becomes corrupted.

See below:

BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_1" , 
"01-03-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_2" , 
"01-04-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_3" , 
"01-05-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_4" , 
"01-06-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_5" , 
"01-07-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_6" , 
"01-08-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_7" , 
"01-09-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_8" , 
"01-10-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_9" , 
"01-11-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_10" , 
"01-12-2017"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_11" , 
"01-01-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_12" , 
"01-02-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_13" , 
"01-03-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_14" , 
"01-04-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_15" , 
"01-05-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_16" , 
"01-06-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_17" , 
"01-07-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_18" , 
"01-08-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_19" , 
"01-09-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_20" , 
"01-10-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_21" , 
"01-11-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_22" , 
"01-12-2018"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_23" , 
"01-01-2019"))
BHC <-BHC %>% mutate ( Date = stringr :: str_replace ( Date , "M_24" , 
"01-02-2019"))

The first line works fine but by the end the output is corrupted to

"01-03-20174"


Any thoughts what I’m doing wrong here?

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.