2013-03-18 24 views
1

我有多個數據文件,其中我有興趣清理然後獲取手段來運行重複測量方差分析。用lapply(和plyr包)創建一個數據幀

這裏的示例數據,實時數據孤單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)) 
}) 
+0

因此,你在每次閱讀相同的數據? – alexwhan 2013-03-18 02:47:26

+0

是的,但做不同的東西到它 – luke123 2013-03-18 03:00:25

+0

你想用'data [-c(seq(from = 1,to = 4001,by = 500)''代碼的其他部分? – alexwhan 2013-03-18 03:14:07

回答

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) 
}) 
+0

感謝看起來不錯......實施它以確保它我想我可以用相同的順序夾緊1 - > n行導入來完成相同的操作嗎? – luke123 2013-03-18 03:41:32

+0

我正努力嘗試一下我如何挑選試驗我不想在分析導入時,例如,我想要在獲得正在進行的分數和RT時,在該試驗後(在文件中;在導入幀中的2個之後可能會闖入新文件中)採取行動響應9或2的任何試驗。我認爲我需要購買一些促智藥或什麼... – luke123 2013-03-18 03:54:22

+0

在一個答案中涵蓋各種問題是相當困難的 - 我認爲你最好嘗試提取單個問題,以便他們能夠得到明確的答案... – alexwhan 2013-03-18 04:03:49