#' duration_stage function
#' @param stage_string name of the stage
#' @param dataset original dataset containing all info about participants
#' @return returns duration of a chosen stage of the game for all participants
#' in form of a tibble with two columns: ID and duration.
#' @importFrom dplyr %>% count select group_by filter mutate row_number n lag
#' @export
#' @examples
#' duration_stage("Key-testing", dataset)
duration_stage <- function(stage_string, dataset){
if (nrow(dataset) == 0 || ncol(dataset) == 0){
stop("Columns or/and rows are empty")
}
dataset %>%
select(PIN, stage, timestamp) %>%
filter(stage == stage_string) %>%
group_by(PIN) %>%
filter(row_number()==1 | row_number()==n()) %>%
mutate(stage_duration = timestamp - lag(timestamp)) %>%
filter(!is.na(difference)) %>%
select(-c(stage, timestamp))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.