Skip to content
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
Closed

na for write_csv would be cool #187

ghost opened this issue Jun 4, 2015 · 3 comments

Comments

@ghost
Copy link

ghost commented Jun 4, 2015

write_csv(..., na="")

@npjc
Copy link
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
Copy link
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
Copy link

+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 as completed 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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants