Mark Hamlin
Mark Hamlin

Reputation: 348

Use max() in dplyr mutate with columns refered to by named index

summary <- tribble(
  ~version, ~label, ~average,
  "0.1", "pagex", 10,
  "0.2", "pagex", 12,
  "0.1", "pagey", 20,
  "0.2", "pagey", 21)

summary %>% 
  spread(version, average,sep="")  %>%
  dplyr::rowwise() %>%
  mutate(slowest = max(version0.1, version0.2))

This code works fine. I want a variation of this code which will adapt any number of versions, so in the last line the version columns would need to be referred to by named indexes.

summary %>% 
  spread(version, average)  %>%
  dplyr::rowwise() %>%
  mutate(slowest = max(.[,unique(summary$version)]))

Whatever I try, ends up with a column max instead of a row max.

One option I can get to work is to separately summarise the long form data with max, and join the result onto this wide format data described above. But I would appreciate knowing how to solve the problem with a function on the wide form.

Upvotes: 1

Views: 109

Answers (1)

akrun
akrun

Reputation: 887108

Perhaps, we can use pmax as a vectorized option

library(dplyr)
library(tidyr)
summary %>%
   pivot_wider(names_from = label, values_from = average) %>% 
   transmute(version, slowest = pmax(pagex, pagey))

Or if it is the other way

summary %>% 
    pivot_wider(names_from = version, values_from = average) %>% 
    mutate(label, slowest = pmax(`0.1`, `0.2`))

If there is more than two columns

library(purrr)
summary %>% 
    pivot_wider(names_from = version, values_from = average) %>% 
    mutate(label, slowest = select(., -label) %>% reduce(pmax))

Upvotes: 1

Related Questions