get_distinct: Get distinct values on different rows

View source: R/get_distinct.R

get_distinctR Documentation

Get distinct values on different rows

Description

A wrapper around 'dplyr::group_by dplyr::ungroup()'. I use this all the time when I am working with data from REDCap. This is meant to make my life easier and clean up my code.

Usage

get_distinct(data, id, ..., fill_direction = "downup")

Arguments

data

A data.frame or tibble.

id

Unquoted name of grouping variable. Typically a subject or record id.

...

A selection of columns. If empty, nothing happens. You can supply bare variable names, select all variables between x and z with x:z, exclude y with -y. For more selection options, see the 'dplyr::select()' documentation.

fill_direction

Direction in which to fill missing values. Currently either "down" (the default), "up", "downup" (i.e. first down and then up) or "updown" (first up and then down).

Value

A tbl_df

Examples

library(dplyr)
library(tibble)

df <- tibble::tribble(
  ~id,            ~form, ~age,     ~sex,               ~ethnicity, ~trt,
  1L,            "age",  25L,       NA,                       NA,   NA,
  1L,            "sex",   NA,   "Male",                       NA,   NA,
  1L, "race_ethnicity",   NA,       NA,     "Hispanic or Latino",   NA,
  1L,      "treatment",   NA,       NA,                       NA,  "A",
  2L,            "age",  32L,       NA,                       NA,   NA,
  2L,            "sex",   NA, "Female",                       NA,   NA,
  2L, "race_ethnicity",   NA,       NA, "Not Hispanic or Latino",   NA,
  2L,      "treatment",   NA,       NA,                       NA,  "B"
)

df

df %>%
  get_distinct(data = .,
               id = id,
               age, sex, ethnicity, trt)
df %>%
  get_distinct(data = .,
               id = id,
               dplyr::starts_with("ag"))

df %>%
  get_distinct(data = .,
               id = id,
               dplyr::one_of("age", "sex"))

emilelatour/redcaptools documentation built on March 21, 2023, 3:35 p.m.