'Cummulative sum in R with unusual conditions
I'm trying to solve a much bigger problem, but this might be the key to the castle.
The example I'm giving has an order to it because there are dates but I dropped them for simplicity.
I would like to have a changing sum. The sum is equal to whatever the value is for the group and when new groups have customers, then those are added or dropped. Here is the example data. It's grouped by CityName
structure(list(CityName = c("A", "A", "B", "B", "C", "D"), Customers = c("5",
"4", "3", "2", "1", "0"), Sum = c("5", "4", "7", "6", "7", "7"
)), class = "data.frame", row.names = c(NA, -6L))
I can't make a cumulative sum work because I'm not trying to sum within the group.
I ended up figuring out the answer by creating basically a tally of differences within the same group. So if 5 to 4, then I have -1 and then can sum a 5+4+-1 to get a current total count of 5 for that group.
Solution 1:[1]
I figured it out. Adding a date for sorting purposes.
sample<-sample %>% group_by(CityName) %>% arrange(RecordedDateTime) %>%
mutate(newCust=Customers,
CustY=ifelse(!is.na(lag(Customers)),lag(Customers),0),
diff=Customers-CustY) %>% ungroup() %>% mutate(sum=cumsum(diff))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ClaireR |
