camr_unique_values: Extract Unique Values

View source: R/R04-Data_wrangling.R

camr_unique_valuesR Documentation

Extract Unique Values

Description

Function that filters out missing values and propagates the unique values associated with distinct cases in an index column. Useful, for example, taking values (like participant characteristics) that were collected at a single time point and propagating them across all time points.

Usage

camr_unique_values(
  dtf,
  index,
  variables,
  default = NA,
  propagate = TRUE,
  first = TRUE,
  warn_if_multiple = TRUE
)

Arguments

dtf

A data frame.

index

The column in dtf that serves as the grouping factor (i.e., each distinct case in index should correspond to a unique value). Non-standard evaluation possible.

variables

A character vector, all columns in dtf in which unique values should be extracted.

default

The default value to return if unique values are missing.

propagate

A logical value; if TRUE, propagates unique values by index to match the number of rows of dtf. If FALSE returns the unique value for each distinct case of index.

first

A logical value; if TRUE uses the first value found there is more than one value identified for a given case in index, otherwise uses the final value.

warn_if_multiple

A logical value; if TRUE returns a warning whenever multiple values are found for a given case in index.

Value

A vector of unique values associated with the distinct cases in index.

Author(s)

Kevin Potter

Examples

data( example_CAM_data_set )

example_CAM_data_set |>
  camr_unique_values(
    IDS.INT.Screening,
    "SBJ.INT.Age"
 )

example_CAM_data_set |>
  camr_unique_values(
    IDS.INT.Screening,
    "SBJ.INT.Age",
    propagate = FALSE
  )

# Can extract values over multiple columns
example_CAM_data_set |>
  camr_unique_values(
    IDS.INT.Screening,
    c( "SBJ.INT.Age", "SBJ.INT.Age_at_visit_1" ),
    propagate = FALSE,
    warn_if_multiple = FALSE
  )
# If 'warn_if_multiple' was TRUE function
# would appropriately generate warning for
# 3rd individual who has different ages
# recorded across variables


rettopnivek/camrprojects documentation built on March 26, 2024, 9:17 a.m.