Efficiently setting month one backwards

Viewed 132

In my dataset I want to create a new variable in which the month is set one backwards. I can do it like this:

df$month.min.1 <- gsub('1', '12', df$month)
df$month.min.1 <- gsub('2', '1', df$month)
df$month.min.1 <- gsub('3', '2', df$month)
df$month.min.1 <- gsub('4', '3', df$month)
....

As I also want to create variables in which the month is set two and three months backwards, I'm wondering if there is a more efficient way to do this?

5 Answers
Related