2010-05-24 60 views
2

我似乎無法得到以下工作[R問題適用+ rbind

directory <- "./" 
files.15x16 <- c("15x16-70d.out", "15x16-71d.out") 
data.15x16<-rbind(lapply(as.array(paste(directory, files.15x16, sep="")), FUN=read.csv, sep=" ", header=F)) 

它所應該做的是非常簡單的 - 我有一個目錄名,一些文件名和實際文件數據。我將目錄和文件名粘貼在一起,從文件中讀取數據,然後將它們全部組合到一個數據塊中。

除lapply的結果具有數據在[[]] - 即,訪問它經由a[[1]], a[[2]],等似乎這rbind不接受發生。

對此提出建議?

+0

看看http://stackoverflow.com/questions/2104483/how-to-read-table-multiple-files-into-a-single-table-in-r – Marek 2010-05-25 19:20:50

+0

還有一些「不完全一致」相同「:http://stackoverflow.com/questions/2209258/merge-several-data-frames-into-one-data-frame-with-a-loop,http://stackoverflow.com/questions/1562124/merge-many-data-frames-from-csv-files – Marek 2010-05-25 19:24:00

回答

13

使用do.call

data.15x16 <- do.call(rbind, lapply(paste(directory, files.15x16, sep=""), 
             FUN=read.csv, sep=" ", header=F)) 

你也不需要as.array - 它並沒有真正做任何事情在這裏。

+0

輝煌,謝謝; as.array是使用apply而不是lapply的遺留物 - apply申訴暗淡(X)不積極 – Carl 2010-05-24 13:28:54