R/readiChart.R

#' Read iChart
#'
#' This function reads a wide format data file generated by RTobii as an iChart
#'
#' @description \code{readiChart()} is a special case of \code{read.table()}. It's useful for doing minor data
#'    processing on the raw .text file that is specific to the Language Learning Lab's analysis workflow.
#' @param iChartFile Either a path to a file, a connection, or literal data.
#' @param sampling_rate An integer indicating the sampling rate of the eyetracker (17 vs. 30).
#' @export
#' @examples
#' \dontrun{d <- readiChart(iChartFile = "Habla2_25_iChart_wide.txt", sampling_rate = 17)}

readiChart <- function(iChartFile, sampling_rate = 17) {
  # set up ms to frames
  scale_factor <- ifelse(sampling_rate == 17, 6, 3)
  msToFrames <- function(value) { return(value * scale_factor/100) }
  framesToMs <- function(value) { return(value * 100/scale_factor) }

  # build meta data
  StudyName <- substr(basename(iChartFile), 1, nchar(iChartFile)-4)
  Directory <- paste(dirname(iChartFile),"/" , sep="")
  iChart <- read.table(iChartFile, header=T, fill=T, as.is=T, sep="\t")
  iChart$RTOld <- iChart$RT
  startWindow <- match("F0", colnames(iChart))
  if(is.na(startWindow)) startWindow <- match("Word.Onset.Frame", colnames(iChart))

  # rename frame columns
  colnames(iChart)[startWindow:length(iChart)] <- round(framesToMs(round(0:(length(iChart)-startWindow))))

  # the original file had one header per subject
  # this line removes all rows that are the same as the header
  # and selects only the columns of interest
  iChart <- iChart[iChart[,"Months"]!="Months",]

  iChart$Block <- rep(NA, nrow(iChart))
  iChart$Offset <- rep(0, nrow(iChart))
  iChart$StartWindowRT <- rep(NA, nrow(iChart))
  iChart$EndWindowRT <- rep(NA, nrow(iChart))
  iChart$StartWindowAcc <- rep(NA, nrow(iChart))
  iChart$EndWindowAcc <- rep(NA, nrow(iChart))
  iChart$StudyName <- rep(StudyName, nrow(iChart))
  iChart$Directory <- rep(Directory, nrow(iChart))

  iChart <- iChart[iChart$Condition != "",]
  iChart
}
kemacdonald/iChartAnalyzeR documentation built on May 7, 2019, 8:44 a.m.