我刚开始和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")您可以在这里找到一个示例数据集:样本数据集
发布于 2016-10-06 22:39:30
我使用了大量的实验室数据,可以依赖于输出文件的可靠格式(相同的列顺序、列名、头格式等)。因此,这是假设您拥有的.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,我认为这是处理日期和时间的更好方法。
https://stackoverflow.com/questions/39899329
复制相似问题