derive_var_extreme_flag: Add a Variable Flagging the First or Last Observation Within...

Description Usage Arguments Details Value Author(s) Examples

View source: R/derive_var_extreme_flag.R

Description

Add a variable flagging the first or last observation within each by group

Usage

1
2
3
4
5
6
7
8
9
derive_var_extreme_flag(
  dataset,
  by_vars,
  order,
  new_var,
  mode,
  filter = NULL,
  check_type = "warning"
)

Arguments

dataset

Input dataset

The variables specified by the by_vars parameter are expected.

by_vars

Grouping variables

Permitted Values: list of variables

order

Sort order

The first or last observation is determined with respect to the specified order.

Permitted Values: list of variables or functions of variables

new_var

Variable to add

The specified variable is added to the output dataset. It is set to "Y" for the first or last observation (depending on the mode) of each by group.

Permitted Values: list of name-value pairs

mode

Flag mode

Determines of the first or last observation is flagged.

Permitted Values: "first", "last"

filter

Filter for flag data

Only observations fulfilling the specified condition are taken into account for flagging. If the parameter is not specified, all observations are considered.

Permitted Values: a condition

check_type

Check uniqueness?

If "warning" or "error" is specified, the specified message is issued if the observations of the input dataset are not unique with respect to the by variables and the order.

Default: "warning"

Permitted Values: "none", "warning", "error"

Details

For each group (with respect to the variables specified for the by_vars parameter), new_var is set to "Y" for the first or last observation (with respect to the order specified for the order parameter and the flag mode specified for the mode parameter). Only observations included by the filter parameter are considered for flagging. Otherwise, new_var is set to NA.

Value

The input dataset with the new flag variable added

Author(s)

Stefan Bundfuss

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
library(dplyr, warn.conflicts = FALSE)
library(admiral.test)
data("vs")

# Flag last value for each patient, test, and visit, baseline observations are ignored
vs %>%
  derive_var_extreme_flag(
    by_vars = vars(USUBJID, VSTESTCD, VISIT),
    order = vars(VSTPTNUM),
    new_var = LASTFL,
    mode = "last",
    filter = VISIT != "BASELINE"
  ) %>%
  arrange(USUBJID, VSTESTCD, VISITNUM, VSTPTNUM) %>%
  select(USUBJID, VSTESTCD, VISIT, VSTPTNUM, VSSTRESN, LASTFL)

# Baseline (ABLFL) examples:

input <- tibble::tribble(
  ~STUDYID, ~USUBJID, ~PARAMCD,  ~AVISIT,    ~ADT,                 ~AVAL, ~DTYPE,
  "TEST01", "PAT01",  "PARAM01", "BASELINE", as.Date("2021-04-27"), 15.0, NA,
  "TEST01", "PAT01",  "PARAM01", "BASELINE", as.Date("2021-04-25"), 14.0, NA,
  "TEST01", "PAT01",  "PARAM01", "BASELINE", as.Date("2021-04-23"), 15.0, "AVERAGE",
  "TEST01", "PAT01",  "PARAM01", "WEEK 1",   as.Date("2021-04-27"), 10.0, "AVERAGE",
  "TEST01", "PAT01",  "PARAM01", "WEEK 2",   as.Date("2021-04-30"), 12.0, NA,

  "TEST01", "PAT02",  "PARAM01", "SCREENING",as.Date("2021-04-27"), 15.0, "AVERAGE",
  "TEST01", "PAT02",  "PARAM01", "BASELINE", as.Date("2021-04-25"), 14.0, "AVERAGE",
  "TEST01", "PAT02",  "PARAM01", "BASELINE", as.Date("2021-04-23"), 15.0, "AVERAGE",
  "TEST01", "PAT02",  "PARAM01", "WEEK 1",   as.Date("2021-04-27"), 10.0, "AVERAGE",
  "TEST01", "PAT02",  "PARAM01", "WEEK 2",   as.Date("2021-04-30"), 12.0, "AVERAGE",

  "TEST01", "PAT01",  "PARAM02", "SCREENING",as.Date("2021-04-27"), 15.0, "AVERAGE",
  "TEST01", "PAT01",  "PARAM02", "SCREENING",as.Date("2021-04-25"), 14.0, "AVERAGE",
  "TEST01", "PAT01",  "PARAM02", "SCREENING",as.Date("2021-04-23"), 15.0, NA,
  "TEST01", "PAT01",  "PARAM02", "BASELINE", as.Date("2021-04-27"), 10.0, "AVERAGE",
  "TEST01", "PAT01",  "PARAM02", "WEEK 2",   as.Date("2021-04-30"), 12.0, NA,

  "TEST01", "PAT02",  "PARAM02", "SCREENING",as.Date("2021-04-27"), 15.0, NA,
  "TEST01", "PAT02",  "PARAM02", "BASELINE", as.Date("2021-04-25"), 14.0, NA,
  "TEST01", "PAT02",  "PARAM02", "WEEK 1",   as.Date("2021-04-23"), 15.0, NA,
  "TEST01", "PAT02",  "PARAM02", "WEEK 1",   as.Date("2021-04-27"), 10.0, NA,
  "TEST01", "PAT02",  "PARAM02", "BASELINE", as.Date("2021-04-30"), 12.0, NA
)

# Last observation
derive_var_extreme_flag(
  input,
  by_vars = vars(USUBJID, PARAMCD),
  order = vars(ADT),
  new_var = ABLFL,
  mode = "last",
  filter = AVISIT == "BASELINE"
)

# Worst observation - Direction = High
derive_var_extreme_flag(
  input,
  by_vars = vars(USUBJID, PARAMCD),
  order = vars(AVAL, ADT),
  new_var = ABLFL,
  mode = "last",
  filter = AVISIT == "BASELINE"
)

# Worst observation - Direction = Lo
derive_var_extreme_flag(
  input,
  by_vars = vars(USUBJID, PARAMCD),
  order = vars(desc(AVAL), ADT),
  new_var = ABLFL,
  mode = "last",
  filter = AVISIT == "BASELINE"
)

# Average observation
derive_var_extreme_flag(
  input,
  by_vars = vars(USUBJID, PARAMCD),
  order = vars(ADT, desc(AVAL)),
  new_var = ABLFL,
  mode = "last",
  filter = AVISIT == "BASELINE" & DTYPE == "AVERAGE"
)

epijim/admiral documentation built on Feb. 13, 2022, 12:15 a.m.