mardi 17 mars 2020

How do I randomize and loop a list of categorical variables?

I'm practicing using loop functions and want to create a loop that will randomly generate which of my cousins should call my grandma during the 14 days of Covid self-isolation.

I created the variable "family", which specifies which cousins (ex. "Rob", "David", "Chantal") belong to which aunt or uncle ex. "Laverne"). However, I only want to sample my cousins and not my aunts/uncles.

I am assuming there will be 14 days in self-isolation, so I will need to randomly sample my cousins more than twice (without replacement). i.e. my grandma will receive calls from 2 grandchildren on most days, and 3 on 2 days.

days <-14
family <- list("Laverne" = c("Rob", "David", "Chantal"), 
              "Jerry" = c("Amber", "Todd", "Kyla"), 
              "Gwen" = c("Kirk", "Heather"), 
              "Carleen" = c("Amber", "Chris", "Katelyn"), 
              "Al" = c("Regan", "Adam", "Dana", "Ally_Taylor"),
              "Celine" = c("Tyler", "Ashley", "Kayla", "Matthew"), 
              "Kathy" = c("Josh", "Cami", "Jessica", "Michelle", "Danielle"), 
              "Leanne" = c("Raylene", "Clinton", "Alina", "Tristina"),
              "Kim" = "Anthony_Emerson", 
              "Dawna"= "Leiland_Kelsey")

I haven't gotten far in writing the loop code. This is what I have so far:

for(i in family:n){
  sample1 <- sample(family, 1, replace = FALSE)
  resample <- sample(sample1)
}

Any help would be appreciated!




Aucun commentaire:

Enregistrer un commentaire