2014-03-13 25 views
1

您好我有我打開依次用for循環添加一行到數據幀有條件

myfiles <- list.files(pattern="*.dat") 
myfilesContent <- lapply(myfiles, read.table, header=T, quote="\"") 
for (i in 1:length(myfiles)){ 
... 
} 

我想要做的就是打開只有那些尊重一定的條件和合並的一個n個數據幀他們通過排,像

df <- data.frame 
for (i in 1:length(myfiles)){ 
    if(unique(myfilesContent[[i]]$V1) %in% test) df <-merge(df,myfilesContent[[i]]) 
} 

,但我得到這個錯誤

Error in as.data.frame.default(x) : 
    cannot coerce class '"function"' into a data.frame 

非常感謝

+1

錯誤是因爲'df < - data.frame'爲'data.frame(...)'*函數*賦值'df'。稍後,您將它引用爲好像是一個變量,因此是錯誤。如果你想創建一個空的數據框,使用'df <-data.frame()'。但是,從@agstudy下面的答案是一個更好的方法來做到這一點。 – jlhoward

回答

2

試試這個,例如:

do.call(rbind,lapply(myfiles, function(x){ 
      dt <- read.table(x, header=TRUE, quote="\"") 
      if (dt$V1 %in% test) dt 
    })) 

你讀,你只保留放入系統驗證的條件。然後你綁定結果。

相關問題