RstatisTik/RstatisTikPortal/RcourSe/FinalFunction

Final Functions

Funtion: Reading File

Here is the function as whole, below we go through it line by line.

   1 read.file <- function(file,skip,verbose=T){
   2     if(verbose) print(paste("read", file))
   3     tmp <- read.table(file,skip = skip,sep = "\t",
   4                       header=T,na.strings = c(" +",""),
   5                       fill=T)
   6     
   7     tmp <- tmp[!is.na(tmp$Subject),] 
   8     
   9     if(sum(str_detect(tmp[,1],"CH|GA|IJ|Kj|RMK"))) print(paste("id",tmp[3,1]))
  10 
  11     if(sum(tmp$Stim.Type %in% c("hit","incorrect"))==0) return(NULL)
  12 
  13     tmp <- lapply(tmp,function(x) {
  14         if( class(x) %in% c("character","factor") ){
  15             x <- factor(gsub(" ","",as.character(x)))
  16             return(x)}else{ return(x) }})
  17     
  18     tmp <- as.data.frame(tmp)
  19     
  20     pause <- which(tmp$Event.Type=="Picture" & tmp$Code=="Pause")
  21     if(length(pause)>0){
  22         drei <- which(tmp$Code==3 & !is.na(tmp$Code))
  23         drei <- drei[drei > pause][1:2]
  24         if(pause + 1 < drei[1]){
  25             tmp <- tmp[-(pause:drei[2]),]
  26         }}
  27 
  28     
  29     tmp <- tmp[!(tmp$Event.Type %in% c("Pause","Resume")), ]
  30 
  31     first.pic <- min(which(tmp$Event.Type=="Picture" & !is.na(tmp$Event.Type) )) - 1
  32     tmp <- tmp[-(1:first.pic),]
  33 
  34     last.pic <- min(which(tmp$Event.Type=="Picture" & !is.na(tmp$Event.Type) &
  35                               tmp$Code=="Fertig!" & !is.na(tmp$Code)))
  36     tmp <- tmp[-(last.pic:nrow(tmp)),]
  37 
  38     zeilen <- which(tmp$Event.Type %in% c("Response"))
  39     zeilen <- sort(unique(c(zeilen,zeilen-1)))
  40     zeilen <- zeilen[zeilen>0]
  41     tmp <- tmp[zeilen,]
  42     
  43     responses <- which(tmp$Code %in% c(1,2))
  44     events <- responses-1
  45     tmp$Type <- NA
  46     tmp$Type[responses] <- as.character(tmp$Event.Type[events])
  47 
  48     if(length(tmp$Type[responses])!=length(tmp$Event.Type[events])) { print(file)}
  49     tmp$Event.Code <- NA
  50     tmp$Event.Code[responses] <- as.character(tmp$Code[events])
  51     tmp$Time1 <- NA
  52     tmp$Time1[responses] <- tmp$Time[events]
  53     tmp$Stim.Type[responses] <- as.character(tmp$Stim.Type[events])
  54     tmp$Duration[responses] <- as.character(tmp$Duration[events])
  55     tmp$Uncertainty.1[responses] <- as.character(tmp$Uncertainty.1[events])
  56     tmp$ReqTime[responses] <- as.character(tmp$ReqTime[events])
  57     tmp$ReqDur[responses] <- as.character(tmp$ReqDur[events])
  58     tmp$Pair.Index[responses] <- as.character(tmp$Pair.Index[events])
  59     
  60 
  61     tmp$Stim.Type[responses] <- as.character(tmp$Stim.Type[events])
  62     tmp <- tmp[tmp$Event.Type=="Response" & !is.na(tmp$Type),]
  63     tmp <- tmp[tmp$Type=="Picture" & !is.na(tmp$Type),]
  64     return(tmp)
  65 }

line 1

   1  read.file <- function(file,skip=3,verbose=T){

line 2

   1  if(verbose) print(paste("read", file))

Line 3-5

   1  tmp <- read.table(file,skip = skip,sep = "\t",
   2                           header=T,na.strings = c(" +",""),
   3                           fill=T)

Line 7

   1     tmp <- tmp[!is.na(tmp$Subject),] 

Line 18:

Line 22:

Funtion: Reading All Files from a DIRECTORY

   1 read.files <- function(filesdir,skip=3,...){
   2     files <- paste(filedir,dir(filedir),sep="/")
   3     Reduce(rbind,lapply(files,read.file,skip=skip))}

RstatisTik/RstatisTikPortal/RcourSe/FinalFunction (zuletzt geändert am 2015-03-15 09:40:54 durch mandy.vogel@googlemail.com)