Größe: 5478
Kommentar:
|
Größe: 5679
Kommentar:
|
Gelöschter Text ist auf diese Art markiert. | Hinzugefügter Text ist auf diese Art markiert. |
Zeile 119: | Zeile 119: |
* the pattern is a regular expression which is more flexible than to use absolut strings | |
Zeile 120: | Zeile 121: |
* [[some basic information about strings and regular expression can be found here|/StringOperations]] | |
Zeile 125: | Zeile 127: |
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],"^0[012][0-9]_[1-8]$|^0[012][0-9]_test[12]$")))
10 print(paste("id",tmp$Subject[1]))
11
12 if(sum(tmp$Stim.Type %in% c("hit","incorrect"))==0) return(NULL)
13
14 tmp <- lapply(tmp,function(x) {
15 if( class(x) %in% c("character","factor") ){
16 x <- factor(gsub(" ","",as.character(x)))
17 return(x)}else{ return(x) }})
18
19 tmp <- as.data.frame(tmp)
20
21 pause <- which(tmp$Event.Type=="Picture" & tmp$Code=="Pause")
22 if(length(pause)>0){
23 drei <- which(tmp$Code==3 & !is.na(tmp$Code))
24 drei <- drei[drei > pause][1:2]
25 if(pause + 1 < drei[1]){
26 tmp <- tmp[-(pause:drei[2]),]
27 }}
28
29
30 tmp <- tmp[!(tmp$Event.Type %in% c("Pause","Resume")), ]
31
32 first.pic <- min(which(tmp$Event.Type=="Picture" & !is.na(tmp$Event.Type) )) - 1
33 tmp <- tmp[-(1:first.pic),]
34
35 last.pic <- min(which(tmp$Event.Type=="Picture" & !is.na(tmp$Event.Type) &
36 tmp$Code=="Fertig!" & !is.na(tmp$Code)))
37 tmp <- tmp[-(last.pic:nrow(tmp)),]
38
39 zeilen <- which(tmp$Event.Type %in% c("Response"))
40 zeilen <- sort(unique(c(zeilen,zeilen-1)))
41 zeilen <- zeilen[zeilen>0]
42 tmp <- tmp[zeilen,]
43
44 responses <- which(tmp$Code %in% c(1,2))
45 events <- responses-1
46 tmp$Type <- NA
47 tmp$Type[responses] <- as.character(tmp$Event.Type[events])
48
49 if(length(tmp$Type[responses])!=length(tmp$Event.Type[events])) { print(file)}
50 tmp$Event.Code <- NA
51 tmp$Event.Code[responses] <- as.character(tmp$Code[events])
52 tmp$Time1 <- NA
53 tmp$Time1[responses] <- tmp$Time[events]
54 tmp$Stim.Type[responses] <- as.character(tmp$Stim.Type[events])
55 tmp$Duration[responses] <- as.character(tmp$Duration[events])
56 tmp$Uncertainty.1[responses] <- as.character(tmp$Uncertainty.1[events])
57 tmp$ReqTime[responses] <- as.character(tmp$ReqTime[events])
58 tmp$ReqDur[responses] <- as.character(tmp$ReqDur[events])
59 tmp$Pair.Index[responses] <- as.character(tmp$Pair.Index[events])
60
61
62 tmp$Stim.Type[responses] <- as.character(tmp$Stim.Type[events])
63 tmp <- tmp[tmp$Event.Type=="Response" & !is.na(tmp$Type),]
64 tmp <- tmp[tmp$Type=="Picture" & !is.na(tmp$Type),]
65 return(tmp)
66 }
line 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
1 read.file <- function(file,skip=3,verbose=T){
line 2
1 if(verbose) print(paste("read", file))
this line just prints out the name of the file while reading it unless verbose is set to wrong
Line 3-5
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
Line 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
1 tmp <- tmp[!is.na(tmp$Subject),]
Line 8-9
Line 8 prints 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