pivot_wider_multicol: pivot_wider_multicol

View source: R/pivot_wider_multicol.R

pivot_wider_multicolR Documentation

pivot_wider_multicol

Description

This function uses a combination of pivot_longer_multicol, pivot_wider, and group_by with summarise to expand the weights for each school a student attended to a single column per school ID.

Usage

pivot_wider_multicol(
  .dat,
  .id_cols,
  .cols,
  .wider_names,
  .wider_values,
  .wider_prefix = "wts_",
  .tag_name = "time",
  .values_fill = 0,
  .aggregator_fun = combine_vals,
  ...
)

Arguments

.dat

A dataframe containing the data to be pivoted.

.id_cols

A string or starts_with selection helper that identifies row ID.

.cols

Either a string vector listing the names of the columns to be pivoted or a starts_with selection helper.

.wider_names

A string. Identifies the "tagless" variable that will be used to construct names for the new expanded weight variables. For example, if the original, wide-format variable is "sch_id_1", the "tagless" variable will be "sch_id". Ultimately, this "tagless" name will be determined by the regex given by .capture_groups passed to pivot_longer_multicol.

.wider_values

A string. Identifies the "tagless" variable that will be used to add values to the new expanded weight variables. For example, if the original, wide-format variable is "sch_wt_1", the "tagless" variable will be "sch_wt". Ultimately, this "tagless" name will be determined by the regex given by .capture_groups passed to pivot_longer_multicol.

.tag_name

String. Indicates the variable name that should be given to the new variable containing the variable tags. Because this function was created to manage the longitudinal school mobility process, the argument defaults to "time".

.values_fill

A scalar. Indicates the value that should be filled in when values are missing. Defaults to 0.

.aggregator_fun

A function. Identifies the function to be used to aggregate over rows during group_by and summarise. Defaults to combine_vals.

...

Other parameters passed to pivot_longer_multicol.

Value

This function outputs a dataframe with the same number of rows as the original input, but with only the new columns created by expanding the weight variables plus the ID columns given in .id_cols. The new dataframe should have a number of columns equal to the unique values of .wider_names.

Examples

## Not run: 

# construct fake data
temp_dat <-
  tibble::tibble(
    x = rnorm(100),
    y = rnorm(100),
    z_1 = rep(c(1, 0), 50),
    z_2 = rep(c(5, 6, 7, 8), 25),
    g_1 = rnorm(100),
    g_2 = rnorm(100)
  )

temp_dat %>%
  pivot_wider_multicol(
    .dat = .,
    .id_cols = "x",
    .cols = tidyr::matches("_"),
    .wider_names = "z",
    .wider_values = "g",
    .wider_prefix = "new_"
  )


## End(Not run)

tessaleejohnson/corclus documentation built on Oct. 11, 2022, 3:46 a.m.