sfirke/janitor

Feature Request: `paste_skip_na()` function that skips NA values when pasting

Closed this issue · 4 comments

I often want to paste multiple values together skipping NA values.

This could be related to #536 as it could be used for combining header values there.

This is how paste works:

paste("A", NA, sep = "_")
# [1] "A_NA"
paste(NA, NA, sep = "_")
# [1] "NA_NA"

The proposed interface would be:

paste_skip_na("A", NA, sep = "_")
# [1] "A"
paste_skip_na(NA, NA, sep = "_")
# [1] NA_character_

The collapse and recycle0 arguments of paste() should also be handled consistently with paste(). collapse would paste_skip_na() again, and I'd have to look into how the recycle0 argument works now.

I have a function that does most of this, if interested.

I'm about to submit the PR. FYI, I decided not to implement the recycle0 argument because it would not be clear how it's handled in the case of all NA values:

paste(c(), "A", c("B", "C"))
# [1] " A B" " A C"
paste(c(), "A", c("B", "C"), recycle0 = TRUE)
# character(0)

If all values were NA in a ... argument, would that mean that the entire output would be zero-length? I think that would be confusing. (Related: I've never used the recycle0 argument before.) Since it's easier to add a new feature later rather than strip it away or modify the feature, I'd prefer to see if there is a clear use case for recycle0 in this function.

Here's a great argument for this function:
image

Once it's on CRAN, maybe we should update the answer to that question. 😄

And, I did it with a slightly different method than those suggestions with the difference that you can have an NA value come out the end of the paste in the PR which I think that those solutions can't do (but I didn't study them exhaustively).