gagolews / stringi

Fast and portable character string processing in R (with the Unicode ICU)
https://stringi.gagolewski.com/
Other
304 stars 44 forks source link

Should str_c etc have na.rm = TRUE? #289

Closed hadley closed 6 years ago

hadley commented 6 years ago

Since it treats NAs like other summary functions.

gagolews commented 6 years ago

Hmm... paste() behaves more like a vectorized arithmetic operator, say, +, and not an aggregation function, say sum()

So I don't think this function should get rid of any strings

Maybe you mean the stri_flatten() function?

hadley commented 6 years ago

Except when collapse is non-NULL. And pmin() and pmax() have na.rm arguments

gagolews commented 6 years ago

Thanks for pointing out that

> pmax(c(NA, 1), c(5, 5))
[1] NA  5
> pmax(c(NA, 1), c(5, 5), na.rm=TRUE)
[1] 5 5
gagolews commented 6 years ago

I'm adding this to stri_flatten

> stri_flatten(c(NA, "", "A", "", "B", NA, "C"), collapse=",", na_empty=TRUE, omit_empty=TRUE)
[1] "A,B,C"
> stri_flatten(c(NA, "", "A", "", "B", NA, "C"), collapse=",", na_empty=TRUE, omit_empty=FALSE)
[1] ",,A,,B,,C"
> stri_flatten(c(NA, "", "A", "", "B", NA, "C"), collapse=",", na_empty=FALSE, omit_empty=TRUE)
[1] NA
gagolews commented 6 years ago

And I added stri_remove_empty and stri_na2empty funs

AndreMikulec commented 6 years ago

Thanks, My earlier concept had a somewhat similar idea. " Safe concatenation/comparison operator #292 " The original idea was because if I do I concatination

... string ... + if(){   } + ... string ...

and "if()" is not TRUE, then, if(){ } silently returns a NULL, and that NULL wreaks havoc in string concatination.