Assign to groups based on GroupConditions
GroupAssignment.Rd
Using GroupConditions object, assign observations to one
of the groups. It can export either indices of the observations, or their
unique ID: if column name is provided in id
argument. Mostly used internally
by more complex functions and R6 classes
, but could also be useful
on its own.
Arguments
- data
data.frame containing observations
- conditions
GroupConditions object
- id
character name of the column containing unique ID of the observations to assign to each group. If not provided, indices will be used instead.
- force_disjoint
boolean indicating if groups disjointedness should be forced in case when one observation would pass conditions for more than one group. If
TRUE
, the first condition which will be met will indicate the group the observation will be assigned to. If not provided, the default fromconditions
will be used- force_exhaustive
boolean indicating if groups exhausiveness should be forced in case when there are observations that don't pass any of the provided conditions. If
TRUE
, then they will be assigned to.NA
group. If not provided, the default fromconditions
will be used- skip_faulty
boolean should the faulty
condition
be skipped? IfFALSE
as in default, error will be produced. Faultiness of seemingly correct condition may be caused by variable names to not be present in thedata
.- .all
boolean. If
TRUE
, then additional group named.all
will be created, which will contain all observations. Useful when object will be used for creation ofGroupedFrequencyTable()
- ...
additional arguments to be passed to or from method
- x
object
- object
GroupAssignment
object
See also
Other observation grouping functions:
extract_observations()
,
intersect_GroupAssignment()
Examples
age_grouping <- GroupConditions(
conditions_category = "Age",
"to 20" ~ age < 20,
"20 to 40" ~ age >= 20 & age <= 40,
"40 to 60" ~ age >= 40 & age < 60
)
# on basis of GroupConditions create GroupAssignment
age_assignment <- GroupAssignment(
data = HEXACO_60,
age_grouping)
#> Warning: ! Some observations were not assigned on provided condition. Set the
#> `force_exhaustive = TRUE` to gather them in `.NA` group.
print(age_assignment)
#> <GroupAssignment>
#> Total assigned: 202
#> Mode: "index"
#> Groups:
#> "to 20", "20 to 40", and "40 to 60"
# overwrite the default settings imposed by `GroupConditions`
age_assignment_forced <- GroupAssignment(
data = HEXACO_60,
age_grouping,
force_exhaustive = TRUE)
summary(age_assignment_forced)
#> <GroupAssignment>
#> Status
#> • Mode: index
#> • Total assigned: 204
#> • Disjointedness: TRUE; Forced: TRUE
#> • Exhaustiveness: TRUE; Forced: TRUE
#> Assignment [tested vars: `age`]
#> • Group: to 20 [obs: 20]
#> • Group: 20 to 40 [obs: 173]
#> • Group: 40 to 60 [obs: 9]
#> • Group: .NA [obs: 2]
# you can also use other unique identifier from your data
age_assignment_forced_w_id <- GroupAssignment(
data = HEXACO_60,
age_grouping,
id = "user_id",
force_exhaustive = TRUE)
summary(age_assignment_forced_w_id)
#> <GroupAssignment>
#> Status
#> • Mode: id [default ID: `user_id`]
#> • Total assigned: 204
#> • Disjointedness: TRUE; Forced: TRUE
#> • Exhaustiveness: TRUE; Forced: TRUE
#> Assignment [tested vars: `age`]
#> • Group: to 20 [obs: 20]
#> • Group: 20 to 40 [obs: 173]
#> • Group: 40 to 60 [obs: 9]
#> • Group: .NA [obs: 2]