I have a sample dataframe from which I want to calculate a value by adding up the previous value over multiple columns.
I have a dataframe df:
LK Loc1 Loc2 Loc3
1 13 22 0
2 20 18 4
3 12 21 2
4 2 0 1
5 1 2 0
I want to get in a new dataframe:
LK Loc1 Loc2 Loc3
1 13 22 0
2 33 40 4
3 45 61 6
4 47 61 7
5 48 63 7
I tried something with:
df2 <- df %>%
mutate_at(vars(-LK), accumulate(function(.) (.) * 0.99))
But I can't get it to work.
Any help is appreciated.
Thank you in advance
df2 %>% mutate(across(-LK, cumsum)). Not sure why you have the0.99part in your attempt.