Skip to content

na for write_csv would be cool #187

Closed

Description

write_csv(..., na="")

Activity

  1. npjc commented on Jun 16, 2015

    @npjc
    Contributor

    Why not do:

    df[is.na(df)] <- ""
    write_csv()

    IMHO this is clearer to understand at a glance and you keep data output and data mutation as distinct as possible.

  2. asnr commented on Jul 11, 2015

    @asnr
    Contributor

    That's neat! I didn't know is.na worked sensibly with data frames. However, this approach doesn't seem to play well with factors:

    d <- data.frame(x = as.factor(c('foo', NA)))
    d[is.na(d)] <- " "
    #> Warning message:
    #> In `[<-.factor`(`*tmp*`, thisvar, value = " ") :
    #>   invalid factor level, NA generated

    and it also has the side effect of type casting integer, double and logical columns to character:

    e <- data.frame(i = as.integer(c(1, NA)), d = c(0.5, NA), l = c(T, NA))
    e[is.na(e)] <- " "
    str(e)
    #> 'data.frame':    2 obs. of  3 variables:
    #>  $ i: chr  "1" " "
    #>  $ d: chr  "0.5" " "
    #>  $ l: chr  "TRUE" " "

    I'm with @ckluss on this one, I think an na option would be helpful--I often get clients requesting specific missing encodings ("", " ", "NULL", but not "NA" so far). @hadley, what do you think? I'd be happy to implement it if you think it's a good idea.

  3. realAkhmed commented on Jul 13, 2015

    @realAkhmed

    +1.
    This feature is very important. Say, PostgreSQL is expecting "\N" as NULLs when importing. MySQL is looking for "NULL" as NULL and so on. One can use this (very smart!) trick suggested by @npjc above but this will coerce types.

  4. locked and limited conversation to collaborators on Sep 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions