1

I am reading in data from a .csv file with numbers of 5 or 6 decimals using the read_csv() function. However, when I print the numbers in R they appear to be truncated.

My raw data looks like this:

4445.32433, 41.9194, 15625.68189,...
16.27793374, 3838.070873, 189.9342746

And when I print the data in R I get:

$ V3   : num  4445.32 16.28 1049.5 36.91 9.52 ...
$ V4   : num  41.9 3838.1 100.6 43.8 11.3 ...

and for other data columns, I got this:

$ V39  : num  1.02e-05 2.67e-01 1.16e-03 7.12e-04 2.14e-04 ...
$ V40  : num  0.221222 0.072993 0.004526 0.002724 0.000817 ...

Why is R truncating my data?

Am I able to prevent the truncation using read_csv()? I tried using colClasses=c("double") but seems to have no effect. Is there something else going on?

Thanks.

anthls
  • 504
  • 4
  • 12
Li haonan
  • 600
  • 1
  • 6
  • 24

3 Answers3

3

R does not truncate your data at all. Your data have been read in successfully without losing any precision. The rounding you see on the screen is just the result of print. Try df$v3[1], you will see what I mean.

Although you can control the number of digits to print, by options(digits), there is no need to do this.

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
1

The full resolution is stored by R, you are just seeing the rounded values displayed on the console. Take a look at the function: options(digits=6).

See this question for more info: Formatting Decimal places in R

Community
  • 1
  • 1
Dave2e
  • 22,192
  • 18
  • 42
  • 50
1

I had a similar problem now. I copy-pasted values from one excel file to another. In excel the values will look just fine, but when you save it as csv file, the values will only retain their shown precision points. I guess this is a bug/feature of excel - that they take the values presented from the clip-board, and not from the actual file.

My current work-around was to increase-decimal of this copy-paste column until it was good enough for my needs.

Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66