I'm considering a potential policy change that will allow organizations (OrgID) to spend their existing grant money if they meet certain conditions. This would occur once per year, and the conditions are based on which group the organization is in (Group), and their current balance (Amount). Specifically,
For
Group=Group1
- IfAmountis $500,000 or less, all the money can be spent
- IfAmountis greater than $500,000, 50% of the money can be spent.For
Group=Group2
- ifAmountis $300,000 or less, all the money can be spent
- IfAmountis greater than $300,000, 30% of the money can be spent.For
Group=Group3
- IfAmountis $100,000 or less, all the money can be spent
- IfAmountis greater than $100,000, 10% of the money can be spent.For
Group=Group4
- No money can be spent under any conditions.
I wanted to know the total money remaining after each year for the next five years, so I turned to the dplyr package and wrote the following:
mydata <-
mydata %>%
mutate(ReleaseOne =
ifelse(Group == "Group1",
ifelse(Amount <= 500000, Amount,
round(Amount*0.50, 2)),
ifelse(Group == "Group2",
ifelse(Amount <= 300000, Amount,
round(Amount*0.30, 2)),
ifelse(Group == "Group3",
ifelse(Amount <= 100000, Amount,
round(Amount*0.10, 2)), 0)))) %>%
mutate(RemainOne =
Amount - ReleaseOne)
...
mydata <-
mydata %>%
mutate(ReleaseFive =
ifelse(Group == "Group1",
ifelse(RemainFour <= 500000, RemainFour,
round((RemainFour)*0.50, 2)),
ifelse(Group == "Group2",
ifelse(RemainFour <= 300000, RemainFour,
round((RemainFour)*0.30, 2)),
ifelse(Group == "Group3",
ifelse(RemainFour <= 100000, RemainFour,
round((RemainFour)*0.10, 2)), 0)))) %>%
mutate(RemainFive =
RemainFour - ReleaseFive)
Hence, I just repeated the same block of code five times, but each time I changed the names of the variables that begin with "Release" and "Remain" (i.e. RemaimOne to RemainTwo, ReleaseOne to ReleaseTwo, etc.).
Doing it this way is fine but it got pretty messy. Is there a way to simplify this with custom functions, possibly including for and while loops, for example?
Also, it would be valuable to know how how many years it would be until all organizations in groups 1, 2, and 3 reached Amount = 0; but, the only way I know how to do it is keep repeating what's above until the amounts reach zero.
The data is named mydata.txt and can be found on GitHub at this link.
Aucun commentaire:
Enregistrer un commentaire