R/stopwatch.R

Defines functions stopwatch

Documented in stopwatch

#' Calculate raw score for stop watch test
#'
#' This function calculates raw score from json recording of "stopwatch" test.
#'
#' @param data Raw data of class \code{data.frame}.
#' @param ... Other input argument for future expansion.
#' @return The raw score calculated of \code{double} type.
#'
#' @importFrom magrittr %>%
#' @importFrom rlang .data
#' @export
stopwatch <- function(data, ...) {
  if (!utils::hasName(data, "RT")) {
    warning("Reaction time (`RT`) variable is required.")
    return(NA_real_)
  }
  data_valid <- data %>%
    dplyr::filter(.data$RT >= 100)
  if (nrow(data_valid) / nrow(data) < 0.8) {
    warning("Proportion of valid trials is too low.")
    return(NA_real_)
  }
  data_valid %>%
    dplyr::summarise(score = 1000 - mean(.data$RT)) %>%
    dplyr::pull("score")
}
psychelzh/dataprocr documentation built on Oct. 12, 2019, 1:50 a.m.