Derive and export indivual tables of two-stage random sequential samples for unequal sets

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.

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(, 15, 1),3)),
  V1 = sample(, 16, 1),3),
  V2 = V1 + 16,
  V3 = V2 + 16
outDat2 <- tibble(
  unit = rep("B", 4),
  row = sort(rep(sample(, 20, 1),4))),
  V1 = sample(, 20, 1),4),
  V2 = V1 + 20,
  V3 = V2 + 20,
  V4 = V3 + 20,
  V5 = V4 + 20

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.