baytrends (version 1.1.0)

group2row: Restructure Data

Description

Combine data from several rows into a single row based on common data in selected columns.

Usage

group2row(data, carryColumns, splitColumn, collectColumns)

Arguments

data

a data frame containing the columns to be combined.

carryColumns

the names of the columns that form a row in the output dataset. Each unique combination of values in these columns will be a new row in the output dataset.

splitColumn

the name of a single column. For each unique value in splitColumn and for each column in collectColumns, a new column is created in the output.

collectColumns

the names of the columns to be collected. See Details.

Details

The function group2row combines data from several rows into a single row. Certain columns in the input dataset are said to be "collected." Other columns may be "carried" into the output dataset by listing them in carryColumns. A new row will be created for each unique combination of values in the carryColumns. The output row consists of the carried columns plus new columns that are named by the unique values in the splitColumn concatenated with the names in the collectColumns. The number of columns in the output data frame is equal to the number of carryColumns plus the number of unique values in the splitColumn times the number of names in the collectColumns.

The strategy for collecting columns is to use a set of index values defined by the splitColumn. The maximum number of input rows collected for each output row is equal to the number of unique values defined in the splitColumn. The splitColumn is used to identify a column from the input data that contains output column information. If a row of input has a value in this column that matches one of the index values, then that row's data will be included in the output in the column positions corresponding to the matched index. The index values are concatenated with the input column names of the collected columns to derive output column names.

Added from smwrBase.

Examples

Run this code
# NOT RUN {
library(smwrData)
data(QWstacked)
group2row(QWstacked, c("site_no", "sample_dt", "sample_tm"), "parm_cd", 
 c("result_va", "remark_cd"))
# }

Run the code above in your browser using DataLab