Reply To: reaction time results appear in different two columns

PennController for IBEX Forums Support reaction time results appear in different two columns Reply To: reaction time results appear in different two columns

#6290
Jeremy
Keymaster

Hi again,

I took a look at your results file, and it turns out I was completely wrong about the spreadsheet editor issue. Given your results file, the code I gave you indeed fails to detect columns after the 13th one, because the first CSV line (ie. non-comment line) it encounters in the file has 13 columns.

Fortunately there actually is a ready-made function from the tutorial that solves the problem: read.pcibex. So I inserted it to an adapted version of the code above, and it’s working now:

read.pcibex <- function(filepath, auto.colnames=TRUE, fun.col=function(col,cols){cols[cols==col]<-paste(col,"Ibex",sep=".");return(cols)}) {
  n.cols <- max(count.fields(filepath,sep=",",quote=NULL),na.rm=TRUE)
  if (auto.colnames){
    cols <- c()
    con <- file(filepath, "r")
    while ( TRUE ) {
      line <- readLines(con, n = 1, warn=FALSE)
      if ( length(line) == 0) {
        break
      }
      m <- regmatches(line,regexec("^# (\\d+)\\. (.+)\\.$",line))[[1]]
      if (length(m) == 3) {
        index <- as.numeric(m[2])
        value <- m[3]
        if (index < length(cols)){
          cols <- c()
        }
        if (is.function(fun.col)){
          cols <- fun.col(value,cols)
        }
        cols[index] <- value
        if (index == n.cols){
          break
        }
      }
    }
    close(con)
    return(read.csv(filepath, comment.char="#", header=FALSE, col.names=cols))
  }
  else{
    return(read.csv(filepath, comment.char="#", header=FALSE, col.names=seq(1:n.cols)))
  }
}

# First read the results file
all_results <- read.pcibex("resultsf")
# We'll work on the DashedSentence lines only
dashed_results <- all_results
# Keep only the DashedSentence lines
dashed_results <- subset(dashed_results, PennElementType=="Controller-DashedSentence")
# Let us first make the columns character-based instead of factors
cols.num <- seq(13,21)
dashed_results[cols.num] <- sapply(dashed_results[cols.num],as.character)
# Now we'll move the columns for the short lines (the ones without extra1 and extra2)
short_lines <- dashed_results$Comments==""
# Now let's move the columns
wrong.cols <- seq(13,16)
empty.cols <- seq(18,21)
dashed_results[short_lines,empty.cols] <- dashed_results[short_lines,wrong.cols]
dashed_results[short_lines,wrong.cols] <- NA
# Done!

Sorry for overlooking this problem, I shouldn’t have insisted it came from an editing process when you were clearly just following my advice

Let me know if you have any questions

Jeremy