Skip to contents

This function checks whether the input dataframe contains the required melatonin column and at least one valid time reference (datetime or time). It ensures that:

  • melatonin is numeric.

  • datetime (if present) is in POSIXct format.

  • time (if present) is in hms format.

  • If only time is provided (as POSIXct), it is renamed to datetime, and a time column is created from it.

  • If datetime exists but is of class hms, it is renamed to time, and an error is raised.

Usage

validate_df_structure(data)

Arguments

data

A dataframe (or tibble) expected to contain at least:

  • melatonin (numeric) – Melatonin concentration levels.

  • datetime (POSIXct) – Timestamps, or

  • time (hms or POSIXct) – Time of measurement.

Value

A modified dataframe with:

  • melatonin (numeric)

  • datetime (POSIXct) if available or converted

  • time (hms) if datetime was present and converted

Details

If time is in POSIXct, it is renamed to datetime, and time is extracted from it. If datetime is of class hms, it is renamed to time, and the function stops execution. If neither datetime nor time exist, an error is raised.

Examples

if (FALSE) { # \dontrun{
df <- tibble::tibble(
  datetime = as.POSIXct(c("2024-04-16 12:00:00", "2024-04-16 12:30:00")),
  melatonin = c(1.2, 1.5)
)
validate_df_structure(df)
} # }