I would like to reformat a factor vector so the figures that it contains have a thousand separator. The vector contains integer and real number without any particular rule with respect to the values or order.
Data
In particular, I'm working with a vector vec
similar to the one generated below:
content <- c("0 - 100", "0 - 100", "0 - 100", "0 - 100",
"150.22 - 170.33",
"1000 - 2000","1000 - 2000", "1000 - 2000", "1000 - 2000",
"7000 - 10000", "7000 - 10000", "7000 - 10000", "7000 - 10000",
"7000 - 10000", "1000000 - 22000000", "1000000 - 22000000",
"1000000 - 22000000",
"44000000 - 66000000.8989898989")
vec <- factor(x = content, levels = unique(content))
Desired results
My ambition is to reformat this vector so the figures contain the Excel-like 1,000 separataor, as in the example below:
100.00 1,000.00
1,000,000.00
1,000,000.56
24,564,000,000.56
Tried approach
I was thinking of making use of the gsubfn
and a proto object that would pass the digit. Then maybe createing another proto object with 3 digits and replacing. As suggested in the code below:
gsubfn(pattern = "[0-9][0-9][0-9]", replacement = ~paste0(x, ','),
x = as.character(vec))
This works only partuially as comma is insterted in:
"150,.22 - 170,.33"
which obviously is wrong. I also had to convert the character vector to factor. Consquently, my question boils down to two elements:
- How can I work around the comma issue?
- How can I maintain the original structure of the factor? - I need to have a factor vector ordered in the same manner as the original one but with commas in right places.
Use positive lookahead based regex...
Maybe you can use
formatC
:Operating only on the
levels
seem to keep your precision level, not converting your vector tocharacter
vector and much more efficient as it is reducing the size of the data you operate on only to the unique values (rather the whole vector)