I am in the process in randomly assigning treatments for an experiment. I have four sites (Site1, ... Site4) where 12 experimental units (e.g., 1 ... 12) are replicated four times (e.g., 1 ... 4). For each replicate, I have randomly assigned one of three treatments (e.g., trt1 ...trt3).
I now need to assign a SecondTreatment (y or no) to each Treatment within a Replicate for each of my Sites. trt2 should always be "y", whereas I want to randomly assign "y" to half of trt1, and "n" to another half of trt1, and then do the same for trt3. The should give me for each Replicate: trt2 with 4 "y", trt1 with 2 "n", and trt3 with 3 "n".
My data looks like this:
Site Experimental unit Replicate Treatment SecondTreatmentAssign (y/n)
Site1 1 1 trt1
Site1 2 1 trt2
Site1 3 1 trt3
Site1 4 1 trt3
Site1 5 1 trt1
Site1 6 1 trt2
Site1 7 1 trt3
Site1 8 1 trt2
Site1 9 1 trt1
Site1 10 1 trt1
Site1 11 1 trt3
Site1 12 1 trt2
Site1 1 2 trt2
Site1 2 2 trt3
Site1 3 2 trt1
Site1 4 2 trt2
Site1 5 2 trt1
Site1 6 2 trt3
Site1 7 2 trt2
Site1 8 2 trt2
Site1 9 2 trt1
Site1 10 2 trt2
Site1 11 2 trt1
Site1 12 2 trt3
Site1 1 3 trt2
Site1 2 3 trt1
Site1 3 3 trt3
Site1 4 3 trt3
Site1 5 3 trt2
Site1 6 3 trt1
Site1 7 3 trt3
Site1 8 3 trt2
Site1 9 3 trt1
Site1 10 3 trt1
Site1 11 3 trt3
Site1 12 3 trt2
Site1 1 4 trt3
Site1 2 4 trt2
Site1 3 4 trt1
Site1 4 4 trt3
Site1 5 4 trt2
Site1 6 4 trt1
Site1 7 4 trt3
Site1 8 4 trt1
Site1 9 4 trt2
Site1 10 4 trt1
Site1 11 4 trt2
Site1 12 4 trt3
. . . .
. . . .
. . . .
Site4 12 4 trt1
I'd like to be able to do this in a way that makes these assignments back into the dataframe that way I don't have to manually move anything around. I am still quite the novice with programming and not sure how to make this happen.
Thanks!
I would do this:
library(dplyr)
data %>%
arrange(runif(n())) %>% # randomize the order
group_by(Site, Replicate, Treatment) %>% # group
mutate(
Treat_2 = case_when(
Treatment == "trt2" ~ "y", # trt2 gets 'y'
row_number() <= n() / 2 ~ "y", # others in the first half get "y"
TRUE ~ "n" # others in the second half get "n"
)) %>%
arrange(Site, Replicate, `Experimental unit`) ## return to original order