0

同じ次元の 5 つのバイナリ ファイル (ラスター) があります。最初の 4 つのファイルはパラメーター 1 を表し、5 番目のファイルは 10 クラスの土地被覆マップを表します。土地被覆クラスに基づいて 4 つのファイルすべての平均を計算したいと考えています。最終的に、各クラスに対応する 4 つの値を取得します。

何かのようなもの:

    1(first class):
  first file = 0.5(average of all pixels correspond to class 1 from the land cover)
  second file = 0.4(average of all pixels correspond to class 1 from the land cover)
  third file = 0.2(average of all pixels correspond to class 1 from the land cover)
  fourth file = 0.1(average of all pixels correspond to class 1 from the land cover)

  2(second class):
first file = 0.5(average of all pixels correspond to class 2 from the land cover)
second file = 0.4(average of all pixels correspond to class 2 from the land cover)
third file = 0.2(average of all pixels correspond to class 2 from the land cover)
fourth file = 0.1(average of all pixels correspond to class 2 from the land cover

等々...

stackoverflow で非常によく似たものを見つけました: 別のバイナリ ファイルのクラスに基づいて、あるバイナリ ファイルの変数の平均を計算する方法は?

ただし、これは、1 つのファイルではなく 4 つのファイルがあるという点で異なります。そのため、そのコードをすべてのファイルでループする必要があります。

すべてのファイル:

1- 1 つのファイルを読み取る場合:

  fre <- file("C:\\corr.bin","rb")
  sdf<- readBin(fre, numeric(), size=4,  n=1440*720, signed=TRUE)

2- 土地被覆ファイルを読み取るには:

        land <- file("C:\\land cover.bin","rb")
        over<- readBin(land, integer(), size=1,  n=1440*720, signed=F)

3- 1 つのファイルのみを使用して平均を計算するには:

    result=tapply(sdf, over, mean, na.rm=TRUE)

すべてのファイルに対してこれを試しました:

  dir1<- list.files("C:\filesh", "*.img", full.names = TRUE)
  fre <- file("C:\\landover_from Suj1440a.bin","rb")
  sdf<- readBin(fre, integer(), size=1,  n=1440*720, signed=F)
  results<- list()
 for (.files in seq_along(dir1)){
   list1 <- readBin(dir1[.files], numeric(), size = 4, n = 1440*720, signed = TRUE)   
   list1=tapply(list1, sdf, mean, na.rm=TRUE)
   results[[length(results) + 1L]]<- list1}

エラーなしで動作したようです.: 結果を書きます (kith answer から):

   for (i in seq_along(results)){
  write.table(results[[i]], paste("C:\\filesh\\data", i, ".txt", sep=""))

4 つのテキスト ファイル data1、data2、data3、......を取得します。

4-すべての結果を 1 つのテキスト ファイルに書き込む方法を教えていただければ幸いです。出力を、すべての結果を含む 1 つのテキスト ファイルにしたいと考えています。

 class            1    2    3    4   5   6  7 ...
 data1            0.2 0.5   0.2  .   .   .  . ...
 data2            0.1  0.5  0.6
 data3            .    .    .   .   .    .  . ...
 data4            .
4

1 に答える 1

1

作成したファイルを保存するには:

for (i in seq_along(results)){
    write.table(results[[i]], "C:\\filesh\\data%03d.txt", sep="\t")
}

もしかして:

for (i in seq_along(results)){
    write.table(results[[i]], paste("C:\\filesh\\data", i, ".txt", sep=""))
于 2013-03-11T16:02:14.337 に答える