Caitlin
Caitlin

Reputation: 37

Add decimal to last two numbers in numeric string in R

I have a numeric string where I need to add a decimal point before the last 2 numbers.

So,

3000

Becomes:

30.00

At first I thought I could use;

df$cost <- gsub("[0-9][0-9]$", ".[0-9][0-9]$", df$cost)

But that idea definitely doesn't work.

What are my options to solve this problem in R?

Upvotes: 0

Views: 1120

Answers (1)

joran
joran

Reputation: 173547

Maybe this is too naive, but how about:

format(as.numeric("1234") / 100,nsmall = 2)
[1] "12.34"

Upvotes: 1

Related Questions