R code for a constrained randomization in a three-dimensional array?
11:54 28 Jan 2026

I am trying to create an experimental randomization for the following setup:

  • There is a 4-by-7 array of pipes.

  • Each pipe can hold 14 samples.

  • Thus we have a 4 x 7 x 14 three-dimensional array that can hold 392 samples.

  • There are samples from 152 farms. Each farm's sample is divided into 3 subsamples, each of which is assigned randomly to one of three treatments (call them A,B,C).

  • The experimenter wants all 152 samples for treatment A, and all 152 samples for treatment B, but only 88 samples from treatment C to be put in the array.

I want to follow these constraints:

  • Each pipe (X by Y combination) should contain roughly the same number of samples, proportionally, from each treatment (so on average 5.4 treatment A samples, 5.4 treatment B samples, and 3.2 treatment C samples)

  • Each depth level (Z coordinate) should also contain roughly the same number of samples, proportionally, from each treatment (so on average 10.8 from treatment A, 10.8 from treatment B, and 6.4 from treatment C)

  • No pipe (X by Y combination) should contain more than one sample from the same farm

I am having trouble getting started writing the R code for this randomization. Can anyone give me a general idea of the approach that I should take?

r experimental-design