This simple function will add a identification field MID to the table supplied for unique combinations of the grouping columns supplied.

MatchTreatments(Data, Match.Cols, Focal.Col)

Arguments

Data

An ERA dataset (e.g. ERAg::ERACompiled)

Match.Cols

A character vector of column names that must all contain identical values for rows in the dataset to match (i.e. share the same MID)

Focal.Col

A character vector of a single column name; the number of unique values of Focal.Col for each combination of value of Match.Cols values (MID) are returned along with a concatenated string of the values.

Value

A data.table with three columns appended::

  • MID = a unique identifier for each combination of the grouping variables (column names) supplied Match.Cols

  • N.Focal= the number of unique values of Focal.Col for each value of MID

  • Focal.List = unique values of Focal.Col concatenated with a - delim calculate for each value MID

Details

For example, if you wish to know which outcomes are shared between the same pairings of experimental and control treatments you would specify Match.Cols = c("Code","Site.ID","C.Descrip","T.Descrip","SubPrName","SubPrName.Base","M.Year") and Focal.Col = Out.SubInd this gives a unique MID to observations that have the same control and treatment practices, location, growing season and study. The N.Focal column of the output dataset tells you how many unique values of Out.SubInd are present in each group, and the Focal.List field in the output data.table concatenates the unique values in alphabetical order with a - delimiter.