Learn R Programming

descsuppR (version 1.2)

convertColumnHeading: Convert Grouping Column Values into Headings with ddply

Description

For each unique value in the specified column of a data frame, creates a "subtable" where that value is moved as a group heading (first row) and the column is blanked for the remaining rows. Optionally, can "move along" specified metadata columns, shifting their first element to the last row.

Usage

convertColumnHeading(df, col, movealong = NULL)

Value

A data frame formatted with expanded headings per group.

Arguments

df

A data frame to be processed.

col

Character. Name of the column in df to use for grouping and converting to headings.

movealong

Optional character vector of column names. For each group, these columns are rotated, placing their first value after the last row.

Author

Andreas Leha

Details

The function uses plyr::ddply to split the data frame by the levels of col. For each group, - Moves the grouping value to a heading (top row) - Adds an empty row for formatting - Optionally rotates metadata columns specified in movealong This can be useful for preparing tables for presentation or reporting.

Examples

Run this code
df <- data.frame(Group = c("A", "A", "B", "B"), Data = 1:4, Meta = c("x", "y", "z", "w"))
convertColumnHeading(df, "Group")
convertColumnHeading(df, "Group", movealong = "Meta")

Run the code above in your browser using DataLab