I have two dataframes in which first data frame is Problems it has problem number and number of problem to be solved, second data frame is Name_Data it is having name of the person who will solve the problems and days for which he can work. I need the resulting data frame such as based on total person and days availability, problem list will be assigned to each person. Problem_Solver_Name has to be assigned equally by calculating total problems divided by days availability of problem solver. Problem_with_Solver is my resulting data frame.
Please help if anybody has the solution for this
Name=c("Antony","Roger","Peter","Samuel")
Days=c(1,1,0.5,1)
Name_Data=data.frame(Name,Days)
Problem_List=c("Problem1","Problem2","Problem3","Problem4","Problem5","Problem6","Problem7","Problem8",
"Problem9","Problem10","Problem11","Problem12","Problem13","Problem14","Problem15","Problem16",
"Problem17","Problem18","Problem19")
Total_Problem=c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,18,19,20,21)
Problems=data.frame(Problem_List,Total_Problem)
Problem_Solver_Name=c("Antony","Antony","Antony","Antony","Antony","Antony","Roger","Roger","Roger","Roger","Roger","Peter","Peter","Peter","Samuel","Samuel","Samuel","Samuel","Samuel")
Problem_with_Solver=data.frame(Problem_List,Total_Problem,Problem_Solver_Name)
CodePudding user response:
Here is a solution.
First assign names proportionally to the days availability, then fill the remaining needs running through all names while there still are problems needing a solver's name.
This last assignment criterion is probably not the best but it will end up assigning names to problems.
# Persons availability data
Name <- c("Antony","Roger","Peter","Samuel")
Days <- c(1,1,0.5,1)
Name_Data <- data.frame(Name, Days)
# Problems data
Problem_List <- c("Problem1","Problem2","Problem3","Problem4","Problem5","Problem6","Problem7","Problem8",
"Problem9","Problem10","Problem11","Problem12","Problem13","Problem14","Problem15","Problem16",
"Problem17","Problem18","Problem19")
Total_Problem <- c(1,2,3,4,5,6,8,9,10,11,12,13,14,15,16,18,19,20,21)
Problems <- data.frame(Problem_List, Total_Problem)
#
# X: person and days availability data
#
assign_solver <- function(X, Problems, Solver) {
N <- nrow(Problems)
tt <- N * X$Days
nn <- N * tt / sum(tt)
#
# psn: problem solver name
psn <- character(N)
psn[1:sum(round(nn))] <- rep(X$Name, round(nn))
k <- 1L sum(round(nn))
i <- 1L
while(k <= N) {
psn[k] <- X$Name[i]
i <- if(i < nrow(X)) i 1L else 1L
k <- k 1L
}
psn <- psn[order(match(psn, X$Name))]
psn
}
Problem_with_Solver <- data.frame(Problem_List, Total_Problem)
Problem_with_Solver$Problem_Solver_Name <- assign_solver(Name_Data, Problems, Problem_Solver_Name)
Problem_with_Solver
#> Problem_List Total_Problem Problem_Solver_Name
#> 1 Problem1 1 Antony
#> 2 Problem2 2 Antony
#> 3 Problem3 3 Antony
#> 4 Problem4 4 Antony
#> 5 Problem5 5 Antony
#> 6 Problem6 6 Antony
#> 7 Problem7 8 Roger
#> 8 Problem8 9 Roger
#> 9 Problem9 10 Roger
#> 10 Problem10 11 Roger
#> 11 Problem11 12 Roger
#> 12 Problem12 13 Peter
#> 13 Problem13 14 Peter
#> 14 Problem14 15 Peter
#> 15 Problem15 16 Samuel
#> 16 Problem16 18 Samuel
#> 17 Problem17 19 Samuel
#> 18 Problem18 20 Samuel
#> 19 Problem19 21 Samuel
Created on 2022-07-25 by the reprex package (v2.0.1)