mercredi 22 mars 2023

Random yet balanced assignment of a binary variable with a map function

i am quite new to R and struggle with assigning a binary variable (domain; which can either be "social" or "analytical") to every row in my dataframe. I need it to add another column called domain and randomly assigned a value to every row. Furthermore, the assignment needs to be balanced in total and especially in the rows mentioned in Neutral. I came up with this code, which does randomly assign the values, but not in a balanced way. The idea was that it creates a score and if this reaches the maximum (for the neutral condition 6, for the rest 12), only the other condition is applied. However, as apparent by the resulting data, this approach does not work. I believe the issue is somehow, that the scores are not updated within the map function. Can anybody guide me how to solve this.

#Assign analytical or social task
Neutral <- c(1, 2, 5, 10, 11, 14, 19, 20, 23, 28, 29, 32)
merged_df <- merged_df %>%
  mutate(domain = NA) %>%
  { social_score <- 0
  analytical_score <- 0
  social_score_C <- 0
  analytical_score_C <- 0
  mutate(., domain = map(1:36, ~{
    if(.x %in% Neutral){
      A <- sample(c("social","analytical"), 1)
      while((A == "social" & social_score >= 6) || (A == "analytical" & analytical_score >= 6)){
        A <- sample(c("social","analytical"), 1)
      }
      if(A == "social"){
        social_score <- social_score + 1
      } else {
        analytical_score <- analytical_score + 1
      }
    } else {
      A <- sample(c("social","analytical"), 1)
      while((A == "social" & social_score_C >= 12) || (A == "analytical" & analytical_score_C >= 12)){
        A <- sample(c("social","analytical"), 1)
      }
      if(A == "social"){
        social_score_C <- social_score_C + 1
      } else {
        analytical_score_C <- analytical_score_C + 1
      }
    }
    return(A)
    print(analytical_score)
    print(analytical_score_C)
  }))
  }

I expected it to randomly assign the values "social" or "analytical" to my dataframe while making sure, that both the total number as well as the number in the "neutral" lines is balanced. However, I was not capable of coming up with a balanced way.




Aucun commentaire:

Enregistrer un commentaire