Home > OS >  Increment a variable by 5% every week in R
Increment a variable by 5% every week in R

Time:05-10

I have the following data:

date <- seq.Date(from = as.Date("2022-01-01"), to = as.Date("2022-07-01"), by = "day")
value <- rep(500, length(date))
mydf <- cbind.data.frame(date, deliveries)

I want to increment my value variable by 5% EACH WEEK, i.e., mydf$value[1:7] remains unchanged, mydf$value[8:14] become 500*(1.05) = 525, mydf$value[15:21] become 525*(1.05) = 551.25, and so on.

CodePudding user response:

the folowing will work, also when your series of dates is not continuous.

It will calculate the difference from each date with the first date, in weeks. Rounds down and then use that num,ber as the power of 1.05.

mydf$newValue <- value * 1.05^(as.numeric(floor(difftime(mydf$date, mydf$date[1], units = "weeks"))))

          date value  newValue
1   2022-01-01   500  500.0000
2   2022-01-02   500  500.0000
3   2022-01-03   500  500.0000
4   2022-01-04   500  500.0000
5   2022-01-05   500  500.0000
6   2022-01-06   500  500.0000
7   2022-01-07   500  500.0000
8   2022-01-08   500  525.0000
9   2022-01-09   500  525.0000
10  2022-01-10   500  525.0000
11  2022-01-11   500  525.0000
12  2022-01-12   500  525.0000
13  2022-01-13   500  525.0000
14  2022-01-14   500  525.0000
15  2022-01-15   500  551.2500
16  2022-01-16   500  551.2500
17  2022-01-17   500  551.2500
18  2022-01-18   500  551.2500
19  2022-01-19   500  551.2500
20  2022-01-20   500  551.2500
21  2022-01-21   500  551.2500
22  2022-01-22   500  578.8125
23  2022-01-23   500  578.8125
24  2022-01-24   500  578.8125
25  2022-01-25   500  578.8125
26  2022-01-26   500  578.8125
27  2022-01-27   500  578.8125
28  2022-01-28   500  578.8125
29  2022-01-29   500  607.7531
...

CodePudding user response:

date <- seq.Date(from = as.Date("2022-01-01"), to = as.Date("2022-07-01"), by = "day")
value <- rep(500, length(date))
mydf <- cbind.data.frame(date, value)
mydf$value_updated <- c(rep(500,times=7), 500   rep(cumsum(rep(500, (length(date)-7)/7)*.05), each = 7))
rbind(head(mydf), tail(mydf))
          date value value_updated
1   2022-01-01   500           500
2   2022-01-02   500           500
3   2022-01-03   500           500
4   2022-01-04   500           500
5   2022-01-05   500           500
6   2022-01-06   500           500
177 2022-06-26   500          1125
178 2022-06-27   500          1125
179 2022-06-28   500          1125
180 2022-06-29   500          1125
181 2022-06-30   500          1125
182 2022-07-01   500          1125

CodePudding user response:

Here's another option using dplyr (the logic is similar @Wimpel's answer), essentially we can create a series of weeks using gl, which is then the power raised to for 1.05. However, this assumes continuous data.

library(dplyr)

mydf %>%
  mutate(value = value* 1.05^(as.integer(gl(n(), 7, n()))-1))

Output

          date     value
1   2022-01-01  500.0000
2   2022-01-02  500.0000
3   2022-01-03  500.0000
4   2022-01-04  500.0000
5   2022-01-05  500.0000
6   2022-01-06  500.0000
7   2022-01-07  500.0000
8   2022-01-08  525.0000
9   2022-01-09  525.0000

Another option if you would like group in the actual weeks that a date falls in, then you can use cut.Date:

mydf %>%
  mutate(value = value* 1.05^(cut.Date(date, breaks = "1 week", labels = FALSE)-1))

Output

          date     value
1   2022-01-01  500.0000
2   2022-01-02  500.0000
3   2022-01-03  525.0000
4   2022-01-04  525.0000
5   2022-01-05  525.0000
6   2022-01-06  525.0000
7   2022-01-07  525.0000
8   2022-01-08  525.0000
9   2022-01-09  525.0000
10  2022-01-10  551.2500
11  2022-01-11  551.2500
12  2022-01-12  551.2500
13  2022-01-13  551.2500
14  2022-01-14  551.2500
15  2022-01-15  551.2500
16  2022-01-16  551.2500
17  2022-01-17  578.8125
  •  Tags:  
  • r
  • Related