What I'm trying to do here is bring in dplyr::select() semantics into a function supplied to dplyr::mutate(). Below is a minimal example.
dat <- tibble(class = rep(c("A", "B"), each = 10),
x = sample(100, 20),
y = sample(100, 20),
z = sample(100, 20))
.reorder_rows <- function(...) {
x <- list(...)
y <- as.matrix(do.call("cbind", x))
h <- hclust(dist(y))
return(h$order)
}
dat %>%
group_by(class) %>%
mutate(h_order = .reorder_rows(x, y, z))
## class x y z h_order
## <chr> <int> <int> <int> <int>
## 1 A 85 17 5 1
## 2 A 67 24 35 5
## ...
## 18 B 76 7 94 9
## 19 B 65 39 85 8
## 20 B 49 11 100 10
##
## Note: function applied across each group, A and B
What I would like to do is something along the lines of:
dat %>%
group_by(class) %>%
mutate(h_order = .reorder_rows(-class))
The reason this is important is that when dat
has many more variables, I need to be able to exclude the grouping/specific variables from the function's calculation.
I'm not sure how this would be implemented, but somehow using select semantics within the .reorder_rows
function might be one way to tackle this problem.