8  Variable recoding, releveling and other transformations

8.1 if_else()

8.2 case_match

replaces case_when() in IMHO because clearer syntax

d_long <- d_long |> 
  mutate(time_32v1 = case_match(time,
                                "time1" ~ -2/3,
                                c("time2", "time3") ~ 1/3),
         time_3v2 = case_match(time, 
                               "time1" ~ 0,
                               "time2" ~ -.5,
                               "time3" ~ .5))

8.3 Manipulating factors

8.3.1 Setting contrasts for factor

(helmert2 = matrix(c(-.5, .5), ncol = 1, dimnames = list(c("time1", "time2"), c("t2v1"))))
      t2v1
time1 -0.5
time2  0.5
(helmert3 = matrix(c(-2/3, 1/3, 1/3, 0, -.5, .5), ncol = 2, dimnames = list(c("time1", "time2", "time3"), c("t32v1", "t3v2"))))
           t32v1 t3v2
time1 -0.6666667  0.0
time2  0.3333333 -0.5
time3  0.3333333  0.5
contrasts(d2_long$time)
contrasts(d2_long$time) <- helmert2
contrasts(d2_long$time)