vctrs icon indicating copy to clipboard operation
vctrs copied to clipboard

Implement `vec_all_complete()`

Open DavisVaughan opened this issue 4 years ago • 2 comments

See https://github.com/r-lib/vctrs/pull/1428#issuecomment-902130258

  • vec_drop_missing() would be useful in vec_interleave() https://github.com/r-lib/vctrs/pull/1448/files#diff-8cc239533824402ee3430f8e3e98acb4e6c21a0aacad4bd0063b26de583a9e79R43
  • vec_slice_complete() is a nice na.omit() replacement

DavisVaughan avatar Sep 15 '21 19:09 DavisVaughan

I now feel somewhat strongly that a more flexible API is:

vec_any_missing(x)
vec_all_complete(x)

vec_detect_missing(x) (vec_equal_na)
vec_detect_complete(x)

Which get combined like:

if (vec_any_missing(x)) {
  x <- vec_slice(x, !vec_detect_missing(x))
} 

if (!vec_all_complete(x)) {
  x <- vec_slice(x, vec_detect_complete(x))
} 

That way you only pay for the logical vector creation and the slicing if you detect any missing or incomplete values.

This also gives you the flexibility to do:

if (vec_any_missing(x)) {
  abort("Can't have missing values")
}

list_drop_empty() is a special case that probably would have been better as list_detect_empty() and list_any_empty(). But it is too late for that (we can obviously make those functions with list_sizes()).

DavisVaughan avatar Sep 19 '22 20:09 DavisVaughan

We already have vec_detect_complete(), vec_equal_na(), and vec_any_missing(), so the only thing we are missing here is vec_all_complete().

DavisVaughan avatar Sep 19 '22 20:09 DavisVaughan