periods: Get a dataframe of sleep periods from a hypnogram, continuous...

View source: R/events.R

periodsR Documentation

Get a dataframe of sleep periods from a hypnogram, continuous or by stages.

Description

Get a dataframe of sleep periods from a hypnogram, continuous or by stages.

Usage

periods(
  hypnogram,
  mode = "continuous",
  stages = c("N1", "N2", "N3", "N4", "REM")
)

Arguments

hypnogram

A hypnogram dataframe. Dataframe must contain begin (POSIXt), end (POSIXt) and event (character) columns.

mode

Period mode. "continuous" computes periods of N1, N2, N3 or REM sleep, regardless of stage. "stages" computes periods of sleep by stage.

stages

Stages to include in periods. Defaults to 'c("N1", "N2", "N3", "N4", "REM")'.

Value

A dataframe of periods with their begin and stop times, duration and stages for stage mode.

Examples

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()

rsleep documentation built on Nov. 6, 2023, 1:06 a.m.