super_spread: Spread key-values pairs across multiple columns.

Description Usage Arguments Value Examples

Description

Spread key-values pairs, where one key maps analogously onto multiple values, into across multiple columns. Equivalent to calling spread separately for dataframes with the same key and different values, and joining them column-wise back into one dataframe.

Usage

1
2
super_spread(data, key, ..., name_order = "value_first", sep = "_",
  fill = NA, convert = FALSE, drop = TRUE)

Arguments

data

A data frame.

key

Column names, unquoted, passed in using vars_select. Accepts select_helpers. To specify multiple key columns all by name, group unquoted variable names using c.

...

A selection of columns containing values to be spread. again, unquoted. Accepts select_helpers. Variable names do not need to be grouped using c; all names passed in as ... are assumed to be value columns.

name_order

Which identifier comes first in final colname? Choose "key_first" or "value_first". Defaults to "value_first".

sep

Separator to use between values, ultimately ending up in colnames. Passed to unite.

fill

If set, missing values will be replaced with this value. Note that there are two types of missingness in the input: explicit missing values (i.e. NA), and implicit missings, rows that simply aren't present. Both types of missing value will be replaced by fill.

convert

If TRUE, type.convert() with asis = TRUE will be run on each of the new columns. This is useful if the value column was a mix of variables that was coerced to a string. If the class of the value column was factor or date, note that will not be true of the new columns that are produced, which are coerced to character before type conversion.

drop

If FALSE, will keep factor levels that don't appear in the data, filling in missing combinations with fill.

Value

A data frame, "fully" spread by all indicated columns.

Examples

1
2
3
4
5
6
7
data <- expand.grid(
id = 1L:10L,
condition = c("a", "b"),
value_1 = 1L,
value_2 = 0L)

super_spread(data, condition, value_1:value_2)

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