Skip to content

Commit 446b070

Browse files
committed
add min option for RHR
1 parent 93d56be commit 446b070

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

R/calculate_RHR.R

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#' Calculate resting heart rate
22

33
#' @description
4-
#' Computes the resting heart rate (RHR) according to the mean heart rate
4+
#' Computes the resting heart rate (RHR) according to the mean/min heart rate
55
#' value between 3am to 7am. See reference for details on computing RHR
66

77
#' @usage
8-
#' calculate_RHR(data, tz)
8+
#' calculate_RHR(data, method, tz)
99

1010
#' @param data A DataFrame object with column names "id", "time", "hr".
11+
#' @param method \strong{Default: "mean".} Method for calculating RHR. Must be either "mean" for average or "min" for minimum HR during 3am-7am
1112
#' @param tz A character string specifying the time zone to be used. System-specific (see \code{\link{as.POSIXct}}), but " " is the current time zone, and "GMT" is UTC (Universal Time, Coordinated). Invalid values are most commonly treated as UTC, on some platforms with a warning
1213

1314
#' @return
@@ -24,9 +25,11 @@
2425
#' calculate_RHR(example_heart_1)
2526

2627

27-
calculate_RHR <- function(data, tz = "") {
28+
calculate_RHR <- function(data, method = c("mean", "min"), tz = "") {
2829
time = hr = id = hour = NULL
2930
rm(list = c('time', 'hr', 'id', 'hour'))
31+
method = match.arg(method, c("mean", "min"))
32+
3033
data$time <- as.POSIXct(data$time, format="%Y-%m-%d %H:%M:%S", tz = tz)
3134
data$hour <- as.numeric(format(data$time, "%H"))
3235
filtered_data <- subset(data, hour >= 3 & hour < 7)
@@ -36,8 +39,17 @@ calculate_RHR <- function(data, tz = "") {
3639
return(NULL) # Return NULL to indicate no data
3740
}
3841

39-
rhr_data <- filtered_data |>
40-
dplyr::group_by(id) |>
41-
dplyr::summarize(RHR = mean(hr, na.rm = TRUE), .groups = 'drop')
42-
return(rhr_data)
42+
43+
if(method == "mean"){
44+
rhr_data <- filtered_data |>
45+
dplyr::group_by(id) |>
46+
dplyr::summarize(RHR = mean(hr, na.rm = TRUE), .groups = 'drop')
47+
return(rhr_data)
48+
}
49+
else{
50+
rhr_data <- filtered_data |>
51+
dplyr::group_by(id) |>
52+
dplyr::summarize(RHR = min(hr, na.rm = TRUE), .groups = 'drop')
53+
return(rhr_data)
54+
}
4355
}

man/calculate_RHR.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)