Sunday 21 January 2018

r - How to change the decimal places in the vector?

itemprop="text">

I'm having trouble with decimals. I
have a vector of numbers, for
example:




x <-
c(400000, 500000, 100000,
97500)


I would like to
turn them like this:



x <-
c(4000.00, 5000.00, 1000.00,
975.00)


I've tried
commands like round, format and
options, but all they do is to add more zeros to these numbers.
How to change the decimal places in the
vector?




Thanks in
advance!



Answer




You could use the function
formatC()



formatC(as.numeric(x/100),
format = 'f', flag='0', digits = 2)


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...