As.Numeric() Removes Decimal Places in R, How to Change

as.numeric() removes decimal places in R, how to change?

R is basically following some basic configuration settings for printing the number of required digits. You can change this with the digits option as follows:

> options(digits=9)
> y <- as.character("0.912345678")
> as.numeric(y)
[1] 0.912345678

Small EDIT for clarity:
digits corresponds to the number of digits to display in total, and not just the number of digits after the comma.

For example,

> options(digits=9)
> y <- as.character("10.123456789")
> as.numeric(y)
[1] 10.1234568

In your example above the leading zero before the comma is not counted, this is why 9 digits was enough to display the complete number.

Formatting Decimal places in R

Background: Some answers suggested on this page (e.g., signif, options(digits=...)) do not guarantee that a certain number of decimals are displayed for an arbitrary number. I presume this is a design feature in R whereby good scientific practice involves showing a certain number of digits based on principles of "significant figures". However, in many domains (e.g., APA style, business reports) formatting requirements dictate that a certain number of decimal places are displayed. This is often done for consistency and standardisation purposes rather than being concerned with significant figures.

Solution:

The following code shows exactly two decimal places for the number x.

format(round(x, 2), nsmall = 2)

For example:

format(round(1.20, 2), nsmall = 2)
# [1] "1.20"
format(round(1, 2), nsmall = 2)
# [1] "1.00"
format(round(1.1234, 2), nsmall = 2)
# [1] "1.12"

A more general function is as follows where x is the number and k is the number of decimals to show. trimws removes any leading white space which can be useful if you have a vector of numbers.

specify_decimal <- function(x, k) trimws(format(round(x, k), nsmall=k))

E.g.,

specify_decimal(1234, 5)
# [1] "1234.00000"
specify_decimal(0.1234, 5)
# [1] "0.12340"

Discussion of alternatives:

The formatC answers and sprintf answers work fairly well. But they will show negative zeros in some cases which may be unwanted. I.e.,

formatC(c(-0.001), digits = 2, format = "f")
# [1] "-0.00"
sprintf(-0.001, fmt = '%#.2f')
# [1] "-0.00"

One possible workaround to this is as follows:

formatC(as.numeric(as.character(round(-.001, 2))), digits = 2, format = "f")
# [1] "0.00"

Convert string to numeric defining the number of decimal digits

You didn't actually lose the digits; that's just how it is being printed to the console:

options(digits = 4)
R> as.numeric(res[[1]][1])
#[1] 44.52
##
options(digits = 12)
R> as.numeric(res[[1]][1])
#[1] 44.524768336

As pointed out in the comments by David Arenburg you can also use print(..., digits = <n>).

Convert character to number without the loss of decimal in R

All of the digits are still there. That is just the default printing method.

new.vec <- as.numeric(f1)
print(new.vec, digits=20)
#[1] 402313.01399128098 402896.30908451998 403165.63827027398
#[4] 403169.06116369402 402891.96329268801 402239.74412469601
#[7] 402271.54950727499 402313.01399128098

You can also change the default with options(digits = n).

More information here.

as.numeric is rounding off values

I think this is a representation problem, not an actual rounding problem ...

options("digits") ## 7

From ?options:

‘digits’: controls the number of digits to print when printing numeric values. It is a suggestion only. Valid values are
1...22 with default 7. See the note in ‘print.default’ about
values greater than 15.

digits can be reset either on a one-off basis, i.e. print(object,digits=...), or globally, i.e. options(digits=20) (20 is probably overkill but helps you see what's happening: based on the results below, 10 might serve your needs well.)

as.numeric(data1$colC)
[1] 4023108 3180863 2558778 2393736 1333148 1275627
print(as.numeric(data1$colC),digits=10)
[1] 4023107.87 3180863.42 2558777.81 2393736.25 1333148.48 1275627.13
print(as.numeric(data1$colC),digits=20)
[1] 4023107.8700000001118 3180863.4199999999255 2558777.8100000000559
[4] 2393736.2500000000000 1333148.4799999999814 1275627.1299999998882

Formatting decimal places in a character column. as.numeric erase the values in the column

I assume you are somewhere which uses a comma for a decimal point, and perhaps a decimal point in place of a thousands separator.

As an example:

df <- c(',958229561278528615818098193915712388824', '2,05561009284393218251509777394193942492', '2,72096803821411321343605598060792704404', '2,00324997757400185789440370684992098409')

First, remove any decimal points, because they may be thousands separators. Then, replace the comma with a decimal point:

as.numeric(gsub(',', '.', gsub('\\.', '', df)))

Edit: however, if you intend to use more than the first few decimal places, you may run into problems with precision. Look into the package Rmpfr if you need arbitrary precision.



Related Topics



Leave a reply



Submit