Skip to content

fread/fwrite default options for NA mismatch #2281

Description

@BenoitLondon

Hi,

first thank you for this package. :)
I had a problem when saving and reading a csv with fread and fwrite

library(data.table)
dt <- data.table(a = c(NA_integer_, 1L, 2L), b = c("a", "b", NA_character_))
tmp <- tempfile()
fwrite(dt, tmp)
dt2 <- fread(tmp)
dt
#    a  b
#1: NA  a
#2:  1  b
#3:  2 NA
dt2
#    a b
#1: NA a
#2:  1 b
#3:  2  
all.equal(dt,dt2)
#[1] "Column 'b': 'is.NA' value mismatch: 0 in current 1 in target"

So I realised fread and fwrite have different default for na(-strings), so I checked read.csv and write.csv and they use the same default option.
In the end it mixes empty strings with NA which is not an expected default behavior imho.
Shouldn't fwrite use the same default option as write.csv i.e. na = "NA" or NA? so that a call to fread after fwrite would recover the same object?

Cheers,
Benoit

Activity

  1. st-pasha commented on Aug 3, 2017

    @st-pasha
    Contributor

    Appears to be the same issue as #2214

  2. BenoitLondon commented on Aug 3, 2017

    @BenoitLondon
    Author

    Thanks I saw the issue you re refering to but I thought it was a similar but different problem.

  3. MichaelChirico commented on Aug 3, 2017

    @MichaelChirico
    Member
  4. BenoitLondon commented on Aug 4, 2017

    @BenoitLondon
    Author

    Just it looked like the other issue is more involved and requires more changes than this one but all fine ;)

  5. added this to the milestone on Mar 3, 2018
  6. mattdowle commented on Mar 3, 2018

    @mattdowle
    Member

    Thanks @BenoitLondon. Agree.
    I've just checked that this is now fixed with the PR #2652 that's in discussion currently. I'll link there back to here and this issue will be closed if and when that PR is merged.

    > dt <- data.table(a = c(NA_integer_, 1L, 2L), b = c("a", "b", NA_character_))
    > tmp <- tempfile()
    > fwrite(dt, tmp)
    > dt2 <- fread(tmp)
    > dt
        a    b
    1: NA    a
    2:  1    b
    3:  2 <NA>
    > dt2
        a    b
    1: NA    a
    2:  1    b
    3:  2 <NA>
    > identical(dt,dt2)
    [1] TRUE
  7. Fablepongiste commented on Oct 22, 2018

    @Fablepongiste

    I think this is still not fixed ? Or back with an issue ?

    Should default in fwrite for NA not be ... NA ?

    > dt <- data.table(a = c(NA_integer_, 1L, 2L), b = c("a", "b", NA_character_))
    > tmp <- tempfile()
    > fwrite(dt, tmp)
    > dt2 <- fread(tmp)
    > dt
        a    b
    1: NA    a
    2:  1    b
    3:  2 <NA>
    > dt2
        a b
    1: NA a
    2:  1 b
    3:  2  
    
  8. modified the milestones: v1.11.0, 1.12.0 on Nov 29, 2018
  9. removed this from the 1.12.0 milestone on Jan 6, 2019
  10. 1 remaining item

  11. modified the milestones: 1.12.2, 1.12.4 on Feb 26, 2019
  12. modified the milestones: 1.12.4, 1.13.0 on Sep 17, 2019
  13. modified the milestones: 1.12.7, 1.12.9 on Dec 8, 2019
  14. modified the milestones: 1.13.1, 1.13.3 on Oct 17, 2020
  15. ben-schwen commented on Aug 8, 2021

    @ben-schwen
    Member

    Cannot reproduce on Ubuntu 20.04 with data.table 1.4.1

  16. modified the milestones: 1.14.3, on Jul 19, 2022
  17. modified the milestones: , 1.15.1 on Oct 29, 2023
  18. removed this from the 1.16.0 milestone on Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions