Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a space every three characters from the end

I need to add a space between every 3rd character in the string but from the end. Also, ignore the element, which has a percentage %.

string <- c('186527500', '3875055', '23043', '10.8%', '9.8%')

And need to get the view: 186 527 500, 3 875 055, 23 043, 10.8%, 9.8%

like image 246
Angelina T Avatar asked Sep 12 '25 07:09

Angelina T


1 Answers

You could do:

ifelse(grepl('%', string), string, scales::comma(as.numeric(string), big = ' '))
#> [1] "186 527 500" "3 875 055"   "23 043"      "10.8%"       "9.8%" 
like image 65
Allan Cameron Avatar answered Sep 13 '25 22:09

Allan Cameron