R/squared.R

Defines functions raw_squared

Documented in raw_squared

#' Raw Tidy Data for Three-Minute Squared Tasks
#'
#' Converts a messy e-prime data file into a tidy raw data file that is
#' easy to work with.
#'
#' @param x dataframe
#' @param include_col c(): list of additional columns to include
#' @export
#'

raw_squared <- function(x, include_col = c()) {

  x <- dplyr::rename(x, TrialProc = `Procedure[Trial]`)
  if (!("FixationDuration" %in% colnames(x))) {
    x <- dplyr::rename(x, FixationDuration = t4)
  }
  proc_names <- unique(x$TrialProc)
  if ("pracproc" %in% proc_names) {
    x <- dplyr::filter(x, TrialProc == "TrialProc" | TrialProc == "pracproc")
    x <- dplyr::mutate(x,
                       TrialProc =
                         dplyr::case_when(TrialProc == "TrialProc" ~ "real",
                                          TrialProc == "pracproc" ~ "practice"),
                       Trial =
                         dplyr::case_when(TrialProc == "real" ~
                                            TrialList.Sample,
                                          TrialProc == "practice" ~
                                            practice.Sample))
  } else {
    x <- dplyr::select(x, -TrialProc)
    x <- dplyr::rename(x, TrialProc = `Running[Trial]`)
    x <- dplyr::filter(x,
                       TrialProc == "TrialList" | TrialProc == "PracticeList2")
    x <- dplyr::mutate(x,
                       TrialProc =
                         dplyr::case_when(TrialProc == "TrialList" ~
                                            "real",
                                          TrialProc == "PracticeList2" ~
                                            "practice"),
                       Trial =
                         dplyr::case_when(TrialProc == "real" ~
                                            TrialList.Sample,
                                          TrialProc == "practice" ~
                                            PracticeList2.Sample))
  }

  x <- dplyr::mutate(x,
                     Target =
                       dplyr::case_when(!is.na(right_targ) ~ right_targ,
                                        !is.na(left_targ) ~ left_targ),
                     TargetLocation = case_when(!is.na(right_cue) ~ "Left",
                                                !is.na(left_cue) ~ "Right"))

  if ("AdminTime" %in% colnames(x)) {
    x <- dplyr::group_by(x, Subject)
    x <- dplyr::mutate(x, AdminTime = dplyr::last(AdminTime) / 60000)
    x <- dplyr::ungroup(x)
    x <- dplyr::select(x, Subject, TrialProc, Trial, Accuracy = Mask.ACC,
                       RT = Mask.RT, Target, FixationDuration,
                       dplyr::all_of(include_col), AdminTime, SessionDate, SessionTime)
  } else {
    x <- dplyr::select(x, Subject, TrialProc, Trial, Accuracy = Mask.ACC,
                       RT = Mask.RT, Target, FixationDuration,
                       dplyr::all_of(include_col), SessionDate, SessionTime)
  }

  return(x)
}
EngleLab/englelab documentation built on Sept. 9, 2024, 4:12 a.m.