0

I am a beginner in R so I am sorry if this is too basic. It seems this topic has been covered at How to read data when some numbers contain commas as thousand separator? but I am not sure why I am losing decimals in some examples and not in others.

Specifically, Here's the first example where it works well:

Number2<-c(2345.23568,234.23)
Dollar_Number2<-scales::dollar_format()(Number2)
as.numeric(gsub('[$,]', '', Dollar_Number2))
#output: [1] 2345.24  234.23

However, this one doesn't.

Number1<-c(234523423423.568,234.23)
Dollar_Number1<-scales::dollar_format()(Number1)
as.numeric(gsub('[$,]', '', Dollar_Number1))
#output: [1] 234523423424          234

I have checked options() for digits, and it seems okay. I'd appreciate any thoughts.

Community
  • 1
  • 1
watchtower
  • 4,140
  • 14
  • 50
  • 92
  • You lost you precision in the `Dollar_Number1<-scales::dollar_format()(Number1)` stage. What is this stage for anyway? Eitherway, you could specify the `largest_with_cents` parameter to be larger as in `Dollar_Number1<-scales::dollar_format(largest_with_cents = 1e15)(Number1)` – David Arenburg Jan 12 '17 at 15:52
  • @David - Thanks for your question. This is an excerpt from other code. I am using above code to display the numbers in Excel. Once this is done, I am then changing the vector back to numeric for further analysis. I am unclear why this is happening in one example and not the other? – watchtower Jan 12 '17 at 15:56
  • Because `largest_with_cents` is set to `1e+05`, see my comment above on how to solve this – David Arenburg Jan 12 '17 at 15:59
  • @Thanks. I tried the code you have posted. `Dollar_Number1<-scales::dollar_format(largest_with_cents = 1e15)(Number1) as.numeric(gsub('[$,]', '', Dollar_Number1))`, but here's what I got: `2.345234e+11 2.342300e+02`. I don't see the decimals. How can I fix this? – watchtower Jan 12 '17 at 16:04
  • 1
    Run `options(scipen = 20)` and then try again – David Arenburg Jan 12 '17 at 16:08
  • @David. Ok. This works. Just for my understanding, the issue was with displaying scientific notation then. By setting scipen=20, we forced R to not display scientific notation. If you can type the solution you have mentioned in the comment, I can accept it as an answer. – watchtower Jan 12 '17 at 16:23
  • 1
    You had two issues. The last one was indeed with scientific notation – David Arenburg Jan 12 '17 at 16:24

0 Answers0