Search code examples
rlistloopsrandomsample

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!


Solution

  • You probably also need to take account for the days.

    # initialize matrix
    samp <- matrix(NA, nrow=days, ncol=length(family), dimnames=list(NULL, names(family)))
    
    set.seed(42)  ## for sake of reproducibility
    for (j in 1:days) {
      for (i in 1:length(family)) {
        samp[j, i] <- sample(family[[i]], 1)
      }
    }
    

    Result

    samp 
    #       Laverne   Jerry   Gwen      Carleen   Al            Celine    Kathy      Leanne     Kim               Dawna           
    #  [1,] "Rob"     "Amber" "Kirk"    "Amber"   "Adam"        "Matthew" "Cami"     "Clinton"  "Anthony_Emerson" "Leiland_Kelsey"
    #  [2,] "Chantal" "Kyla"  "Heather" "Amber"   "Regan"       "Ashley"  "Michelle" "Clinton"  "Anthony_Emerson" "Leiland_Kelsey"
    #  [3,] "Chantal" "Amber" "Kirk"    "Katelyn" "Ally_Taylor" "Tyler"   "Danielle" "Raylene"  "Anthony_Emerson" "Leiland_Kelsey"
    #  [4,] "Chantal" "Todd"  "Kirk"    "Chris"   "Ally_Taylor" "Ashley"  "Jessica"  "Alina"    "Anthony_Emerson" "Leiland_Kelsey"
    #  [5,] "David"   "Todd"  "Kirk"    "Amber"   "Ally_Taylor" "Ashley"  "Cami"     "Alina"    "Anthony_Emerson" "Leiland_Kelsey"
    #  [6,] "Rob"     "Amber" "Heather" "Chris"   "Adam"        "Ashley"  "Cami"     "Tristina" "Anthony_Emerson" "Leiland_Kelsey"
    #  [7,] "Rob"     "Todd"  "Kirk"    "Chris"   "Adam"        "Ashley"  "Danielle" "Raylene"  "Anthony_Emerson" "Leiland_Kelsey"
    #  [8,] "Rob"     "Todd"  "Kirk"    "Amber"   "Ally_Taylor" "Matthew" "Josh"     "Alina"    "Anthony_Emerson" "Leiland_Kelsey"
    #  [9,] "Rob"     "Todd"  "Kirk"    "Chris"   "Adam"        "Ashley"  "Josh"     "Clinton"  "Anthony_Emerson" "Leiland_Kelsey"
    # [10,] "Chantal" "Amber" "Heather" "Amber"   "Ally_Taylor" "Kayla"   "Cami"     "Clinton"  "Anthony_Emerson" "Leiland_Kelsey"
    # [11,] "Rob"     "Todd"  "Kirk"    "Chris"   "Ally_Taylor" "Tyler"   "Josh"     "Tristina" "Anthony_Emerson" "Leiland_Kelsey"
    # [12,] "Rob"     "Todd"  "Kirk"    "Amber"   "Adam"        "Matthew" "Danielle" "Alina"    "Anthony_Emerson" "Leiland_Kelsey"
    # [13,] "Rob"     "Todd"  "Kirk"    "Amber"   "Dana"        "Matthew" "Cami"     "Alina"    "Anthony_Emerson" "Leiland_Kelsey"
    # [14,] "Rob"     "Kyla"  "Kirk"    "Amber"   "Regan"       "Kayla"   "Josh"     "Clinton"  "Anthony_Emerson" "Leiland_Kelsey"
    

    Without for loop

    Without a for loop you may do (note that there's no need to initialize the matrix in this case):

    set.seed(42)
    samp <- t(replicate(days, Map(`[`, family, lapply(lengths(family), function(x) 
      sample(seq(x), 1)))))
    ## with output similar to above