Skip to content

Add parameter to fread to read numbers with leading zeros as character #2999

@marc-outins

Description

@marc-outins

Add a parameter or option to not ignore leading zeros when reading data with fread. I have data that contains numeric looking data that has leading zeros that I would like fread to read as character. I've seen other people asking about this on stack overflow and the some of the main responses are to set colClasses = "character" so all columns are read as character or specifically call out the columns that need to be read as character. These options aren't great if there are lots of columns with this issue along with other columns that should be read in as non-character. I've never dealt with data that looks like "0300" that really represents 300 so I would by default like to read columns that contain data with leading 0's as character.

# Minimal Reproducible Example

library(data.table)

dt <- fread(input = "0300\n")
str(dt)

Classes ‘data.table’ and 'data.frame':	1 obs. of  1 variable:
 $ V1: int 300
 - attr(*, ".internal.selfref")=<externalptr>

# Output of sessionInfo()

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] doSNOW_1.0.16     snow_0.4-2        iterators_1.0.10  foreach_1.4.4     purrr_0.2.5       stringi_1.2.4    
 [7] stringr_1.3.1     fuzzyjoin_0.1.4   data.table_1.11.4 rvest_0.3.2       xml2_1.2.0       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18     compiler_3.5.1   pillar_1.3.0     bindr_0.1.1      tools_3.5.1      digest_0.6.15   
 [7] evaluate_0.11    tibble_1.4.2     pkgconfig_2.0.1  rlang_0.2.1      rstudioapi_0.7   curl_3.2        
[13] yaml_2.1.19      bindrcpp_0.2.2   dplyr_0.7.6      httr_1.3.1       knitr_1.20       hms_0.4.2       
[19] rprojroot_1.3-2  tidyselect_0.2.4 glue_1.3.0       R6_2.2.2         rmarkdown_1.10   readr_1.1.1     
[25] selectr_0.4-1    magrittr_1.5     backports_1.1.2  codetools_0.2-15 htmltools_0.3.6  assertthat_0.2.0
[31] crayon_1.3.4    

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions