automatization of a sum

Hi, I have a dataset that looks like this
store week year
1 1 2017
1 2 2017
1 1 2018
1 2 2018
1 1 2019
1 2 2019
2 1 2017
2 2 2017
2 1 2018
2 2 2018
1 1 2019
1 2 2019
My goal is to unifiy that week variable so that i won't need my year variable anymore, i want for each store the week number to be 1 - 2 (for 2017)- 3 - 4(for 2018) - 5 - 6(for 2019)
well I succeeded in doing that, but i'm absolutely not statisfied, I would like to know if there a more automatized and better way to do it
Here is my code :

data_c4_0 = data_c6 %>% dplyr::filter(year==2017)
data_c4_1 = data_c6 %>% dplyr::filter(year==2018) %>% mutate(week = week + 2)
data_c4_2 = data_c6 %>% dplyr::filter(year==2019) %>% mutate(week = week + 4)

data_c44 = rbind(data_c4_0,data_c4_1)
data_c6 = rbind(data_c44,data_c4_2)

Thanks in advance

library(tidyverse)
data6 <- data.frame(store =c(rep(1,6),rep(2,6)),
week  =rep(c(1,2),6),
year= rep(c(rep(2017,2),rep(2018,2),rep(2019,2)),2))

data6 %>% mutate(ynum=2*(year-2017),
                 week2 = week+ynum)

# store week year ynum week2
# 1      1    1 2017    0     1
# 2      1    2 2017    0     2
# 3      1    1 2018    2     3
# 4      1    2 2018    2     4
# 5      1    1 2019    4     5
# 6      1    2 2019    4     6
# 7      2    1 2017    0     1
# 8      2    2 2017    0     2
# 9      2    1 2018    2     3
# 10     2    2 2018    2     4
# 11     2    1 2019    4     5
# 12     2    2 2019    4     6
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Thanks it works well