1

私はRを扱うのはかなり新しいですが、これを成し遂げようとしています。ディレクトリに数十の ENVI スペクトル データセットが保存されています。各データセットは 2 つのファイルに分かれています。それらはすべて同じ命名規則を持っています。

  • ID_YYYYMMDD_350-200nm.asr
  • ID_YYYYMMDD_350-200nm.hdr

タスクは、データセットを読み取り、2 つの列 (ファイル名からの ID と日付) を追加し、結果を *.csv ファイルに保存することです。これを単一のファイル(ハードコード)で機能させるようにしました。

library(caTools)

setwd("D:/some/path/software_scripts")

### filename without extension
name <- "011a_20100509_350-2500nm"

### split filename in area-id and date
flaeche<-substr(name, 0, 4)
date <- as.Date((substr(name,6,13)),"%Y%m%d")

### get values from ENVI-file in a matrix
spectrum <- read.ENVI(paste(name,".esl", sep = ""), headerfile=paste(name,".hdr", sep=""))

### add columns
spectrum <- cbind(Flaeche=flaeche,Datum=as.character(date),spectrum)


### CSV-Dataset with all values
write.csv(spectrum, file = name,".csv", sep=",")

利用可能なすべてのファイルを 1 つの *.csv ファイルに結合したいと考えています。list.files を使用する必要があることはわかっていますが、read.ENVI 関数を実装して結果の行列を継続的に CSV に追加する方法がわかりません。


アップデート:

library(caTools)

setwd("D:/some/path/mean")

files <- list.files() # change or leave totally empty if setwd() put you in the right spot

all_names <- sub("^([^.]*).*", "\\1", files) # strip off extensions

name <- unique(all_names) # get rid of duplicates from .esl and .hdr

# wrap your existing code in a function
mungeENVI <- function(name) {

  # split filename in area-id and date
  flaeche<-substr(name, 0, 4)
  date <- as.Date((substr(name,6,13)),"%Y%m%d")

  # get values from ENVI-file in a matrix
  spectrum <- read.ENVI(paste(name,".esl", sep = ""), headerfile=paste(name,".hdr", sep=""))

  # add columns
  spectrum <- cbind(Flaeche=flaeche,Datum=as.character(date),spectrum)
  return(spectrum)
}

# use lapply to 'loop' over each name
list_of_ENVIs <- lapply(name, mungeENVI) # returns a list

# use do.call(rbind, x) to turn it into a big data.frame
final_df <- do.call(rbind, list_of_ENVIs)

# now write output
write.csv(final_df, "all_results.csv")

ここでサンプル データセットを見つけることができます:サンプル データセット

4

1 に答える 1

0

出力ファイルが信頼できる形式 (同じ列の順序、列名、ヘッダー形式など) であることを信頼できる多くのラボ データを扱っています。したがって、これはあなたが持っている.ENVIファイルがそれに似ていると仮定しています. あなたのファイルがそうでない場合は、喜んでお手伝いします。ダミー ファイルを 1 つか 2 つ確認する必要があります。

とにかくここにアイデアがあります:

library(caTools)
library(lubridate)
library(magrittr)

setwd("~/Binfo/TST/Stack/") # adjust as needed

files <- list.files("data/", full.name = T) # adjust as needed
all_names <- gsub("\\.\\D{3}", "", files) # strip off extensions
names1 <- unique(all_names) # get rid of duplicates

# wrap your existing code in a function
mungeENVI <- function(name) {
    # split filename in area-id and date
    f <- gsub(".*\\/(\\d{3}\\D)_.*", "\\1", name)
    d <- gsub(".*_(\\d+)_.*", "\\1", name) %>% ymd()
    # get values from ENVI-file in a matrix
    spectrum <- read.ENVI(paste(name,".esl", sep = ""), headerfile=paste(name,".hdr", sep=""))
    # add columns
    spectrum <- cbind(Flaeche=f,Datum= as.character(d),spectrum)
    return(spectrum)
}
# use lapply to 'loop' over each name
list_of_ENVIs <- lapply(names1, mungeENVI) # returns a list

# use do.call(rbind, x) to turn it into a big data.frame
final_df <- do.call(rbind, list_of_ENVIs)
# now write output
write.csv(final_df, "data/all_results.csv")

何か問題がありましたらお知らせください。乾杯。

私は自分の答えを少し編集しました。あなたが打っていた問題にlist.files()は議論があったはずfull.name = Tです。また、解析方法をもう少し防御的に調整し、grep キャプチャ式を使用しました。2 つのサンプル ファイル (実際には 4 つ) を使用してコードをテストしましたが、大きなマトリックス (66743 要素) を作成できます。またlubridate、私は を使用しましたが、日付と時刻を操作するためのより良い方法だと思います。

于 2016-10-06T22:39:30.527 に答える