I have a database of residents and staff. They have each made requests for certain food types, but we have a limited quantity to give out. Based on their requests, I'd like to create a prioritization based upon their age. The older the individual, the higher the priority.
Here is a sample dataframe:
dummy <- data.frame(caseID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19),
position = c("Resident", "Staff", "Resident", "Staff", "Staff", "Resident", "Resident", "Staff", "Resident", "Staff", "Staff", "Resident", "Resident", "Resident", "Staff", "Resident", "Staff", "Staff", "Resident") ,
grouphome = c("Group Home 1", "Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 2","Group Home 2","Group Home 2","Group Home 2", "Group Home 3", "Group Home 3","Group Home 3","Group Home 3","Group Home 3","Group Home 3","Group Home 3"),
agegroup = c("Young", "Young", "Young", "Young", "Young", "Very old", "Very old", 'Young', "Old","Young", "Young", "Young", "Young", "Young", "Very old", "Very old", 'Young', "Old", "Old"),
FoodType1 = c(5, 5, 8, 10, 6, 10, 5, 5, 10, 15, 2, 6, 10, 14, 5, 5, 8, 10, 6),
FoodType2 = c(2, 3, 4, 6, 2, 6, 1, 5, 8,2, 3, 4, 6, 2, 6, 1, 5, 8,2),
FoodType3 = c(1, 3, 6, 2, 4, 3, 7, 1, 2, 1, 3, 6, 2, 4, 3, 7, 1, 2, 1))
To create the priority groups, I have done this :
dummy <- dummy %>%
mutate(priority = case_when(
agegroup == "Very old" ~ 1,
agegroup== "Old" ~ 2,
agegroup== "Young" ~ 3,
TRUE ~ 4))
Now, say I have a set quantity of each FoodType (100 of FoodType1, 65 of FoodType2, and 50 of FoodType3), and I want to allocate these based on priority. The new allocations would appear in a new dataframe, and the code would look at each individual and the overall quantity to make the allocations. For each food type, if the individual is priority 1, then she should get at least 90% of her request allocated. Whatever is left after allocating to priority number 1 individuals will get divided randomly (giving each person as much of their requested share as possible) between individuals of priority number 2 and 3.
The output dataframe would look like this:
dummy2 <- data.frame(caseID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19),
position = c("Resident", "Staff", "Resident", "Staff", "Staff", "Resident", "Resident", "Staff", "Resident", "Staff", "Staff", "Resident", "Resident", "Resident", "Staff", "Resident", "Staff", "Staff", "Resident") ,
grouphome = c("Group Home 1", "Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 1","Group Home 2","Group Home 2","Group Home 2","Group Home 2", "Group Home 3", "Group Home 3","Group Home 3","Group Home 3","Group Home 3","Group Home 3","Group Home 3"),
agegroup = c("Young", "Young", "Young", "Young", "Young", "Very old", "Very old", 'Young', "Old","Young", "Young", "Young", "Young", "Young", "Very old", "Very old", 'Young', "Old", "Old"),
FoodType1 = c(5, 5, 6, 7, 5, 10, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5),
FoodType2 = c(1, 2, 3, 5, 1, 6, 1, 4, 7,1, 2, 3, 5, 2, 6, 1, 5, 8,2),
FoodType3 = c(1, 2, 5, 1, 3, 3, 7, 1, 1, 1, 2, 5, 1, 3, 3, 7, 1, 2, 1))
EDIT: I dont care so much about the division of the resources after making sure priority 1 gets as much of their requests as possible
IS there a way to do this in R? Is there a better place to do it? Thank you in advance!