私は、より理解しやすいかもしれないわずかに異なるアプローチをとります:
temp = list.files(pattern="*.csv")
for (i in 1:length(temp))
{
tmp <- read.csv(temp[i], header=FALSE, skip =20)
colnames(tmp) <- c("Date","Unit","Temp")
# Now what do you want to do?
# For instance, use the file name as the name of a list element containing the data?
}
アップデート:
temp = list.files(pattern="*.csv")
stations <- vector("list", length(temp))
for (i in 1:length(temp)) {
tmp <- read.csv(temp[i], header=FALSE, skip =20)
colnames(tmp) <- c("Date","Unit","Temp")
stations[[i]] <- tmp
}
names(stations) <- temp # optional; could process file names too like using basename
station1 <- station[[1]] # etc station1 would be a data.frame
この 2 番目の部分も、データの使用方法とデータの量によっては改善される可能性があります。知っておくと便利なコマンドは str(some object) です。R のデータ構造を理解するのに非常に役立ちます。
更新 #2:
個々のデータ フレームをワークスペースに取り込むのは非常に困難です。これらをプロットしたいので、最初にあなたが望むように名前を付けます:
names(stations) <- paste(basename(temp), 1:length(stations), sep = "_")
次に、上で作成したリストを次のように繰り返し処理し、プロットを作成します。
for (i in 1:length(stations)) {
tmp <- stations[[i]]
# tmp is a data frame with columns Date, Unit, Temp
# plot your data using the plot commands you like to use, for example
p <- qplot(x = Date, y = Temp, data = tmp, geom = "smooth", main = names(stations)[i])
print(p)
# this is approx code, you'll have to play with it, and watch out for Dates
# I recommend the package lubridate if you have any troubles parsing the dates
# qplot is in package ggplot2
}
それらをファイルに保存する場合は、これを使用します。
pdf("filename.pdf")
# then the plotting loop just above
dev.off()
複数ページの PDF が作成されます。幸運を!