Skip to content

Commit

Permalink
Added a function to compute peak step accumulation metrics.
Browse files Browse the repository at this point in the history
  • Loading branch information
pydemull committed Apr 26, 2024
1 parent 3b67360 commit 8404169
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
19 changes: 19 additions & 0 deletions R/compute_peak_step_acc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#' Compute mean step accumulation (per min) from a given number of the best continous
#' or discontinuous minutes
#'
#' @param x A vector of steps data, each value corresponding to a total of steps
#' in a minute.
#' @param n An integer value setting the number of minutes to be used to compute
#' the metric.
#'
#' @return A numeric value.
#'
compute_peak_step_acc <- function(x, n) {

vec_length <- length(head(sort(x, decreasing = TRUE), n = n))

if (vec_length < n) {NA}
else {
round(mean(head(sort(x, decreasing = TRUE), n = n), na.rm = TRUE), 2)
}
}
24 changes: 13 additions & 11 deletions R/recap_by_day.R
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ recap_by_day <- function(
# Defining a function for getting a NA value when computing a step-based metric
# from a vector with no non-missing values and the max() function
# Retrieved from: https://stackoverflow.com/questions/24519794/r-max-function-ignore-na
my_max <- function(x) ifelse( !all(is.na(x)), max(x, na.rm = TRUE), NA)
my_max <- function(x) ifelse(!all(is.na(x)), max(x, na.rm = TRUE), NA)


# Getting step-based metrics for each day of measurement (using a 1-min epoch)
Expand Down Expand Up @@ -212,11 +212,12 @@ recap_by_day <- function(
max_steps_20min = round(my_max(zoo::rollmean(steps, align = "center", k = 20L, fill = NA)), 2),
max_steps_5min = round(my_max(zoo::rollmean(steps, align = "center", k = 5L, fill = NA)), 2),
max_steps_1min = round(my_max(zoo::rollmean(steps, align = "center", k = 1L, fill = NA)), 2),
peak_steps_60min = round(mean(head(sort(steps, decreasing = TRUE), n = 60L), na.rm = TRUE), 2),
peak_steps_30min = round(mean(head(sort(steps, decreasing = TRUE), n = 30L), na.rm = TRUE), 2),
peak_steps_20min = round(mean(head(sort(steps, decreasing = TRUE), n = 20L), na.rm = TRUE), 2),
peak_steps_5min = round(mean(head(sort(steps, decreasing = TRUE), n = 5L), na.rm = TRUE), 2),
peak_steps_1min = round(mean(head(sort(steps, decreasing = TRUE), n = 1L), na.rm = TRUE), 2),

peak_steps_60min = compute_peak_step_acc(x = .data$steps, n = 60L),
peak_steps_30min = compute_peak_step_acc(x = .data$steps, n = 30L),
peak_steps_20min = compute_peak_step_acc(x = .data$steps, n = 20L),
peak_steps_5min = compute_peak_step_acc(x = .data$steps, n = 5L),
peak_steps_1min = compute_peak_step_acc(x = .data$steps, n = 1L)
)
} else {

Expand Down Expand Up @@ -245,11 +246,12 @@ recap_by_day <- function(
max_steps_20min = round(my_max(zoo::rollmean(steps, align = "center", k = 20L, fill = NA)), 2),
max_steps_5min = round(my_max(zoo::rollmean(steps, align = "center", k = 5L, fill = NA)), 2),
max_steps_1min = round(my_max(zoo::rollmean(steps, align = "center", k = 1L, fill = NA)), 2),
peak_steps_60min = round(mean(head(sort(steps, decreasing = TRUE), n = 60L), na.rm = TRUE), 2),
peak_steps_30min = round(mean(head(sort(steps, decreasing = TRUE), n = 30L), na.rm = TRUE), 2),
peak_steps_20min = round(mean(head(sort(steps, decreasing = TRUE), n = 20L), na.rm = TRUE), 2),
peak_steps_5min = round(mean(head(sort(steps, decreasing = TRUE), n = 5L), na.rm = TRUE), 2),
peak_steps_1min = round(mean(head(sort(steps, decreasing = TRUE), n = 1L), na.rm = TRUE), 2),

peak_steps_60min = compute_peak_step_acc(x = .data$steps, n = 60L),
peak_steps_30min = compute_peak_step_acc(x = .data$steps, n = 30L),
peak_steps_20min = compute_peak_step_acc(x = .data$steps, n = 20L),
peak_steps_5min = compute_peak_step_acc(x = .data$steps, n = 5L),
peak_steps_1min = compute_peak_step_acc(x = .data$steps, n = 1L)
)
}

Expand Down
23 changes: 23 additions & 0 deletions man/compute_peak_step_acc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8404169

Please sign in to comment.