Misc - 7 months ago 45
R Question

# Correct number of decimal places reading in a .csv

I have a .csv where one of the columns contains numbers that have 7 decimal places, e.g.:

-117.2403266
.

.csv
into R it only ever shows 4 decimal places for that column, e.g.:
-117.2403
. Or maybe they are all there but when I print it only shows the four decimal places?

I thought that this might be solved within the arguments of the
function, but it doesn't say anything about decimal places.

read.csv is not truncating or rounding, but your print.data.frame function is only displaying the values to the precision specified in options(). Try:

print(dfrm, digits=10)

> dfrm<- data.frame(test=-117.2403266)
> print(dfrm)
test
1 -117.2403
> print(dfrm, digits=10)
test
1 -117.2403266

Using format as suggested would show that the precision has not been lost, but it would return a character vector, so it might not be suitable for assignment when a numeric value was expected.

Edit of a 2 yr-old post: This topic might bring up the question regarding how integers can be imported when they are larger than .Machine\$integer.max #[1] 2147483647, since such they can now be internally stored exactly as 'numeric'-abscissa values, so that maximum would be 2^52 (or 2^53-1, I forget which it is). When these are read in from a scan-based function (as are all 0f the read.*-family), you would need to declare as 'numeric' rather than 'integer':

> str( scan(text="21474836470", what=integer()))
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
scan() expected 'an integer', got '21474836470'
> str( scan(text="21474836470", what=numeric()))