Validate and Standardize Dataframe Structure for Melatonin Analysis
Source:R/validate_df_structure.R
validate_df_structure.RdThis function checks whether the input dataframe contains the required melatonin
column and at least one valid time reference (datetime or time). It ensures that:
melatoninis numeric.datetime(if present) is inPOSIXctformat.time(if present) is inhmsformat.If only
timeis provided (asPOSIXct), it is renamed todatetime, and atimecolumn is created from it.If
datetimeexists but is of classhms, it is renamed totime, and an error is raised.
Value
A modified dataframe with:
melatonin(numeric)datetime(POSIXct) if available or convertedtime(hms) ifdatetimewas 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)
} # }