-
Notifications
You must be signed in to change notification settings - Fork 66
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
Implement vec_arg_min and vec_arg_max #86
Comments
I now think it would be useful to have: # error if size 0, requires size >0 to be able to return a single location back
vec_arg_min(x)
vec_arg_max(x)
# Returns a size 1 value that represents the maximum/minimum value for a particular ptype
# - generic
# - default would cast Inf/-Inf to the type of x
# - clock would override this to return the max/min possible date-time values
# - data frames would map(x, vec_ptype_maximum) over the columns to get a 1 row result
vec_ptype_maximum(x)
vec_ptype_minimum(x)
vec_min(x)
vec_max(x)
# the above two are implemented as:
vec_min <- function(x) {
if (vec_size(x)) {
vec_slice(x, vec_arg_min(x))
} else {
vec_ptype_minimum(x)
}
} I'm currently at a place where I'm using I'm not sure what the |
I also like that Python's So maybe its something like: vec_min <- function(x, ..., na_rm = FALSE, empty = NULL) {
check_dots_empty0(...)
if (na_rm) {
x <- vec_slice(x, !vec_equal_na(x))
}
if (is.null(empty)) {
empty <- vec_ptype_maximum(x)
} else {
empty <- vec_cast(empty, x)
vec_assert(empty, size = 1L)
}
if (vec_is_empty(x)) {
empty
} else {
vec_slice(x, vec_arg_min(x))
}
} I think the suppressPackageStartupMessages({
library(dplyr)
library(vctrs)
})
#> Warning: package 'dplyr' was built under R version 4.1.2
df <- tibble(g = c(1, 1, 2), date = new_date(c(0, NA, NA)))
df_min <- df %>%
group_by(g) %>%
summarise(date = min(date, na.rm = TRUE))
#> Warning in min.default(structure(NA_real_, class = "Date"), na.rm = TRUE): no
#> non-missing arguments to min; returning Inf
# the print method lies!
df_min
#> # A tibble: 2 × 2
#> g date
#> <dbl> <date>
#> 1 1 1970-01-01
#> 2 2 NA
# EW!
unclass(df_min$date)
#> [1] 0 Inf
is.na(df_min$date)
#> [1] FALSE FALSE
# this was actually what she wanted:
df_min <- df %>%
group_by(g) %>%
summarise(date = vec_min(date, na_rm = TRUE, empty = NA)) |
|
Should |
Probably |
Another example where this would be helpful tidyverse/dplyr#6703 |
And use in
min()
andmax()
methodsThe text was updated successfully, but these errors were encountered: