super_gather: Gather columns into key-values pairs.

Description Usage Arguments Details Value Examples

Description

Partially collapse multiple columns into key-values pairs. Use super_gather() when you have have multiple wide-form variables that you want to partially collapse, but you want those gathered variables to stay in separate columns. Equivalent to calling gather separately for dataframes with the same key and different values, and joining them column-wise back into one dataframe. Requires the common key(s) to be spelled identically in names of columns to be gathered.

Usage

1
2
super_gather(data, key = "key", key_names = NULL, value_names = NULL,
  name_order, delim_nchar = 1)

Arguments

data

A data frame.

key

Name of new key column, as string.

key_names

Names of key levels present in column names to be gathered, as character vector. Specify either this or value_names but not both. If both this and value_names are specified, will only use key names to identify columns to gather.

value_names

Names of value levels present in column names to be gathered, as character vector. Specify either this or key_names but not both.

name_order

Which identifier comes first in existing colnames? Used to assist in identifying which columns to gather. Choose "key_first" or "value_first".

delim_nchar

How many characters delimit between key and value names? Defaults to 1.

Details

Implements a strategy akin to calling gather, separate, and spread in succession. As a result, may coerce some columns to character under the hood. Please pay attention to warnings.

Value

A data frame, with all indicated columns gathered, but separate.

Examples

1
2
3
4
5
6
7
8
data <- data.frame(id = 1:10,
value1_cond1 = "a",
value1_cond2 = "b",
value2_cond1 = 0L,
value2_cond2 = 1L)

super_gather(data, "condition", key_names = c("cond1", "cond2"), name_order = "value_first")
super_gather(data, "condition", value_names = c("value1", "value2"), name_order = "value_first")

cu-psych-r-users/cuPsychRTools documentation built on July 25, 2019, 5:39 p.m.