我在与R合作方面相当新,但试图完成这项工作。我有几十个ENVI光谱数据集存储在一个目录中。每个数据集分成两个文件。它们都具有相同的名称约定,即:
任务是读取数据集,添加两列(文件名中的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=",")
我想将所有可用文件合并到一个* .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")
您可以在此处找到示例数据集:Sample dataset
答案 0 :(得分:0)
我使用大量实验室数据,我可以依赖输出文件的可靠格式(相同的列顺序,列名,标题格式等)。所以这假设您拥有的.ENVI文件与此类似。如果您的文件不是这样的,我也很乐意为您提供帮助,我只需要查看一两个虚拟文件。
无论如何,这个想法是:
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捕获表达式。我用你的两个示例文件测试了代码(真的是4个),但我可以构建一个大矩阵(66743个元素)。我还使用了lubridate
,我认为这是处理日期和时间的更好方式。