readr : Turn off scientific notation in write_csv

后端 未结 6 2011
谎友^
谎友^ 2021-02-07 02:54

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 w

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 03:22

    Use bit64, it is a S3 class for vectors for 64bit Integers

    library(dplyr)
    library(readr)
    options(digits = 22)
    tbl_df <- data.frame(GEOID = seq(from=60150001022000, to=60150001022005, 1))
    > tbl_df
               GEOID
    1 60150001022000
    2 60150001022001
    3 60150001022002
    4 60150001022003
    5 60150001022004
    6 60150001022005
    
    library(bit64)
    tbl_df$GEOID <- as.integer64(tbl_df$GEOID)
    write_csv(tbl_df,'test.csv')
    

    If you read this data again in R, it will assign the correct datatype.

    dfr <- read_csv('test.csv')
    > dfr
    Source: local data frame [6 x 1]
    
               GEOID
    1 60150001022000
    2 60150001022001
    3 60150001022002
    4 60150001022003
    5 60150001022004
    6 60150001022005 
    
    > str(tbl_df)
    'data.frame':   6 obs. of  1 variable:  
    Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   6 obs. of  1 variable:
     $ GEOID: num  6.02e+13 6.02e+13 6.02e+13 6.02e+13 6.02e+13 ...
    

    Hope this helps. I opened the csv in a text editor, the numbers had "" around them. But it still worked.

提交回复
热议问题