Learn R Programming

splinetrials (version 0.1.1)

bin_timepoints: Categorize Observed Timepoints According to Scheduled Timepoints

Description

Create an ordered factor from a vector of observed values, associating each observed value with the level corresponding to a vector of expected/scheduled values.

Usage

bin_timepoints(
  observed,
  scheduled = unique(observed[!is.na(observed)]),
  breaks = c(-Inf, midpoints(scheduled), Inf),
  labels = make_visit_labels(seq_along(scheduled) - 1),
  ...
)

Value

And [ordered] factor with the same length as observed.

Arguments

observed

A numeric vector of values.

scheduled

A numeric vector of unique, finite values. Length must be at least 2. The default is to take the unique, finite values of observed.

breaks

A numeric vector of unique values. -Inf and Inf are valid. Passed to cut(). The default is to take the midpoints of scheduled and to put them in between c(-Inf, [Inf]).

labels

A vector of labels for the resulting ordered factor. Passed to cut(). Must have length() equal to scheduled. Defaults to "Baseline" as the first level's label and "Visit#" for all subsequent levels, where # is the numeric index of the timepoint minus 1.

...

Additional arguments passed to cut().

Examples

Run this code
observed_timepoints <- c(0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
scheduled_timepoints <- c(0, 1, 2, 3, 4, 5, 10, 15, 20, 30, 50, 75)
bin_timepoints(
  observed_timepoints,
  scheduled = scheduled_timepoints
)

bin_timepoints(
  observed_timepoints,
  scheduled = scheduled_timepoints,
  breaks = c(-Inf, 0.1, 1.5, 2.5, 3.5, 4.4, 7, 11, 15.1, 21, 31, 58, 80)
)

bin_timepoints(
  observed_timepoints,
  scheduled = scheduled_timepoints,
  labels = month.name
)

bin_timepoints(
  observed_timepoints,
  scheduled = scheduled_timepoints,
  labels = make_visit_labels(scheduled_timepoints, visit = "Week")
)

bin_timepoints(observed_timepoints)

Run the code above in your browser using DataLab