Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct number of decimal places reading in a .csv

Tags:

r

decimal

csv

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

When I'm reading the .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 read.csv() function, but it doesn't say anything about decimal places.

like image 715
Misc Avatar asked Jul 23 '13 21:07

Misc


People also ask

How many decimal places should I show?

A suitable rule specifies up to one decimal place and up to two significant digits. When comparing group means or percentages in tables, rounding should not blur the differences between them.

Can CSV files have decimals?

CSV file, you cannot keep decimal digits directly. However, you can change the number of decimal places that are displayed for this value. For example, when the data is like 123457.78 in the underlying data, you can change decimal places as 0 to display is like 123457 to export as a . Csv.

What is the correct way of reading the decimal point?

Decimal numbers are made up of a whole number part, a decimal (or fraction) part, and the decimal point. The proper way to read a decimal number is to (1) read the whole number part, (2) read the decimal point as the word "and", and (3) read the decimal part.

How do I change the decimal separator in CSV?

Change separator when saving Excel file as CSVClick File > Options > Advanced. Under Editing options, clear the Use system separators check box. Change the default Decimal separator. As this will change the way decimal numbers are displayed in your worksheets, choose a different Thousands separator to avoid confusion.


1 Answers

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()))
Read 1 item
 num 2.15e+10
> str( read.table(text="21474836470", colClasses="integer"))
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  scan() expected 'an integer', got '21474836470'
> str( read.table(text="21474836470", colClasses="numeric"))
'data.frame':   1 obs. of  1 variable:
 $ V1: num 2.15e+10

If you don't specify a type or mode for "what", scan would assume numeric() and it would succeed.

like image 141
IRTFM Avatar answered Nov 05 '22 00:11

IRTFM