character exchange

I would like ask for help in character exchange in R

I got text like this:

wsp = "2019-03-11: 23.5, 19/03/12: 12.7, 2019.03.13: 11.1, 2019-marzec-14: 14.3"

I want change characters just in this section: 2019.03.13 from "." to "-",
but i don't want changing in other parts.
I tried use this code:

library("stringr")
wsp2 = str_replace_all(str_sub(wsp, start = 35, end = 44 ), pattern = "\\.", replacement = "\\-")
wsp2
"2019-03-13"

After i have just one record. I would like still have all.

There are probably better ways than this but it seems to work.

wsp = "2019-03-11: 23.5, 19/03/12: 12.7, 2019.03.13: 11.1, 2019-marzec-14: 14.3"
stringr::str_replace_all(wsp, "(\\d{4})\\.(\\d{2})\\.", "\\1-\\2-")
#> [1] "2019-03-11: 23.5, 19/03/12: 12.7, 2019-03-13: 11.1, 2019-marzec-14: 14.3"

Created on 2020-02-05 by the reprex package (v0.3.0)

1 Like

Thank you a lot.
It's really helpful

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.