rdataframedatasetbioinformaticsexperimental-design

Generate Experimental design Data in R


I have a randomized block design experiment in which we d like to test 10 treatments (8 genotypes + 2 controls).

It s structured as follow :

9 sites, within each one, there are 4 blocks (repetitions), within each block, There are 10 plots, Within each plot, there are 144 individuals of a given treatment (genotype).

I'd like to generate a dataset as follow :

NB: With a random assignement of treatements to plots within each block.

Site <- LETTERS[1:6]
Block <- LETTERS[1:4] ## For each Site
Plot <- paste(rep("P",10),seq(1,10,1),sep="_") ## For each Block 
Trt <- c(LETTERS[1:8],rep("control",2)) ## 10 Treatments within each block
id <- seq(1,144,1) ## for each Plot
observation <- runif(n=144,min=1, max=4) ## repeated for each treatment, each block,and each site.

data.frame(Site,Block,Plot,Trt,id,observation)

Knowing that this code will drop an error, id'like to have,for the same Site, all block levels, for the same block, all plot levels and their coresponding treatments, and for the same plot all id Tags and their corresponding observations.

Many Thanks


Solution

  • I think you might be looking for expand.grid :

    Site <- LETTERS[1:6]
    Block <- LETTERS[1:4] ## For each Site
    Plot <- paste(rep("P",10),seq(1,10,1),sep="_") ## For each Block 
    data <- expand.grid(Site = Site, Block = Block, Plot = Plot)
    data$Trt <- sample(c(LETTERS[1:8],paste0("control",1:2)), nrow(data), replace = TRUE)
    data