= Final Functions = == Funtion: Reading File == Here is the function as whole, below we go through it line by line. {{{#!highlight r read.file <- function(file,skip,verbose=T){ if(verbose) print(paste("read", file)) tmp <- read.table(file,skip = skip,sep = "\t", header=T,na.strings = c(" +",""), fill=T) tmp <- tmp[!is.na(tmp$Subject),] if(sum(!str_detect(tmp[,1],"^0[012][0-9]_[1-8]$|^0[012][0-9]_test[12]$"))) print(paste("id",tmp$Subject[1])) if(sum(tmp$Stim.Type %in% c("hit","incorrect"))==0) return(NULL) tmp <- lapply(tmp,function(x) { if( class(x) %in% c("character","factor") ){ x <- factor(gsub(" ","",as.character(x))) return(x)}else{ return(x) }}) tmp <- as.data.frame(tmp) pause <- which(tmp$Event.Type=="Picture" & tmp$Code=="Pause") if(length(pause)>0){ drei <- which(tmp$Code==3 & !is.na(tmp$Code)) drei <- drei[drei > pause][1:2] if(pause + 1 < drei[1]){ tmp <- tmp[-(pause:drei[2]),] }} tmp <- tmp[!(tmp$Event.Type %in% c("Pause","Resume")), ] first.pic <- min(which(tmp$Event.Type=="Picture" & !is.na(tmp$Event.Type) )) - 1 tmp <- tmp[-(1:first.pic),] last.pic <- min(which(tmp$Event.Type=="Picture" & !is.na(tmp$Event.Type) & tmp$Code=="Fertig!" & !is.na(tmp$Code))) tmp <- tmp[-(last.pic:nrow(tmp)),] zeilen <- which(tmp$Event.Type %in% c("Response")) zeilen <- sort(unique(c(zeilen,zeilen-1))) zeilen <- zeilen[zeilen>0] tmp <- tmp[zeilen,] responses <- which(tmp$Code %in% c(1,2)) events <- responses-1 tmp$Type <- NA tmp$Type[responses] <- as.character(tmp$Event.Type[events]) if(length(tmp$Type[responses])!=length(tmp$Event.Type[events])) { print(file)} tmp$Event.Code <- NA tmp$Event.Code[responses] <- as.character(tmp$Code[events]) tmp$Time1 <- NA tmp$Time1[responses] <- tmp$Time[events] tmp$Stim.Type[responses] <- as.character(tmp$Stim.Type[events]) tmp$Duration[responses] <- as.character(tmp$Duration[events]) tmp$Uncertainty.1[responses] <- as.character(tmp$Uncertainty.1[events]) tmp$ReqTime[responses] <- as.character(tmp$ReqTime[events]) tmp$ReqDur[responses] <- as.character(tmp$ReqDur[events]) tmp$Pair.Index[responses] <- as.character(tmp$Pair.Index[events]) tmp$Stim.Type[responses] <- as.character(tmp$Stim.Type[events]) tmp <- tmp[tmp$Event.Type=="Response" & !is.na(tmp$Type),] tmp <- tmp[tmp$Type=="Picture" & !is.na(tmp$Type),] return(tmp) } }}} === line 1 === * [[#CA-a8692f97d06bd15e4a572260cf8b72ef3c9d984e_1|line 1]] gives the function its name including arguments and their default values * the file argument will take the file name and is without a default * skip takes a number which indicates how many lines will be skipped at the beginning of the file * verbose indicates if the file name will be printed out while reading {{{#!highlight r read.file <- function(file,skip=3,verbose=T){ }}} === line 2 === {{{#!highlight r if(verbose) print(paste("read", file)) }}} * [[#CA-a8692f97d06bd15e4a572260cf8b72ef3c9d984e_2|this line]] just prints out the name of the file while reading it unless verbose is set to wrong === Line 3-5 === * [[#CA-a8692f97d06bd15e4a572260cf8b72ef3c9d984e_3|here]] we have the command to read in the text file * it takes the skip argument from above * we are setting sep which indicates the field separator to tab * set header to T because the file contains the columns names * with setting na.strings to the empty string or any string containing only spaces to we indicate to code this fields as missings * [[/ReadingFiles|more on reading files]] {{{#!highlight r tmp <- read.table(file,skip = skip,sep = "\t", header=T,na.strings = c(" +",""), fill=T) }}} === Line 7 === * [[#CA-a8692f97d06bd15e4a572260cf8b72ef3c9d984e_7|here]] we remove all rows with a missing Subject field * therefore we need indexing * is.na(x) gives back a logical vector, containing TRUE for missings in x and FALSE for any existing value * [[/indeXing|read more on indexing/subscripting]] {{{#!highlight r tmp <- tmp[!is.na(tmp$Subject),] }}} === Line 9-10 === * [[#CA-a8692f97d06bd15e4a572260cf8b72ef3c9d984e_9|Line 9 and 10]] print the content of Subject to stdout if the content is not in standard form * str_detect() is a R function and part of the stringr package it gives back a logical value dependend on if the pattern is contained in the given string * the pattern is a regular expression which is more flexible than to use absolut strings * so we check every entry of Subject, take the negation and sum the resulting logical vector - this sum is zero if no deviant Subject coding is found, otherwise the print command is executed * [[/StringOperations|some basic information about strings and regular expression can be found here]] {{{#!highlight r if(sum(!str_detect(tmp$Subject,"^0[012][0-9]_[1-8]$|^0[012][0-9]_test[12]$"))) print(paste("id",tmp$Subject[1])) }}} [[#CA-a8692f97d06bd15e4a572260cf8b72ef3c9d984e_18|Line 18]]: [[#CA-efbf8a93fbd22f17bfbdcb535a155bbb9c389093_22|Line 22]]: == Funtion: Reading All Files from a DIRECTORY == {{{#!highlight r read.files <- function(filesdir,skip=3,...){ files <- paste(filedir,dir(filedir),sep="/") Reduce(rbind,lapply(files,read.file,skip=skip))} }}}