dplyr::mutate_at() applied to columns of similar name

This is one solution, courtesy of @cderv (#21583).

data %>% 
  gather(key, value, -group_id) %>% 
  mutate(key = str_extract(key, '.*(?=\\.)')) %>% 
  ungroup() %>% 
  group_by(group_id, key) %>% 
  summarise_if(is.numeric, mean, na.rm = TRUE) %>% 
  spread(key, value)
#> # A tibble: 6 x 3
#> # Groups:   group_id [6]
#>   group_id   whp   wht
#>      <int> <dbl> <dbl>
#> 1        1  478.  575 
#> 2        2 1021   548.
#> 3        3 1021.  548.
#> 4        4  949.  548.
#> 5        5  994.  544.
#> 6        6  997.  545.
2 Likes