Learn R Programming

LightLogR (version 0.10.0)

Circular2Time: Convert circular time columns to hms

Description

Convert circular time columns to hms

Usage

Circular2Time(
  dataset,
  cols = dplyr::where(circular::is.circular),
  silent = FALSE
)

Value

The input dataset with converted circular columns as time (hms) columns. With the default settings, if no circular column exists, input and output will be identical.

Arguments

dataset

A data.frame with circular columns representing time of day.

cols

The column names to convert. Expects a symbol. The default will convert all circular columns. If uncertain whether columns exist in the dataset, use dplyr::any_of().

silent

Logical on whether no message shall be shown if input and output are identical. Defaults to FALSE (i.e., a message is shown).

Examples

Run this code
times <- lubridate::as_datetime("2023-01-01 10:00:00") + lubridate::hours(0:2)
times
circular_times <- Datetime2Time(tibble::tibble(Timestamp = times), circular = TRUE)
circular_times
Circular2Time(circular_times)

#if times are not circular, then an averaging can be problematic across midnight:
selected_times <- 
sample.data.environment |> 
sample_groups() |> 
dplyr::slice(c(40:43, 51838:51840))
selected_times

#a simple averaging will lead to a nonsensical value, e.g. if this should 
#calculate average sleep timing: ~10:00 in the morning
selected_times |> summarize_numeric()

#by converting it to a circular beforehand, averaging works as expected: 
#~3 minutes after midnight
selected_times |> 
summarize_numeric(Datetime2Time.circular = TRUE) |> 
Circular2Time()

Run the code above in your browser using DataLab