mutate multiple columns at once: how i can mutate the columns above with the conditions described in a more elegant way ?

#Code comment
by_year <- combined_pivot %>%
  mutate('202011' = case_when(Fiscal_Month<= 202011 ~ `Sum of Negative Expired seats`,
                              TRUE ~ 0),
         '202012' = case_when(Fiscal_Month<= 202012 ~ `Sum of Negative Expired seats`,
                              TRUE ~ 0),
         '202101' = case_when(Fiscal_Month<= 202101 ~ `Sum of Negative Expired seats`,
                              TRUE ~ 0),
         '202102' = case_when(Fiscal_Month<= 202102 ~ `Sum of Negative Expired seats`,
                              TRUE ~ 0),
         '202103' = case_when(Fiscal_Month<= 202103 ~ `Sum of Negative Expired seats`,
                              TRUE ~ 0),
         '202104' = case_when(Fiscal_Month<= 202104 ~ `Sum of Negative Expired seats`,
                              TRUE ~ 0),
         '202105' = case_when(Fiscal_Month<= 202105 ~ `Sum of Negative Expired seats`,
                              TRUE ~ 0))

I am not sure what your original data looks like, but the column names seems from the "unique" value of one column, consider

pivot_wider ()

1 Like