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
<r-help at r-project.org> 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 at 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