私はRにはかなり新しく、助けが必要です。私は、001から332までの複数のCSVファイルを用意しています。それらをすべて1つのdata.frameにまとめたいと思います。これまで私が行ってきたことです:関数を使用して、複数のcsvファイルを含むdata.frameをRで作成するにはどうすればよいですか?
filesCSV <- function(id = 1:332){
fileNames <- paste(id) ## I want fileNames to be a vector with the names of all the csv files that I want to join together
for(i in id){
if(i < 10){
fileNames[i] <- paste("00",fileNames[i],".csv",sep = "")
}
if(i < 100 && i > 9){
fileNames[i] <- paste("0", fileNames[i],".csv", sep = "")
}
else if (i > 99){
fileNames[i] <- paste(fileNames[i], ".csv", sep = "")
}
}
theData <- read.csv(fileNames[1], header = TRUE) ## here I was trying to create the data.frame with the first csv file
for(a in 2:332){
theData <- rbind(theData,read.csv(fileNames[a])) ## here I wanted to use a for loop to cycle through the names of files in the fileNames vector, and open them all and add them to the 'theData' data.frame
}
theData
}
ありがとうございます、ありがとう!
の代わりに、forループを使用する必要がありますときのケースに対応するためだけに必要なアイデア、 pattern引数は、ファイル名の正規表現を定義すると便利でしょう。 –
@qqqこれは、実際の質問が行バインディングに関するものである間に、多くのファイルの列バインディングまたはマージを扱うので、良いデュープターゲットではありません。 – Uwe