<- data.frame(tag = rep(letters[1:.N], times = .group), key = rep(LETTERS[1:.group], each = .N)) %>% mutate(value = sample(1:10, .N * .group, replace = T)) dat_scaled <- dat %>% group_by(key) %>% mutate(value = value / sum(value)) %>% ungroup() } arrange_forJSD <- function(dat, .key = "key"){ dat %>% tidyr::pivot_wider(values_from = value, names_from = tag) }