我有多個數據文件,其中我有興趣清理然後獲取手段來運行重複測量方差分析。用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))
})
因此,你在每次閱讀相同的數據? – alexwhan 2013-03-18 02:47:26
是的,但做不同的東西到它 – luke123 2013-03-18 03:00:25
你想用'data [-c(seq(from = 1,to = 4001,by = 500)''代碼的其他部分? – alexwhan 2013-03-18 03:14:07