R/duration.R

Defines functions general_info_all_participants

Documented in general_info_all_participants

#' general_info_all_participants function
#' @param dataset original dataset containing all info about participants
#' @return dataframe with five columns:
#'         PIN, version, location, duration of the game and
#'         number of actions (key presses and mouse clicks performed)
#' @importFrom dplyr %>% count select group_by filter mutate row_number n lag
#' @export
#' @examples
#' general_info_all_participants(dataset)
general_info_all_participants <- function(dataset){
  if (nrow(dataset) == 0 || ncol(dataset) == 0){
    stop("Columns or/and rows are empty")
  }
  tab1 <- dataset %>%
    select(PIN, version, timestamp, location) %>%
    group_by(PIN) %>%
    filter(row_number()==1 | row_number()==n()) %>%
    mutate(time_spent = timestamp - lag(timestamp)) %>%
    filter(!is.na(time_spent)) %>%
    select(-timestamp)
  tab2 <- dataset %>%
    select(PIN, event.type) %>%
    filter(event.type == 'key press') %>%
    group_by(PIN) %>%
    count()
  fin <- data.frame(merge(tab1, tab2, by='PIN'))
  names(fin) <- c("PIN", "version", "location","total_time", "num_acts")
  return(fin)
}
Art83/Deval documentation built on Nov. 24, 2019, 12:22 p.m.