Home > Software design >  Remove extra string characters in decimal formatting
Remove extra string characters in decimal formatting

Time:04-06

how do I remove just the ".00" in a data frame column: so that 5.00% /GP per tonne becomes 5% / GP per tonne ? I am trying something like the below but it is saying invalid regexp.


sched$duty_rate <- c("5.00% /GP per tonne","10.00% /GP per tonne")

for(i in 1:nrow(sched)){
  sched$duty_rate[i] <- gsub("[.]?[0-9]*(?=%)","\\%",sched$duty_rate[i])
}

CodePudding user response:

You can use sub, i.e.

sub('.00', '', sched$duty_rate, fixed = TRUE)
[1] "5% /GP per tonne"  "10% /GP per tonne"

CodePudding user response:

You can use

sched$duty_rate <- gsub("\\.0 (%)", "\\1", sched$duty_rate)

Details:

  • \. - a literal dot
  • 0 - one or more 0 chars
  • (%) - Group 1: a % char (so that we do not remove zeros from other contexts, only between . and %)

See the R demo:

duty_rate <- c("5.00% /GP per tonne","10.00% /GP per tonne", "5.0% /GP per 2.000 tonne","10.0000% /GP per 20.000 tonne")
gsub("\\.0 (%)", "\\1", duty_rate)
## => [1] "5% /GP per tonne"         "10% /GP per tonne"       
##    [3] "5% /GP per 2.000 tonne"   "10% /GP per 20.000 tonne"

See the regex demo.

  • Related