r-lib / slider

Sliding Window Functions
https://slider.r-lib.org
Other
296 stars 12 forks source link

Consider computing ranks with `ties = "min"` #130

Closed DavisVaughan closed 3 years ago

DavisVaughan commented 3 years ago

I think that we can simplify the comparison handling by combining .i, .before, and .after into 1 vector, computing the rank with ties = "min", then splitting it back apart into the 3 vectors, which are now simple integers. That would be much easier to handle at the C level, and could simplify handling of complex .i like clock types

DavisVaughan commented 3 years ago
i <- as_naive_time(as.Date("2019-01-01") + 0:5)

before <- i - 5
after <- i + 5

combined <- c(i, before, after)
combined

size <- length(i)
seq1 <- seq_len(size)
seq2 <- seq1 + size
seq3 <- seq2 + size

seq1
seq2
seq3

slider_dense_rank <- function(x) {
  # na_equal = FALSE because we know there will be no missing values
  vec_match(x, vec_sort(vec_unique(x)), na_equal = FALSE)
}

ranks <- slider_dense_rank(combined)

vec_chop(ranks, list(seq1, seq2, seq3))

this is dense rank actually