Skip to content

include na.omit() method (and friends) for vctrs_rcrd records class #1413

Closed

Description

@maxheld83

(courtesy of base R), vctrs_vctr plays nice with na.omit() (and friends):

library(vctrs)
base_vec <- c(2L, NA_integer_)
base_vec
#> [1]  2 NA
vctrs_vec <- c(new_vctr(2L), new_vctr(NA_integer_))
vctrs_vec
#> <vctrs_vctr[2]>
#> [1]  2 NA
na.omit(base_vec)
#> [1] 2
#> attr(,"na.action")
#> [1] 2
#> attr(,"class")
#> [1] "omit"
na.omit(vctrs_vec) # works as expected
#> <vctrs_vctr[1]>
#> [1] 2
str(na.omit(base_vec))
#>  int 2
#>  - attr(*, "na.action")= 'omit' int 2
str(na.omit(vctrs_vec)) # also retains na.action metadata
#>  vctrs_vc [1:1] 2
#>  @ na.action: 'omit' int 2

records don't:

library(vctrs)
new_rational <- function(n = integer(), d = integer()) {
  new_rcrd(list(n = n, d = d), class = "vctrs_rational")
}
# necessary to avoid error msgs, but not pertinent here
format.vctrs_rational <- function(x, ...) {
  n <- field(x, "n")
  d <- field(x, "d")
  out <- paste0(n, "/", d)
  out[is.na(n) | is.na(d)] <- NA
  out
}
vctrs_rcrd <- new_rational(c(NA_integer_, 1L, 2L), c(NA_integer_, NA_integer_, 3L))
is.na(vctrs_rcrd) # works
#> [1]  TRUE FALSE FALSE
str(na.omit(vctrs_rcrd)) # does not work out of the box
#>  vctrs_rt [1:3] <NA>, <NA>, 2/3

Perhaps a method for vctrs_rcrd could be added to support na.omit() and friends.

Activity

  1. maxheld83 commented on Jul 13, 2021

    @maxheld83
    MemberAuthor

    In my package, I ended up leaning on na.omit.data.frame() which seems to make sense by default.

    I used something like the below.
    Because of S3 registration I'm having a hard time making this fully reprexed in reprex::reprex(), sorry.

    #' @method na.fail vctrs_rational
    #' @exportS3Method stats::na.fail
    na.fail.vctrs_rational <- function(object, ...) na.fail(vec_proxy(object))
    
    #' @method na.omit vctrs_rational
    #' @exportS3Method stats::na.omit
    na.omit.vctrs_rational <- function(object, ...) {
      df <- na.omit(vec_proxy(object))
      out <- vec_restore(x = df, to = object, ...)
      attr(out, "na.action") <- na.action(df)
      out
    }
    
    #' @method na.exclude vctrs_rational
    #' @exportS3Method stats::na.exclude
    na.exclude.vctrs_rational <- function(object, ...) {
      df <- na.exclude(vec_proxy(object))
      out <- vec_restore(x = df, to = object, ...)
      attr(out, "na.action") <- na.action(df)
      out
    }

    This also maintains the na.action metadata.

    A method for na.pass() appears to be unnecessary.

    Could this just be added as methods for vctrs_rcrd?
    Happy to draft a PR about it, if I'm not completely on the wrong track here (as is likely 🙃).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions