How to perform a calculation on numbers that are comma separated. For example
result <- 10,000 + 5,000 + 60,000
gives the error
Error: unexpected ',' in "result <- 10,"
How to perform a calculation on numbers that are comma separated. For example
result <- 10,000 + 5,000 + 60,000
gives the error
Error: unexpected ',' in "result <- 10,"
While using gsub
and format
functionality will certainly do the trick, another alternative is to use some of tidyverse
packages. The code is arguably easier to read and interpret than gsub
:
library( readr )
library( scales )
comma( parse_number("10,000") + parse_number("5,000") + parse_number("60,000") )
# [1] "75,000"
Remove commas, do your math, and then add them back. Documentation:
In R: remove commas from a field AND have the modified field remain part of the dataframe