我有一堆CSV文件,我想對每個文件中的數據執行相同的分析(在R中)。首先,我假定每個文件都必須讀入R中(而不是在CSV上運行函數並提供輸出,如sed腳本)。從R中的多個.csv文件讀取,處理和導出分析結果
將多個CSV文件輸入到R中以便執行分析然後爲每個輸入輸出單獨結果的最佳方法是什麼?
感謝(順便說一句,我爲完整的R新手)
我有一堆CSV文件,我想對每個文件中的數據執行相同的分析(在R中)。首先,我假定每個文件都必須讀入R中(而不是在CSV上運行函數並提供輸出,如sed腳本)。從R中的多個.csv文件讀取,處理和導出分析結果
將多個CSV文件輸入到R中以便執行分析然後爲每個輸入輸出單獨結果的最佳方法是什麼?
感謝(順便說一句,我爲完整的R新手)
您可以通過將所有CSV文件在同一目錄下試試下面的代碼。
names = list.files(pattern="*.csv") %csv file names
for(i in 1:length(names)){ assign(names[i],read.csv(names[i],skip=1, header=TRUE))}
希望這有助於!
您可以去肖恩的選擇,但它會導致幾個問題:
更好的做法可能與lapply()
。
# List files
filelist <- list.files(pattern = "*.csv")
# Now we use lapply to perform a set of operations
# on each entry in the list of filenames.
to_dispose_of <- lapply(filelist, function(x) {
# Read in the file specified by 'x' - an entry in filelist
data.df <- read.csv(x, skip = 1, header = TRUE)
# Store the filename, minus .csv. This will be important later.
filename <- substr(x = x, start = 1, stop = (nchar(x)-4))
# Your analysis work goes here. You only have to write it out once
# to perform it on each individual file.
...
# Eventually you'll end up with a data frame or a vector of analysis
# to write out. Great! Since you've kept the value of x around,
# you can do that trivially
write.table(x = data_to_output,
file = paste0(filename, "_analysis.csv"),
sep = ",")
})
並完成。
我會建議'for'循環:) – Jealie
我會建議一個函數,並且可能會從'for'循環中調用它;-) – janos