New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

na for write_csv would be cool #187

Closed
ghost opened this Issue Jun 4, 2015 · 3 comments

Comments

Projects
None yet
3 participants
@ghost

ghost commented Jun 4, 2015

write_csv(..., na="")

@npjc

This comment has been minimized.

Contributor

npjc commented Jun 16, 2015

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.

@asnr

This comment has been minimized.

Contributor

asnr commented Jul 11, 2015

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.

@realAkhmed

This comment has been minimized.

realAkhmed commented Jul 13, 2015

+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.

@hadley hadley closed this in 76aaa54 Sep 3, 2015

@lock lock bot locked and limited conversation to collaborators Sep 25, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.