Skip to contents

Adds a variable numbering the observations within each by group

Usage

derive_var_obs_number(
  dataset,
  by_vars = NULL,
  order = NULL,
  new_var = ASEQ,
  check_type = "none"
)

Arguments

dataset

Input dataset

The variables specified by the order and the by_vars parameter are expected.

by_vars

Grouping variables

Permitted Values: list of variables

order

Sort order

Within each by group the observations are ordered by the specified order.

Permitted Values: list of variables or functions of variables

new_var

Name of variable to create

The new variable is set to the observation number for each by group. The numbering starts with 1.

Default: ASEQ

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: "none"

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

Value

A dataset containing all observations and variables of the input dataset and additionally the variable specified by the new_var parameter.

Details

For each group (with respect to the variables specified for the by_vars parameter) the first or last observation (with respect to the order specified for the order parameter and the mode specified for the mode parameter) is included in the output dataset.

Examples

library(dplyr, warn.conflicts = FALSE)
library(admiral.test)
data("admiral_vs")

admiral_vs %>%
  select(USUBJID, VSTESTCD, VISITNUM, VSTPTNUM) %>%
  derive_var_obs_number(
    by_vars = exprs(USUBJID, VSTESTCD),
    order = exprs(VISITNUM, desc(VSTPTNUM))
  )
#> # A tibble: 29,643 x 5
#>    USUBJID     VSTESTCD VISITNUM VSTPTNUM  ASEQ
#>    <chr>       <chr>       <dbl>    <dbl> <int>
#>  1 01-701-1015 DIABP         1        817     1
#>  2 01-701-1015 DIABP         1        816     2
#>  3 01-701-1015 DIABP         1        815     3
#>  4 01-701-1015 DIABP         2        817     4
#>  5 01-701-1015 DIABP         2        816     5
#>  6 01-701-1015 DIABP         2        815     6
#>  7 01-701-1015 DIABP         3        817     7
#>  8 01-701-1015 DIABP         3        816     8
#>  9 01-701-1015 DIABP         3        815     9
#> 10 01-701-1015 DIABP         3.5      817    10
#> # … with 29,633 more rows