Home > OS >  Create new columns using across() and if_else()
Create new columns using across() and if_else()

Time:01-05

I have survey data that has a binary 1, 0 (indicating peak or off-peak) variable with the related peak or off-peak numbers in two separate columns.

    structure(list(q9_jul_2019 = c(1, 0, 1, 0, 1, 0), q9_aug_2019 = c(1, 
0, 1, 0, 1, 0), q9_sep_2019 = c(1, 0, 1, 0, 1, 0), q9_oct_2019 = c(0, 
0, 1, 0, 1, 0), q9_nov_2019 = c(0, 0, 1, 0, 1, 0), q9_dec_2019 = c(0, 
0, 1, 0, 0, 0), q9_jan_2020 = c(0, 0, 1, 0, 0, 0), q9_feb_2020 = c(0, 
1, 0, 1, 0, 0), q9_mar_2020 = c(1, 1, 0, 1, 0, 0), q9_apr_2020 = c(1, 
1, 1, 1, 0, 1), q9_may_2020 = c(0, 1, 0, 0, 0, 0), q9_jun_2020 = c(0, 
0, 0, 0, 0, 0), q15 = c(1, 10, 30, 0, 2, 0), q22 = c(0, 10, 6, 
0, 0, 0)), row.names = c(NA, 6L), class = "data.frame")

I have created new monthly columns that have the associated visitation numbers in that column but I'm sure there must be a neater way to do it using across(). I haven't been able to make it work though, so at the moment I'm stuck at the following:

survey <- survey %>%
  mutate(visitation_jul_19 = if_else(q9_jul_2019 == 1, q15, q22),
         visitation_aug_19 = if_else(q9_aug_2019 == 1, q15, q22),
         visitation_sep_19 = if_else(q9_sep_2019 == 1, q15, q22),
         visitation_oct_19 = if_else(q9_oct_2019 == 1, q15, q22),
         visitation_nov_19 = if_else(q9_nov_2019 == 1, q15, q22),
         visitation_dec_19 = if_else(q9_dec_2019 == 1, q15, q22),
         visitation_jan_20 = if_else(q9_jan_2020 == 1, q15, q22),
         visitation_feb_20 = if_else(q9_feb_2020 == 1, q15, q22),
         visitation_mar_20 = if_else(q9_mar_2020 == 1, q15, q22),
         visitation_apr_20 = if_else(q9_apr_2020 == 1, q15, q22),
         visitation_may_20 = if_else(q9_may_2020 == 1, q15, q22),
         visitation_jun_20 = if_else(q9_jun_2020 == 1, q15, q22))

CodePudding user response:

You may try

library(dplyr)

survey %>%
  mutate(across(q9_jul_2019:q9_jun_2020, ~ ifelse(.x == 1, q15, q22)))

  q9_jul_2019 q9_aug_2019 q9_sep_2019 q9_oct_2019 q9_nov_2019 q9_dec_2019 q9_jan_2020 q9_feb_2020 q9_mar_2020 q9_apr_2020
1           1           1           1           0           0           0           0           0           1           1
2          10          10          10          10          10          10          10          10          10          10
3          30          30          30          30          30          30          30           6           6          30
4           0           0           0           0           0           0           0           0           0           0
5           2           2           2           2           2           0           0           0           0           0
6           0           0           0           0           0           0           0           0           0           0
  q9_may_2020 q9_jun_2020 q15 q22
1           0           0   1   0
2          10          10  10  10
3           6           6  30   6
4           0           0   0   0
5           0           0   2   0
6           0           0   0   0
  •  Tags:  
  • Related