knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
RCBD_table <- function(n_reps, n_treats, outtable = TRUE, save_tidy = FALSE, blk_matrix = FALSE) {
  # variables
  # n_reps <- 6
  # n_treats <- 8
  # n_pop <- 14

  # get 100 levels blocks names
  blk_initial <- seq(from = 100,to = (n_reps*100), by = 100)

  # create a list of block names
  blk_names <- NULL
  for (i in seq(1:n_reps)){
    blk_names <- append(blk_names,rep(i,n_treats))
  }

  # create plot numbers
  blocks <- NULL
  block <- NULL
  for(i in blk_initial){
    for(j in seq(1:n_treats)){
      plot = i + j
      block <- append(block,plot)
    }
    block <- sample(block)
    blocks <- append(blocks, block)
    block <- NULL
  }

  # create tidy df
  blocks_df <- tibble(blocks, blk_names) %>%
    rename(plots = blocks) %>%
    mutate(blk_names = factor(paste0("Block_",blk_names)),
           treatments = rep(seq(1:n_treats),n_reps))
  if (save_tidy == TRUE) {
    blocks_df <<- blocks_df
  }
  # create spread
  blocks_spread <<- blocks_df %>%
    spread(key = blk_names, value = plots)

  # Spread table not randomized
  if (blk_matrix == TRUE){
    block_matrix <<- blocks_spread %>%
      select(-treatments) %>%
      gather(key = "block", value = "plot") %>%
      arrange(plot) %>%
      mutate(Row = rep(seq(1:n_treats),n_reps)) %>%
      spread(block, plot) %>%
      select(-Row)
  }

  #print table?
  if (outtable == TRUE){
    return(blocks_spread)
  }


}
library(DT)
library(kableExtra)

RCBD - Block and treatments arragement

RCBD_table(n_reps = 6, n_treats = 9, blk_matrix = TRUE, outtable = FALSE)
DT::datatable(blocks_spread, rownames = F)

Block Matrix

DT::datatable(block_matrix, rownames = F)


kopeckyl/WeedSciR documentation built on Jan. 8, 2022, 2:23 a.m.