Do you know of an elegant solution to the case, where you have a numeric subset of columns in your data and then you want a new column, which holds the column name of the max of the aforementioned subset?
Example:
# Library
library("tidyverse")
# Make some data
set.seed(247350)
d <- tibble(id = str_c("id_", 1:20),
v1 = rnorm(20),
v2 = rnorm(20),
v3 = rnorm(20),
v4 = rnorm(20),
v5 = rnorm(20))
# Non-elegant solution 1 - rowwise
d %>%
select(v1:v5) %>%
rowwise %>%
mutate(is_max = colnames(d)[which.max(c(v1, v2, v3, v4, v5))+1]) %>%
ungroup
# Non-elegant solution 2 - pivotting
d %>%
pivot_longer(cols = -id, names_to = "variable", values_to = "value") %>%
group_by(id) %>%
mutate(is_max = variable[which.max(value)]) %>%
ungroup %>%
pivot_wider(names_from = variable, values_from = value)