1

I'm trying to crate a data.table with 3 vectors. Where vector A Trial = [a,b,c...n], vector B rep = [1,2,3,...,n] and vector C plot = [r01, r02, r03,...,n] where r= "rep" (replicates)

Example:

> trial <- c("a", "b", "c")
> plot <- c(101:103,201:203,301:303)
> rep <- c(1,2,3)
> trial
[1] "a" "b" "c"
> plot
[1] 101 102 103 201 202 203 301 302 303
> rep
[1] 1 2 3
> dt <- data.table(trial,plot,rep)
> dt
   trial plot rep
1:     a  101   1
2:     b  102   2
3:     c  103   3
4:     a  201   1
5:     b  202   2
6:     c  203   3
7:     a  301   1
8:     b  302   2
9:     c  303   3
> dt <- data.table(trial,rep,plot)
> dt
   trial rep plot
1:     a   1  101
2:     b   2  102
3:     c   3  103
4:     a   1  201
5:     b   2  202
6:     c   3  203
7:     a   1  301
8:     b   2  302
9:     c   3  303

Neither of these are quite correct.

I want rep to increment plot by 100 x rep + plot #.

For trial (x): rep 1, plot 1 -> 101

For trial (x): rep 1, plot 2 -> 102

For trial (x): rep 2, plot 1 -> 201

For trial (x): rep 2, plot 2 -> 202

etc.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
Mister
  • 11
  • 3
  • What is your expected output? Do you need `data.table(trial = c("a", "b", "c"), rep = rep(1:3, each = 3), plot = c(101:103,201:203,301:303))` ? – Ronak Shah Jan 18 '20 at 07:40
  • `data.frame(trial = letters[1:3], rep = rep(1:3, each = 3), plot = rep(1:3, 3) + rep(seq(from=100,to=300, by = 100), each = 3), stringsAsFactors = FALSE) ` – Sathish Jan 18 '20 at 07:53
  • Both are close... but don't allow for plots = n. I'd like to be able to create a table where # trials = x, # reps = y, and # plots = y. – Mister Jan 18 '20 at 08:21

2 Answers2

0

The problem seems to be apply a function to vectors rep and plot in the correct order. outer is a good candidate to solve it.

library(data.table)

trial <- c("a", "b", "c")
plot <- 1:3
rep <- 1:3

f <- function(r, p) 100*r + p
as.vector(t(outer(rep, plot, f)))
#[1] 101 102 103 201 202 203 301 302 303

dt <- data.table(trial, rep, plot = as.vector(outer(rep, plot, f)))
dt
#   trial rep plot
#1:     a   1  101
#2:     b   2  201
#3:     c   3  301
#4:     a   1  102
#5:     b   2  202
#6:     c   3  302
#7:     a   1  103
#8:     b   2  203
#9:     c   3  303
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • Output is almost there. I'm still trying to figure out functions. I"m not sure why I need to create a matrix and take the outer product. I can do this in excel in a matter of minutes but I'm trying to understand how to write a script so I can replicate by entering a set of variables such as # trials, # reps, # plots. Plots are essentially treatments (genotypes). Reps could be considered blocks. Trials could be considered environments, cohorts, populations, etc. – Mister Jan 18 '20 at 09:04
  • @Mister I am using the increment formula you've posted, can you post the expected output of your example? And there is no need to create a matrix, that's just what `outer` outputs. Also, maybe `expand.grid` is what you are after. – Rui Barradas Jan 18 '20 at 09:15
  • Aargh. I can't figure out how to post my example without it being a complete mess. Trial A -> Rep 1 -> plots 101, 102, 103, ... , 1xx Trial A -> Rep 2 -> plots 201, 202, 203, ... , 2xx Trial A -> Rep 3 -> plots 301, 302, 303, ... , 3xx Trial B -> Rep 1 -> plots 101, 102, 103, ... , 1xx ... Trial (i) -> Rep (j) -> plots k + (j*100) Essentially a RCBA design where every block contains every tx, every trial contains the same number of blocks. – Mister Jan 18 '20 at 09:57
0

Thanks for all the help. The following allows me to dynamically edit all portions of the df.

I was able to work through the problem with the code provided by everyone. I don't know who to give credit to.

Thanks to all!

loc <- c("Orchard", "Roggen", "Yuma", "Walsh", "Akron", "Julesburg", "Arapahoe", "Genoa", "Burlington", "Lamar", "Brandon")

i <- loc

rep <- 3 ## num reps

j <- seq(rep)

plot <- 5## plots

k <- seq(plot)

df <- data.frame(loc = i, block = rep(j, each = length(loc)), plot=rep(k, length(i)*rep))
Mister
  • 11
  • 3