I am trying to find a solution that I think requires the use of some "not any of" function. I have a large set of applications that run in several different sites. Some of these sites are closing and I am trying to find a nice solution to determine which sites the applications can move to. Here is a look at some example data:
App_ID Current_Site Env
1 1 Alpha Dev
2 1 Alpha Test
3 1 Bravo Prod
4 1 Charlie Prod
5 2 Alpha Dev
6 2 Alpha Test
7 2 Delta Prod
8 3 Bravo Dev
9 3 Charlie Test
10 3 Bravo Prod
11 3 Delta Prod
12 4 Bravo Dev
13 4 Charlie Test
14 4 Echo Prod
The "Alpha" and "Beta" sites represent sites that are closing while "Charlie", "Delta", and "Echo" will remain open. Geographic Diversity by environment is also something that needs to be preserved (or implemented if not already). For simple geographic reasons, I first would plan to move anything from site Alpha to Charlie, and from site Bravo to Delta. But if this move causes the geographic diversity requirement to break, I would like to attempt to select an alternate site.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
App_ID <- c("1","1","1","1","2","2","2","3","3","3","3","4","4","4")
Current_Site <- c("Alpha","Alpha","Bravo","Charlie","Alpha","Alpha","Delta","Bravo","Charlie","Bravo","Delta","Bravo","Charlie","Echo")
Env <- c("Dev", "Test", "Prod", "Prod", "Dev", "Test", "Prod", "Dev", "Test", "Prod", "Prod", "Dev", "Test", "Prod")
df <- data.frame(App_ID, Current_Site, Env)
df2 <- df %>%
mutate(Target_Site = case_when(
Current_Site == "Alpha" ~ "Charlie",
Current_Site == "Bravo" ~ "Delta",
Current_Site == "Charlie" ~ "NONE",
Current_Site == "Delta" ~ "NONE"
)
) %>%
group_by(App_ID, Env) %>%
mutate(Backup_Target_Site = ifelse(any(Current_Site %in% Target_Site), "Do Something", "NONE")) %>%
ungroup()
Created on 2020-12-07 by the reprex package (v0.3.0)
Here is the output:
> df2
# A tibble: 14 x 5
App_ID Current_Site Env Target_Site Backup_Target_Site
<fct> <fct> <fct> <chr> <chr>
1 1 Alpha Dev Charlie NONE
2 1 Alpha Test Charlie NONE
3 1 Bravo Prod Delta NONE
4 1 Charlie Prod NONE NONE
5 2 Alpha Dev Charlie NONE
6 2 Alpha Test Charlie NONE
7 2 Delta Prod NONE NONE
8 3 Bravo Dev Delta NONE
9 3 Charlie Test NONE NONE
10 3 Bravo Prod Delta Do Something
11 3 Delta Prod NONE Do Something
12 4 Bravo Dev Delta NONE
13 4 Charlie Test NONE NONE
14 4 Echo Prod NONE NONE
What I am struggling with is to figure out the right function for "Do Something". As you can see for App 3, the initial choice to move from Bravo to Delta for the prod environment would prevent geographic diversity. I would like to randomly select any site between "Charlie", "Delta", and "Echo" so long as that site does not already exist within 'Current_Site" for the group (App_ID, Env).
I have tried to include various negated forms of "any", "contains", "matches", etc. but they are either unsuccessful or throw an error. Is there a simple tidyverse solution for this?