The objective is to derive a set of tables, one for each 'unit', with the Unit and the randomly selected row then a sequence of n equal to 'Vs' beginning with 'selectV', incremented by 'Vcount', 'Vs' times. This is a modified two stage random sampling activity, with an ordered component. The issue is not the sampling but what should be trivial data tidying. I can do this one at a time using classical R but am struggling with the transformations in dplyr/purr. A simple map() of a function should suffice but it has thus far defeated me :-(.
First the minimal data. Rn in the crop row count and Vn is the item count for each row (length). Rs is the desired count of rows to select and Vs, the desired item count selected within each row. Rather than selecting random numbers within the whole row, a start item number is defined and then added to derive the remaining item (plant) positions for sampling. the purpose is to make it easy for a worker and to reduce errors by simplifying the procedure.
library(tidyverse) library(purrr) xdat <- tibble(unit = c("A", "B"), Rn = c(15,20), Vn = c(50, 100), Rs = c(3, 4), Vs = c(3, 5)) xdat <- xdat %>% group_by(unit) %>% mutate(selectR = list(sort(sample(seq(1,Rn,1),Rs))), selectV = list(sample(seq(1,trunc(Vn/Vs),1),Rs)), Vcount = trunc(Vn/Vs))
Then the expected output
outDat1 <- tibble( unit = rep("A", 3), row = sort(sample(seq.int(1, 15, 1),3)), V1 = sample(seq.int(1, 16, 1),3), V2 = V1 + 16, V3 = V2 + 16 ) outDat2 <- tibble( unit = rep("B", 4), row = sort(rep(sample(seq.int(1, 20, 1),4))), V1 = sample(seq.int(1, 20, 1),4), V2 = V1 + 20, V3 = V2 + 20, V4 = V3 + 20, V5 = V4 + 20 )