1

我有多个数据文件,我有兴趣在其中进行清理,然后从中获取运行重复测量方差分析的方法。

这是示例数据,在实际数据中,有 4500 行,另一行称为 Actresponse,有时包含我修剪的 9:https ://docs.google.com/file/d/0B20HmmYd0lsFVGhTQ0EzRFFmYXc/edit?pli=1

我刚刚发现了 plyr 以及它在处理数据方面是多么的棒,但是我现在使用它的方式对我来说看起来相当愚蠢。我有 4 种不同的东西我感兴趣,我想读入一个数据框。我已经将它们读入 4 个单独的数据框开始,我想知道是否有一种方法可以将它结合起来并阅读所有内容用更少的代码行将手段放入一个数据帧(每个文件的每个reqresponse一行)。基本上,我可以在不重写很多相同代码4次的情况下实现我在这里所做的事情吗?

 PMScoreframe <- lapply(list.files(pattern='^[2-3].txt'),function(ff){
  data <-  read.table(ff, header=T, quote="\"")
  data <- data[-c(seq(from = 1, to = 4001, by=500), seq(from = 2, to = 4002, by=500)), ]
  ddply(data[data$Reqresponse==9,],.(Condition,Reqresponse),summarise,Score=mean(Score)) 
})

PMRTframe <- lapply(list.files(pattern='^[2-3].txt'),function(ff){
 data <-  read.table(ff, header=T, quote="\"")
 data <- data[data$RT>200,]
  data <-  ddply(data,.(Condition),function(x) x[!abs(scale(x$RT)) > 3,])
 ddply(data[data$Reqresponse==9,],.(Condition,Reqresponse,Score),summarise,RT=mean(RT))
})

OtherScoreframe <- lapply(list.files(pattern='^[2-3].txt'),function(ff){
  data <-  read.table(ff, header=T, quote="\"")
 data <- data[-c(seq(from = 1, to = 4001, by=500), seq(from = 2, to = 4002, by=500)), ]
  select <- rep(TRUE, nrow(data))
  index <- which(data$Reqresponse==9|data$Actresponse==9|data$controlrepeatedcue==1)
  select[unique(c(index,index+1,index+2))] <- FALSE
  data <- data[select,]
 ddply(data[data$Reqresponse=="a"|data$Reqresponse=="b",],.     (Condition,Reqresponse),summarise,Score=mean(Score)) 
})

 OtherRTframe <- lapply(list.files(pattern='^[2-3].txt'),function(ff){
  data <-  read.table(ff, header=T, quote="\"")
  data <- data[-c(seq(from = 1, to = 4001, by=500), seq(from = 2, to = 4002, by=500)), ]
  select <- rep(TRUE, nrow(data))
  index <- which(data$Reqresponse==9|data$Actresponse==9|data$controlrepeatedcue==1)
  select[unique(c(index,index+1,index+2))] <- FALSE
  data <- data[select,]
  data <- data[data$RT>200,]
  data <-  ddply(data,.(Condition),function(x) x[!abs(scale(x$RT)) > 3,])
  ddply(data[data$Reqresponse=="a"|data$Reqresponse=="b",],.(Condition,Reqresponse,Score),summarise,RT=mean(RT))
 })
4

1 回答 1

2

我认为这涉及您正在尝试做的事情。基本上,我认为您需要一次读取所有数据,然后处理data.frame。有几个问题涉及如何全部阅读,这是我将如何做到的,所以我会记录每行data.frame来自哪个文件,也可以用于分组:

filenames <- list.files(".", pattern="^[2-3].txt")
import <- mdply(filenames, read.table, header = T, quote = "\"")
import$file <- filenames[import$X1]

现在import是一个包含所有文件的大数据框(我假设您用于读取文件的模式识别等是正确的)。然后,您可以根据您喜欢的任何标准进行总结。

我不确定您在上面代码的第 3 行中要实现什么,但对于ddply下面的内容,您只需要执行以下操作:

ddply(import[import$Reqresponse==9,],.(Condition,Reqresponse,file),summarise,Score=mean(Score)) 

在你的其余代码中发生了太多事情,很难准确地判断出你想要什么。

我认为重要的是,要使这一过程高效且易于遵循,您需要一次性读取数据,然后处理该数据集——如有必要,制作子集,进行汇总统计或其他任何事情。

作为如何处理此问题的示例,这里尝试解决您处理具有reqresponse == 9和以下两个的试验(行?)的问题。可能有一些方法可以更有效地执行此操作,但这稍微基于您的操作方式,以简要向您展示如何使用更大的数据框。现在修改为删除每个文件的前两个试验:

  import.clean <- ddply(import, .(file), function(x) {
   index <- which(x$reqresponse == 9)
   if(length(index) > 0) {
     index <- unique(c(index, index + 1, index + 2, 1, 2))
   }
   else index <- c(1,2)
   x <- x[-index,]
   return(x)
})
于 2013-03-18T03:08:53.253 回答