################### BBC-extractor by Phil Enock 8/21/2012 ################### For getting data from BBC files and turning it into Excel files ################### This will output 1 file per person, so you can use it with your existing methods of handling the RTs. ################### It will also output 1 special file containing ALL trials, which is great for if you handle trial data in SPSS. # THE OPTIONS YOU NEED TO SET --------------------------------------------- COLUMN.NAMES <- c("items", "assess_1", "assess_2", "assess_3", "assess_4", "assess_5", "assess_6", "assess_7") # No need to edit anything below ------------------------------------------ # Set working directory to source script location library(rstudioapi) PATH.USER.DATA <- dirname(rstudioapi::getActiveDocumentContext()$path) # Phil's functions ------------------------------------------------------- N.LINES.PER.TRIAL <- length(COLUMN.NAMES) strs.pos <- function (haystack, needle) { if ( (is.null(haystack) | is.na(haystack) | length(haystack) != 1) | (is.null(needle) | is.na(needle) | length(needle) != 1)) stop("Your haystack or needle arg is not legit -Phil") if (nchar(haystack) < nchar(needle)) return (FALSE) if (needle == "") return (ifelse(haystack == "", 1, FALSE)) for (iStart in 1:(nchar(haystack)-nchar(needle) + 1)) { if (substr(haystack, iStart, iStart+nchar(needle)-1) == needle) return (iStart) } FALSE } strs.pos.vec <- function (vec, needle) { if (length(vec) < 1) stop("Your vec arg is not legit -Phil") posVec <- vector() for (i in 1:length(vec)) posVec[i] <- strs.pos(vec[i], needle) posVec } strs.detect <- function (...) strs.pos(...) != FALSE strs.detect.vec <- function (vec, needle) strs.pos.vec(vec, needle) != 0 strs.replace <- function(haystack, needle.old, needle.new) { needle.old.start <- strs.pos(haystack, needle.old) needle.old.end <- needle.old.start + nchar(needle.old)-1 head <- substr(haystack, 1, needle.old.start-1) if (needle.old.end >= nchar(haystack)) tail <- "" else tail <- substr(haystack, needle.old.end+1, nchar(haystack)) paste(head, needle.new, tail, sep="") } #### Code part 1 - first run up through this section to see what BBC files we've detected ---- setwd(PATH.USER.DATA) fileList <- list.files(recursive=TRUE) fileList <- fileList[strs.detect.vec(fileList, ".BBC")] fileList # Shows what BBC files we've detected #### Code part 2 - makes the individual files in the same folder(s) the files were originally ---- bigtrloe <- data.frame() for(i in 1:length(fileList)) { iFileName <- fileList[i] cat("File #", i, ": ", iFileName, "\n", sep="") fileoe <- read.delim(iFileName, sep="\n", header=F) fileVec <- as.character(fileoe[[1]]) personoe <- data.frame(matrix(nrow=length(fileVec) / N.LINES.PER.TRIAL, ncol=N.LINES.PER.TRIAL)) for (iRow in 1:(length(fileVec) / N.LINES.PER.TRIAL)) { for (iCol in 1:N.LINES.PER.TRIAL) { personoe[[iRow, iCol]] <- fileVec[(iRow-1)*N.LINES.PER.TRIAL + iCol] } } names(personoe) <- COLUMN.NAMES for (iCol in 1:N.LINES.PER.TRIAL) personoe[[iCol]] <- type.convert(personoe[[iCol]]) fileNameMainPart <- strs.replace(iFileName, ".BBC", "") personoe <- cbind(data.frame(subjectid=fileNameMainPart), personoe) write.csv(personoe, file=paste(fileNameMainPart, ".csv", sep=""), row.names=FALSE) bigtrloe <- rbind(bigtrloe, personoe) } #### Code part 3 - writes the one big CSV file, what you want if you are handling trial data in SPSS, R, or MatLab ---- write.csv(bigtrloe, file="ALL trials in one.csv", row.names=FALSE) # Warnings printed here: warnings() # # -------------- # |End Of Script| # -------------- # ?\_(:D)_/? # \|/ # _/\_