extract_observations {stenR}R Documentation

Extract observations from data

Description

On basis of GroupAssignment extract one or many groups from provided data.frame

Usage

extract_observations(
  data,
  groups,
  group_names = NULL,
  extract_mode = c("list", "data.frame"),
  strict_names = TRUE,
  simplify = FALSE,
  id
)

Arguments

data

data.frame from which to extract data

groups

GroupAssignment object on basis of which extract the data.

group_names

character vector of group names which to extract. If kept as default NULL, all groups are extracted.

extract_mode

character: either list or data.frame. When kept as default: list, data is extracted as named list: where the name of list is name of the groups, and each one contains data.frame with observations. When data.frame is used, then assigned data is returned as one data.frame with new column named: GroupAssignment, declaring the group.

strict_names

boolean If TRUE, then intersected groups are extracted using strict strategy: group_names need to be provided in form: "group1:group2". If FALSE, then intersected groups will be taken into regard separately, so eg. when "group1" is provided to group_names, all of: "group1:group2", "group1:group3", "group1:groupN" will be extracted. Defaults to TRUE

simplify

boolean If TRUE, then when only one group is to be returned, it returns as data.frame without taking into account value of group_name argument. Defaults to FALSE

id

If GroupAssignment mode is id, and you want to overwrite the original id_col, provide a name of the column there. If none is provided, then the default id_col will be used.

Value

either:

See Also

Other observation grouping functions: GroupAssignment(), intersect_GroupAssignment()

Examples

#### Create Group Conditions ####
sex_grouping <- GroupConditions(
  conditions_category = "Sex",
  "M" ~ sex == "M",
  "F" ~ sex == "F",
  "O" ~ !sex %in% c("M", "F")
)

age_grouping <- GroupConditions(
  conditions_category = "Age",
  "to 20" ~ age < 20,
  "20 to 40" ~ age >= 20 & age <= 40,
  "41 to 60" ~ age > 40 & age <= 60,
  "above 60" ~ age > 60
)

#### Create Group Assignement ####
# can be done both with indices, so later this can be used only on the same data
# or with IDs - so later it can be done with only subset or transformed original data

sex_assignment <- GroupAssignment(HEXACO_60, sex_grouping, id = "user_id")
age_assignment <- GroupAssignment(HEXACO_60, age_grouping, id = "user_id")

#### Intersect two Group Assignement ###
# with additional forcing set
intersected <- intersect_GroupAssignment(
  sex_assignment,
  age_assignment,
  force_exhaustive = TRUE,
  force_disjoint = FALSE
)

extracted <- extract_observations(
  HEXACO_60,
  groups = intersected,
  group_names = c("M"),
  extract_mode = "data.frame",
  strict_names = FALSE)

# only groups created from "M" group were extracted
# groups without observations were dropped
table(extracted$GroupAssignment)

[Package stenR version 0.6.9 Index]