1

温度と時間のデータを持つ一連の CSV ファイルを開くために、次のコードを実行しています。

temp = list.files(pattern="*.csv")
for (i in 1:length(temp)) 
{
  assign(temp[i], read.csv(temp[i], header=FALSE, skip =20))
  colnames(as.data.frame(temp[i])) <- c("Date","Unit","Temp")
}

データ フレーム内のデータは次のようになります。

                   V1 V2   V3
1 6/30/13 10:00:01 AM  C 32.5
2 6/30/13 10:20:01 AM  C 32.5
3 6/30/13 10:40:01 AM  C 33.5
4 6/30/13 11:00:01 AM  C 34.5
5 6/30/13 11:20:01 AM  C 37.0
6 6/30/13 11:40:01 AM  C 35.5

列名を割り当てようとしていますが、次のエラー メッセージが表示されます。

Error in `colnames<-`(`*tmp*`, value = c("Date", "Unit", "Temp")) : 
  'names' attribute [3] must be the same length as the vector [1]

私のループがcsvファイルをどのように読んでいるかに何か関係があるのではないかと思います。これらはすべて R の同じディレクトリに保存されます。

ご協力いただきありがとうございます!

4

5 に答える 5

1

私は、より理解しやすいかもしれないわずかに異なるアプローチをとります:

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 が作成されます。幸運を!

于 2013-07-09T21:16:19.930 に答える
1

It is usually not recommended practice to use the 'assign' statement in R. (I should really find some resources on why this is so.)

You can do what you are trying using a function like this:

read.a.file <- function (f, cnames, ...) {
  my.df <- read.csv(f, ...)
  colnames(my.df) <- cnames
  ## Here you can add more preprocessing of your files.
}

And loop over the list of files using this:

lapply(X=temp, FUN=read.a.file, cnames=c("Date", "Unit", "Temp"), skip=20, header=FALSE)
于 2013-07-09T21:16:57.497 に答える
1
  1. 「read.csv」は data.frame を返すため、「as.data.frame」呼び出しは必要ありません。
  2. 「col.names」引数を「read.csv」に使用して、列名を割り当てることができます。
  3. 使用しているRのバージョンはわかりませんが、「colnames(as.data.frame(...)) <-」は「as.data.frame<-」関数を呼び出すため、間違った呼び出しです少なくともバージョン 2.14 では存在しません。
于 2013-07-09T21:19:56.927 に答える
0

At a glance it appears that you are missing a set of subset braces, [], around the elements of your temp list. Your attribute list has three elements but because you have temp[i] instead of temp[[i]] the for loop isn't actually accessing the elements of the list thus treating as an element of length one, as the error says.

于 2013-07-09T21:16:31.900 に答える