Reputation: 129
I am trying to replace comma separator to a dot separator (and vice versa) in numbers, while lines may have other dots and commas. I tried using gsub(), but don't know how to stop it from recognizing replacement pattern as a character string.
For example, suppose I have a line
`Today I bought apples, oranges for 3,55 dollars`
where I want to replace comma separator to a dot separator at the price:
`Today I bought apples, oranges for 3.55 dollars`
A naive gsub
approach doesnt work as I want to:
`gsub('[[:digit:]],[[:digit:]]',
'[[:digit:]][.][[:digit:]]',
'Today I bought apples, oranges for 3,55 dollars')`
which obviously gives [1] "Today I bought apples, oranges for [[:digit:]][.][[:digit:]]5 dollars"
, i.e. gsub treats the replacement as a character line.
I cannot supply anything other than a string to gsub()
. What can I do then? I have in mind that maybe I can write a function that will match this pattern, then basically replace ,
to .
inside this pattern and place it back in the sentence. However, I hope there is a shorter solution for such a problem. Any ideas?
Upvotes: 0
Views: 1502
Reputation: 70336
You can use capture groups and reference them in the replacement:
gsub("(\\d+),(\\d+)", "\\1.\\2", "Today I bought apples, oranges for 3,55 dollars")
# [1] "Today I bought apples, oranges for 3.55 dollars"
Upvotes: 3
Reputation: 2283
You can group each digit with parenthesis and recall them in the second part of the substitution. Below \\1
recalls the first set of parenthesis and \\2
the second set.
gsub('([[:digit:]]),([[:digit:]])','\\1.\\2','Today I bought apples, oranges for 3,55 dollars')
# [1] "Today I bought apples, oranges for 3.55 dollars"
Upvotes: 1
Reputation: 887951
We can use a regex lookaround to match the ,
before a numeric and a numeric value and replace it with .
gsub("(?<=[0-9]),(?=[0-9])", ".", str1, perl = TRUE)
#[1] "Today I bought apples, oranges for 3.55 dollars"
str1 <- 'Today I bought apples, oranges for 3,55 dollars'
Upvotes: 2