I am working with a long (person-period) dataset with unbalanced number (varying N of observations per person). What I want to do is randomly assign a treatment (A, B or C) to every row/observation for each person (within a new column) with the treatment varying randomly by person. So each person will get one of three interventions and the intervention stays the same for each of their observations.
So starting from just an ID column I want to randomly assign the treatment. The final result would look something like this.
ID <- c(1,1,1,2,2,2,2,2,3,3,4,4,4,4,5,6,6,6,7,7)
Treatment <- c('a','a','a','b','b','b','b','b','c','c','a','a','a','a','b','c','c','c','a','a')
data <- data.frame(ID, Treatment)
data
I tried the example using ddply (How to generate a random treatment variable by factor?) however I want by treatment variable to be constant for the grouping variable.
Appreciate any help you can offer :)