1

I have the following data, where I have calculated the tercile for each value in x:

dc <- data.frame(x = c(1, 2, 5, 6, 8, 9))
dc$tercile <- fabricatr::split_quantile(dc$x, 3)

x tercile
1       1
2       1
5       2
6       2
8       3
9       3

I want to reflect the ranges in x for each tercile. The desired output:

x       tercile
1    period 1-2
2    period 1-2
5    period 5-6
6    period 5-6
8    period 8-9
9    period 8-9

A tidyverse solution is preferred. Thanks!

Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
Cloft X
  • 141
  • 7

1 Answers1

3

You can use sprintf() or paste() by groups of tercile.

library(dplyr)

dc %>%
  mutate(range = sprintf("period %d-%d", min(x), max(x)),
         .by = tercile)

#   x tercile      range
# 1 1       1 period 1-2
# 2 2       1 period 1-2
# 3 5       2 period 5-6
# 4 6       2 period 5-6
# 5 8       3 period 8-9
# 6 9       3 period 8-9
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51
  • This works when I use `group_by(tercile)` instead of `.by = tercile`. Any idea why? And what is the difference between the two methods? – Cloft X Jul 25 '23 at 09:54
  • 3
    @CloftX `.by = tercile` is a new feature after `dplyr v1.1.0`. It's a shortcut of `df %>% group_by(tercile) %>% mutate(...) %>% ungroup()`. You need to update `dplyr` version to activate it. – Darren Tsai Jul 25 '23 at 09:58