View source: R/pivot_wider_multicol.R
pivot_wider_multicol | R Documentation |
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.
pivot_wider_multicol( .dat, .id_cols, .cols, .wider_names, .wider_values, .wider_prefix = "wts_", .tag_name = "time", .values_fill = 0, .aggregator_fun = combine_vals, ... )
.dat |
A dataframe containing the data to be pivoted. |
.id_cols |
A string or |
.cols |
Either a string vector listing the names of the columns to be
pivoted or a |
.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 |
.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 |
.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 |
... |
Other parameters passed to |
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
.
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.