AME
AME

Reputation: 5300

Using Regular Expressions in R to change number formating

In data frame df column c1 has negative numerical values formatted like this:

(1,000,000)

I would like to remove the parentheses from the negative values in df$c1, so as to return:

-1,000,000

I am using the following command in R: df$c1<-gsub('^\\($','-',gsub(',','',df$c1))

But the output is not returning the desired effect.

How can I adjust the regular expression in this R command to return the proper formatting?

Upvotes: 1

Views: 66

Answers (2)

IRTFM
IRTFM

Reputation: 263489

Wouldn't it instead be:

df$c1<-sub('^\\(', '-' , sub('\\)$','',df$c1))

This removes leading left-parens, replacing them with minus signs, and removes trailing right-parens. Your version was insisting that the 'outer' pattern be exactly (, which I doubt would match any items, and was removing commas using the 'inner' call. I changed to sub since there was only the desire to do this once for each element.

Upvotes: 1

Julius Vainora
Julius Vainora

Reputation: 48251

gsub("\\((.+)\\)", "-\\1", "(1,000,000)")
# [1] "-1,000,000"

Upvotes: 2

Related Questions