我被卡住了。我需要一種方法來遍歷目錄中的一堆子文件夾,提取4個.csv文件,綁定這些4個.csv文件的內容,然後使用初始子文件夾的名稱將新的.csv寫入新目錄作爲新的.csv的名稱。如何使用R來遍歷子文件夾並綁定具有相同ID的CSV文件?
我知道 R可以做到這一點。但我堅持如何遍歷子文件夾並將csv文件綁定在一起。我的障礙是,每個子文件夾包含相同的4個.csv文件,使用相同的8位數的ID。例如,子文件夾A包含09061234.csv,09061345.csv,09061456.csv和09061560.csv。子文件夾B包含9061234.csv,09061345.csv,09061456.csv和09061560.csv。 (......)。有42個子文件夾,因此具有相同名稱的168個csv文件。我想將文件壓縮到42.
我可以使用list.files
檢索所有子文件夾。但那又如何?
##Get Files from directory
TF = "H:/working/TC/TMS/Counts/June09"
##List Sub folders
SF <- list.files(TF)
##List of File names inside folders
FN <- list.files(SF)
#Returns list of 168 filenames
###?????###
#How to iterate through each subfolder, read each 8-digit integer id file,
#bind them all together into one single csv,
#Then write to new directory using
#the name of the subfolder as the name of the new csv?
有可能是一個方法可以輕鬆地做到這一點,但我與R.一些涉及到的功能,paste
和write.table
也許是一個小白?任何提示/幫助/建議非常感謝。謝謝!
非常感謝@agstudy! – myClone 2013-03-04 17:37:56