R/events.R
periods.RdGet a dataframe of sleep periods from a hypnogram, continuous or by stages.
periods(
hypnogram,
mode = "continuous",
stages = c("N1", "N2", "N3", "N4", "REM")
)A hypnogram dataframe. Dataframe must contain begin (POSIXt), end (POSIXt) and event (character) columns.
Period mode. "continuous" computes periods of N1, N2, N3 or REM sleep, regardless of stage. "stages" computes periods of sleep by stage.
Stages to include in periods. Defaults to `c("N1", "N2", "N3", "N4", "REM")`.
A dataframe of periods with their begin and stop times, duration and stages for stage mode.
tryCatch({
library(ggplot2)
download.file(
"https://rsleep.org/data/hypnodensity.csv",
"hypnodensity.csv")
hypnodensity <- read.csv2("hypnodensity.csv")
unlink("hypnodensity.csv")
events <- hypnogram(hypnodensity)
periods_continuous <- periods(events, mode = "continuous")
ggplot(periods_continuous, aes(x=duration)) + geom_histogram(bins = 30)
periods_stages <- periods(events, mode = "stages")
ggplot(periods_stages, aes(x=event,y=duration,color=event)) + geom_boxplot()
}, error = function(e) {
print("Error executing this example, check your internet connection.")
})