I'm trying to model assets depreciating over time in R. I have a dataset df=tibble(expand_grid(id=1:2, time=1:10), assets=id * 5000)
, and want to show the assets falling by 250 in each period for each id. I've tried
input_data %>%
group_by(id) %>%
mutate(assets = case_when(time > 1 ~ lag(assets) - 250, TRUE ~ assets))
but this doesn't iterate in the way I want. I've also tried to use a for loop, but I've only figured out how to do this for the first ID -
for (i in 2:max(input_data$time)) {
input_data$assets[i] <- input_data$assets[i-1] - 250
}
I'd really like advice as to how to do the same thing the for loop does for each ID - the input dataset should have about 100 IDs so an easy way to loop over them, or to split the dataset and apply the for loop to each group, would help. I'd especially appreciate an answer using tidyverse functions, since I know them much better.
Thank you. I hope this was clear enough.
I think this is what you want. I created a sequence assets_drop
to ensure this code also works when time is date.
input_data %>%
group_by(id) %>%
mutate(assets_drop = seq(0, length.out = n(), by = 250),
assets_new = assets - assets_drop)
The output is
# A tibble: 20 x 5
# Groups: id [2]
id time assets assets_drop assets_new
<int> <int> <dbl> <dbl> <dbl>
1 1 1 5000 0 5000
2 1 2 5000 250 4750
3 1 3 5000 500 4500
4 1 4 5000 750 4250
5 1 5 5000 1000 4000
6 1 6 5000 1250 3750
7 1 7 5000 1500 3500
8 1 8 5000 1750 3250
9 1 9 5000 2000 3000
10 1 10 5000 2250 2750
11 2 1 10000 0 10000
12 2 2 10000 250 9750
13 2 3 10000 500 9500
14 2 4 10000 750 9250
15 2 5 10000 1000 9000
16 2 6 10000 1250 8750
17 2 7 10000 1500 8500
18 2 8 10000 1750 8250
19 2 9 10000 2000 8000
20 2 10 10000 2250 7750
Similar approach to @tivd's, here I define depreciation per period, then calculate the sum of all the prior depreciations, and then redefine assets to be it's original value minus depreciation to that point.
df %>%
group_by(id) %>%
mutate(deprec_period = 250,
deprec = pmin(assets, cumsum(lag(deprec_period, default = 0))),
assets = assets - deprec)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.