fixed_force: Enforce consistency in variables

Description Usage Arguments Details Examples

Description

This function forces values the variables in .var to take constant values within combinations of the variables in .within. fixed_force() will return a data frame with consistency enforced.

Usage

1
2
3
4
5
6
7
fixed_force(
  .df,
  .var = NULL,
  .within = NULL,
  .resolve = mode_order,
  .flag = NA
)

Arguments

.df

Data frame, pibble, or tibble.

.var

Quoted or unquoted variable(s) in .df that should be consistent. If not specified, uses all variables in .df that are not in .within.

.within

Quotes or unquoted variable(s) that the .var variables should be consistent within.

.resolve

Function capable of being passed to dplyr::summarize() that will be used to resolve inconsistencies. Or, set to 'drop' or any string to drop all inconsistent observations. By default, this will return the mode (ties use the first observed value).

.flag

String indicating the name of a new variable that flags any observations altered by fixed_force().

Details

Inconsistencies will be resolved by the function .resolve. Or, set .resolve to 'drop' (or any string, really) to drop all cases with inconsistency.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data(Scorecard)
# The variables pred_degree_awarded_ipeds and state_abbr should be constant within unitid
# However, sometimes colleges change what they offer.
# For the purpose of my analysis, though,
# I want to treat any changers as whatever they are most often (the mode).
# So let's enforce that with fixed_force
Scorecard <- fixed_force(Scorecard,
  .var = c(pred_degree_awarded_ipeds, state_abbr),
  .within = unitid, .flag = "changed"
)
# Did we catch any changers?
table(Scorecard$changed)
# We did!

pmdplyr documentation built on July 2, 2020, 4:08 a.m.