Hi Guys,
I am trying to prepare a function/loop which would sample a number of rows based on conditions.
I have two dataframe the first one is a dataframe with day dates, people and their roles.
library(bizdays)
library(tidyverse)
from_date <- "2023-01-01"
to_date <- "2023-12-31"
create.calendar(name = "cal23", holidays = holidays, weekdays = c("saturday", "sunday"))
daty2023 <- bizseq(from_date, to_date, "cal23") %>% as.data.frame()
daty2023 %>% mutate(resource = "Michael")
daty2023 <- daty2023 %>% mutate(role = "Manager")
daty20231 <- daty2023 %>% mutate(resource = "Jose", role = "Manager")
daty20232 <- daty2023 %>% mutate(resource = "Jakub", role = "Researcher")
daty2023 <- rbind(daty2023, daty20231, daty20232)
colnames(daty2023) <- c("date", "resource", "role")
daty2023$date <-as.Date(daty2023$date)
The second dataframe is a tasklist with names of tasks, roles needed for these tasks, a period in which they should be performed and a number of mandays which should be assigned to each task in a given period.
tasklist <- data.frame(task = c("Task1", "Task2", "Task3", "Task4"), sdate = c("2023-01-12", "2023-04-18", "2023-08-25", "2023-11-03"), fdate = c("2023-01-12", "2023-04-25","2023-09-26","2023-12-29"), resource = c("Manager", "Manager","Researcher", "Manager")
tasklist <- tasklist %>% mutate(mandays = c("1", "10","20","30"))
tasklist$sdate <- as.Date(tasklist$sdate)
tasklist$fdate <- as.Date(tasklist$fdate)
What I am trying to do now is to sample dates in which people are available from the first data frame and assign them to tasks in the second data frame based on conditions. To do that I use the sample_n package.
df = data.frame()
for (i in 1:nrow(tasklist)) {
output = daty2023 %>% filter(date >= tasklist$sdate[i] & date <= tasklist$fdate[i] & role == tasklist$resource[i]) %>% sample_n(., tasklist$mandays[i]) %>% mutate(Task = tasklist$task[i])
df = rbind(df, output)
}
This loop allows me to match a person's availability with project task time slot and a role. However I started to wonder, whether it would be possible, and if yes how it would be possible, to add additional condition which would assign a given amount of mandays to a person. The loop from above produces 61 mandays assigned to people. I am thinking how to make sure that i.e. 40 mandays at least are assigned to Michael, 11 to Jose and 10 to Jakub. I reckon I could probably play with the weight argument but I do not see how it could give me exactly the split I am looking for.
All the best!