mardi 24 novembre 2020

Randomly assing to condition on equal proportions R

I have the following database (as an example)

participants <- c(1:60)
codes <- c(1:60)
database <-cbind(participants, codes)

The second variable codes contains emails linked to each participant ID, although for example proposes I just filled it with numbers. I have 60 participants each one with a participant ID and an email tied to this ID (a number from 1 to 60). As such in the example row 1 is 1 ,1 and so on. I need to divide the list on 3 groups of identical proportion, eg 20 participants per group. The way I am doing it now is

#generating list of participants
participants <- c(1:60)
codes <- c(1:60)
database <-cbind(participants, codes)

#randomizing the order of the list
randomized_list <- sample(participants)

#Extracting the three groups 

group1 <- randomized_list[c(1:20)]
group2 <- randomized_list[c(21:40)]
group3 <- randomized_list[c(41:60)]

Leaving me to do the work of getting the email addresses and dividing the lists more or less by hand (compare group 1, 2 and 3 with database and making the link).

Is there a more elegant and compact solution for achieving the results I seek?




Aucun commentaire:

Enregistrer un commentaire