library(tidyr)
library(dplyr)
# No. of rows: 4 * 3 * 2 = 24 rows
# Define levels as vectors
n_levels <- c(100,200,300,400)
p_levels <- c(50,100,150)
irri_levels <- c("yes","no")Solution for generating experimental treatment combinations
1 Solution with R output
1.1 Task 1
The number of possible treatment combinations is the product of the number of levels of each variable:
4 (N) * 3 (P) * 2 (irrigation) = 24 possible combinations
1.2 Task 2
1.2.1 One potential approach using nested rep-calls
dat_exp1 <- tibble(nitrogen = rep(n_levels, each = 2*3),
phosphorus = rep( rep(p_levels, each = 2), times = 4),
irrigation = rep(irri_levels, times = 4 * 3))
dat_exp1# A tibble: 24 × 3
nitrogen phosphorus irrigation
<dbl> <dbl> <chr>
1 100 50 yes
2 100 50 no
3 100 100 yes
4 100 100 no
5 100 150 yes
6 100 150 no
7 200 50 yes
8 200 50 no
9 200 100 yes
10 200 100 no
# ℹ 14 more rows
1.2.2 Another approach with one table for each irrigation levels, which are combined
dat_irri <- tibble(nitrogen = rep(n_levels, each = 3),
phosphorus = rep(p_levels, times = 4),
irrigation = "yes")
dat_no_irri <- dat_irri %>% mutate(irrigation = "no")
dat_exp2 <- bind_rows(dat_irri, dat_no_irri)
dat_exp2# A tibble: 24 × 3
nitrogen phosphorus irrigation
<dbl> <dbl> <chr>
1 100 50 yes
2 100 100 yes
3 100 150 yes
4 200 50 yes
5 200 100 yes
6 200 150 yes
7 300 50 yes
8 300 100 yes
9 300 150 yes
10 400 50 yes
# ℹ 14 more rows
1.3 Extra:
With a single call to tidyr::expand_grid
dat_exp3 <- expand_grid(n_levels, p_levels, irri_levels)
dat_exp3# A tibble: 24 × 3
n_levels p_levels irri_levels
<dbl> <dbl> <chr>
1 100 50 yes
2 100 50 no
3 100 100 yes
4 100 100 no
5 100 150 yes
6 100 150 no
7 200 50 yes
8 200 50 no
9 200 100 yes
10 200 100 no
# ℹ 14 more rows
2 Solution as one script without output
library(tidyr)
library(dplyr)
# No. of rows: 4 * 3 * 2 = 24 rows
# Define levels as vectors
n_levels <- c(100,200,300,400)
p_levels <- c(50,100,150)
irri_levels <- c("yes","no")
##One potential approach using nested rep-calls
dat_exp1 <- tibble(nitrogen = rep(n_levels, each = 2*3),
phosphorus = rep(rep(p_levels, each = 2), times = 4),
irrigation = rep(irri_levels, times = 4*3))
dat_exp1
# Another approach with one table for each irrigation levels, which are combined
dat_irri <- tibble(nitrogen = rep(n_levels, each = 3),
phosphorus = rep(p_levels, times = 4),
irrigation = "yes")
dat_no_irri <- dat_irri %>% mutate(irrigation = "no")
dat_exp2 <- bind_rows(dat_irri, dat_no_irri)
dat_exp2
# Extra: With a single call to tidyr::expand_grid
dat_exp3 <- expand_grid(n_levels, p_levels, irri_levels)
dat_exp3