Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

readr : Turn off scientific notation in write_csv

Tags:

I am using R to process Census data which uses really long numeric GEOIDs to identify the geographies. The issue I am facing is when writing out the processed data using write_csv (from the readr package) it is writing these GEOIDs in scientific notation. Is there a way to get around this?

Note: I am able to toggle scientific notation display on R console using by setting the scipen option to a sufficiently large value. But this setting does not seem to extend into the readr library.

Here is a toy dataset:

library(dplyr) library(readr) # which is the package with write_csv (tbl_df(data.frame(GEOID = seq(from=60150001022000, to=60150001022005, 1)))) Source: local data frame [6 x 1]             GEOID 1 60150001022000 2 60150001022001 3 60150001022002 4 60150001022003 5 60150001022004 6 60150001022005  write_csv((tbl_df(data.frame(GEOID = seq(from=60150001022000, to=60150001022005, 1)))), "test.csv") 

This is what I am getting currently. I am looking for a way to get the same numbers as above:

GEOID 6.02E+13 6.02E+13 6.02E+13 6.02E+13 6.02E+13 6.02E+13 
like image 356
sriramn Avatar asked May 20 '15 05:05

sriramn


People also ask

How do you turn off scientific notation?

(1) Right-click a cell where you want to remove scientific notation, and (2) choose Format Cells… 2. In the Format Cells window, (1) select the Number category, (2) set the number of decimal places to 0, and (3) click OK. Now the scientific notation is removed.

What is Scipen in R?

The value of scipen is the number of orders of ten smaller (i.e. decimal places preceded by 0's) required to switch to scientific notation. Decreasing the value of scipen will cause R to switch to scientific location for larger numbers. This is easier to understand with an example.


1 Answers

I'd rather suggest recoding such columns to type int, because if so write_* won't use scientific number encoding anymore. To convert all numeric columns in one pass (e.g. in case you're dealing with a counts matrix), you could do:

require(dplyr)     tbl_df = mutate_if(tbl_df, is.numeric, as.integer) 
like image 122
Holger Brandl Avatar answered Oct 10 '22 07:10

Holger Brandl