1

次のようにフォーマットされた複数のデータファイル(タブ区切りのtxtファイル)があります。

いくつかのサンプルファイルを作成しました

https://docs.google.com/file/d/0B20HmmYd0lsFVGhTQ0EzRFFmYXc/edit?usp=sharing

https://docs.google.com/file/d/0B20HmmYd0lsFbWxmQzV6X0o2Y1E/edit?usp=sharing

Condition  Block Session Stimuli    Score   Reqrespons Act RT extra
 X          3      3    asdfa        1           a      a  500  0
 Y          1      2    qewrq        0           b      a  1100 0

範囲外のRTを除外し、RTの平均値とファイルのスコアでANOVAを実行したい(因子条件付き)。これまでのところ、私はこれを非常に醜い方法で行い、件名ごとに行を作成しました (行を件名 x 条件としてフォーマットすることをお勧めします)。

私の現在の試みは for ループを使用しています:

all_data<-data.frame(rbind(1:27)) #make empty data.frame 
all_data
for(i in 1:2)
{
n= paste(i,".txt", sep="")
a<- sprintf("table%d", i, i)
data <- read.table(toString(n), header = TRUE, sep = "\t")

列 1:9 にスコア 1 ~ 9 を記入します

Score<-as.vector(tapply(data$Score,list(data$Condition,data$Reqresponse),mean))

for(o in 1:9)
{
all_data [i, o] <- Score[o]
}

次に、希望する方法で RT 値をトリミングし、all_data の列 10 に入れます

data <- data[which(data$RT>200),]
data <- do.call(rbind,by(data,data$Condition,function(x) x[!abs(scale(x$RT)) > 3,] ))
RT<-as.vector(tapply(data$RT,list(data$Condition,data$Reqresponse, data$Score),mean))
for(j in 1:18)
{
all_data [i, j+9] <- RT[j]
}
}

また、このコードは、R のまともな人にとって美的に不快でなければなりません。よろしければ、それを修正する方法を教えてください。

4

1 に答える 1

1

パッケージddplyから使用してこれを行います。plyr例えば:

require(plyr)
res <- lapply(list.files(pattern='^[1-2].txt'),function(ff){
  ## you read the file 
  data <-  read.table(ff, header=T, quote="\"")
  ## remove the outlier
  data <- data[data$RT>200,]
  data <-  ddply(data,.(Condition),function(x) x[!abs(scale(x$RT)) > 3,])
  ## compute the mean
  ddply(data,.(Condition,Reqresponse,Score),summarise,RT=mean(RT))
})

[[1]]
   Condition Reqresponse Score   RT
1          X           a     0  500
2          X           a     1  750
3          X           b     0  500
4          X           b     1  500
5          Y           a     0  400
6          Y           a     1  640
7          Y           b     1 1000
8          Z           a     0 1000
9          Z           a     1 1675
10         Z           b     0  400

[[2]]
   Condition Reqresponse Score   RT
1          X           a     0  500
2          X           a     1  750
3          X           b     0  500
4          X           b     1  500
5          Y           a     0  400
6          Y           a     1  640
7          Y           b     1 1000
8          Z           a     0 1000
9          Z           a     1 1675
10         Z           b     0  400
于 2013-03-15T08:51:29.100 に答える