Extract and Pad a Sub-Trial from a Field Trial Layout
padTrial.RdGiven a field trial data frame that mixes multiple plot types (e.g. test
lines, checks, guard rows), padTrial() extracts the rectangular sub-trial
occupied by a target plot type and, optionally, inserts placeholder rows for
any missing grid positions within that rectangle.
Steps performed per group (block):
Check for duplicate Row × Column positions and stop with an informative error if any are found.
Identify the bounding box (min/max of the two spatial coordinates) for all plots matching
match.Subset the data to that bounding rectangle (dropping checks and guards that fall outside it).
If
pad = TRUE, detect any Row × Column cells that are absent within the bounding box and insert placeholder rows for them, carrying identifier columns specified inkeepand writingfill_valueinto every other character or factor column.
An add column is appended to the result, taking the value "old" for
original rows and "new" for inserted padding rows.
Usage
padTrial(
data,
pattern = "Row:Column",
match = "DH",
split = "Block",
pad = TRUE,
keep = split,
fill_value = "Blank",
type_col = "Type",
verbose = FALSE
)Arguments
- data
Data frame containing the trial layout.
- pattern
Character string of the form
"Var1:Var2"naming the two spatial coordinate columns (rows and columns of the field grid). Default"Row:Column".- match
Character vector of values in
type_colthat define the target sub-trial (e.g. test lines). The bounding box is computed from these plots only. Default"DH".- split
Character vector of one or more column names used to define independent groups (blocks) for extraction and padding. Pass
NULLto treat the entire dataset as a single block. Default"Block".- pad
Logical. If
TRUE(default), missing grid cells within the bounding box are inserted as placeholder rows.- keep
Character vector of column names whose values should be carried into the padding rows (must be constant within each group). Defaults to
split. Ignored whensplit = NULL.- fill_value
Character string written into every character or factor column of the padding rows, except for columns named in
keepand the two spatial coordinate columns. Numeric columns always remainNA. Default"Blank".- type_col
Name of the column in
datathat identifies plot types (e.g."DH","Check","Guard"). Default"Type".- verbose
Logical. If
TRUE, prints a per-group message reporting the detected bounding box and the number of cells padded. DefaultFALSE.
Value
A data frame with the same columns as data plus an add column
("old" / "new"), ordered by the first then second spatial coordinate.
The spatial coordinate columns are re-levelled as factors in ascending
numeric order across the combined output. When multiple split columns
are supplied the temporary grouping key column is dropped before returning.
Examples
if (FALSE) { # \dontrun{
# Single blocking column
result <- padTrial(trial_df,
pattern = "Row:Column",
match = "DH",
split = "Block",
fill_value = "Blank",
verbose = TRUE)
# No blocking -- treat whole dataset as one group
result <- padTrial(trial_df, split = NULL)
# Multi-environment: process each Site x Block independently
result <- padTrial(trial_df, split = c("Site", "Block"))
# Custom type column name
result <- padTrial(trial_df, type_col = "PlotType", match = "Line")
table(result$add) # count original vs padded rows
subset(result, add == "new") # inspect the inserted blank plots
} # }