PreviousDate {Transition}R Documentation

Find Previous Test Date for Subject

Description

get_prev_dates() identifies the previous test date for individual subjects and timepoints in a longitudinal study.

add_prev_date() interpolates these previous test dates into a data frame for further analysis.

Usage

add_prev_date(
  object,
  subject = "subject",
  timepoint = "timepoint",
  result = "result"
)

get_prev_date(
  object,
  subject = "subject",
  timepoint = "timepoint",
  result = "result"
)

Arguments

object

a data.frame (or object coercible by as.data.frame() to a data frame) containing the data to be analysed.

subject

character, name of the column (of type integer or factor) identifying individual study subjects; default "subject".

timepoint

character, name of the column recording time points (as Dates) of testing of subjects; default "timepoint".

result

character, name of the column (of type ordered factor) recording test results; default "result".

Details

See Transitions details.

Value

add_prev_date()

A data.frame based on object, with an added column of class Date containing the values of the previous test dates.

get_prev_date()

An vector of length nrow(object), class Date, containing the values of the previous test dates ordered in the exact sequence of the subject and timepoint in object.

See Also

data.frame, Dates, ordered factor.

Other transitions: PreviousResult, Transitions, uniques()

Examples




 ## Continuing example from `add_transitions()`
  # subject, timepoint and result arguments all defaults and required types
Blackmore |> str()

  # Integer vector of the previous test dates
get_prev_date(Blackmore)

  # Add column of  previous test dates to data frame
add_prev_date(Blackmore) |> head(32)

rm(Blackmore)

###
## Example on formatting numeric values as R dates

#  Data frame containing year as numeric: 2018 to 2025
(df <- data.frame(
    subject = rep(1001:1003),
    timepoint = rep(2018:2025, each = 3),
    result = gl(3, 4, lab = c("jolly", "good", "show"), ordered = TRUE)
    ))

#  Convert to R dates
df <- transform(df,
           timepoint = as.Date(paste(timepoint, "01", "01", sep = "-"))
      )

  # Add column of test result transitions (defaults: cap = 0, modulate = 0)
(df <- add_transitions(df))

# Format R dates just to show the year
transform(df, timepoint = format(timepoint, "%Y"))

#  Data frame containing year and month as numeric: July 2024 to June 2025
(df <- data.frame(
           subject = 1001:1002,
           year = rep(2024:2025, each = 12),
           month = rep(c(7:12, 1:6), each = 2),
           result = gl(2, 3, lab = c("low", "high"), ordered = TRUE)
       ))

#  Convert to R dates
df <- transform(df, timepoint = as.Date(paste(year, month, "01", sep = "-")))


  # Add column of test result transitions (defaults: cap = 0, modulate = 0)
(df <- add_transitions(df))

# Format R dates just to show the month and year
transform(df, timepoint = format(timepoint, "%b-%Y"))

rm(df)


[Package Transition version 1.0.0 Index]