tidyverse/forcats

last2() doesn't handle missings in y

hadley opened this issue · 2 comments

Maybe should be more like this?

last2 <- function (x, y) {
  miss <- is.na(x) | is.na(y)
  x <- x[!miss]
  y <- y[!miss]
  y[order(x)][length(y)]
}

This would not be an expected behavior IMHO, as the last value of y ordered by x can totally be NA.

However, it would be quite handy to have a na.rm option in the function. This would avoid some breaking changes too.

The point of last2() is to support fct_reorder2() which aims to order the legend in the same way as the lines on on far right of the plot. A point with missing y isn't drawn, so I think it should be ignored.