Skip to contents

relevel a target column by another reference column

Usage

ref_level(x, col, ref)

Arguments

x

tibble

col

target column

ref

reference column

Value

tibble

Examples


cut_level <- mini_diamond %>%
  dplyr::pull(cut) %>%
  unique()

mini_diamond %>%
  dplyr::mutate(cut = factor(cut, cut_level)) %>%
  dplyr::mutate(cut0 = stringr::str_c(cut, "xxx")) %>%
  ref_level(cut0, cut)
#> # A tibble: 100 × 8
#>    id    carat cut   clarity price     x     y cut0    
#>    <chr> <dbl> <fct> <chr>   <int> <dbl> <dbl> <fct>   
#>  1 id-1   1.02 Fair  SI1      3027  6.25  6.18 Fairxxx 
#>  2 id-2   1.51 Good  VS2     11746  7.27  7.18 Goodxxx 
#>  3 id-3   0.52 Ideal VVS1     2029  5.15  5.18 Idealxxx
#>  4 id-4   1.54 Ideal SI2      9452  7.43  7.45 Idealxxx
#>  5 id-5   0.72 Ideal VS1      2498  5.73  5.77 Idealxxx
#>  6 id-6   2.02 Fair  SI2     14080  8.33  8.37 Fairxxx 
#>  7 id-7   0.27 Good  VVS1      752  4.1   4.07 Goodxxx 
#>  8 id-8   0.51 Good  SI2      1029  5.05  5.08 Goodxxx 
#>  9 id-9   1.01 Ideal SI1      5590  6.43  6.4  Idealxxx
#> 10 id-10  0.7  Fair  VVS1     1691  5.56  5.41 Fairxxx 
#> # ℹ 90 more rows