使用R将.sp文件(来自Perkin Elmer)合并到矩阵中

时间:2014-02-03 15:40:55

标签: r matrix merge

我是R的新手。我有分光光度计的数据,我想把我的所有数据都处理一次。

我有一个巨大的目录,每个样本有45个.sp文件(暂时有大约200个样本)。我想通过将45个文件合并到一个矩阵中来为每个样本构建一个矩阵,其中发射和提取为列和行名称。文件名定义为:blahblah#01.sp,blahblah#02.sp到blahblah#45.sp(第一个样本),toptop#01.sp,toptop#45.sp,并且在同一目录中我有所有其他样本。

是否可以将具有相同前缀(使用#01.sp ...)的所有sp文件合并到一个矩阵中,并将所有样本合并...

到目前为止,我已经尝试过这个:

## Set up data paths and read mask from file
setwd("C:/EEMs/")
getwd()

## Define paths for data, results table
datainpath<-"C:/EEMs/rawdata/"
dataoutpath<-"C:/EEMs/results/"


## Get list of sp files in the data directory that will be processed and print a list
files<-list.files(datainpath,pattern="\\.sp$")
print(files)

filename<-substring(files,1,nchar(files)-6)
print(filename)


### - 2. Define excitation and emission ranges for matrix
emission<-seq(250,600.5,0.5) # define emission wavelength range for matrix (start, finish, by) (nm)
excitation<-seq(200,420,5)# define excitation wavelength range for matrix (start, finish, by) (nm)
# Number of files per spectral
nfic<-45
# # Number of measurements per spectral
nc<-702

### - 3. Read in sample and blank EEMs files
## - 3.1.: Start loop through files in data folder
for (Samplefile in files) {

filename2<-paste(dataoutpath,filename,"#",as.character(0),as.character(1),".sp",sep="")
fich<-matrix(scan(filename2,skip=54),ncol=2,byrow=T)

dataechant<-matrix(NA,nrow=nc,ncol=nfic)
dimnames(dataechant)<-list(as.character(emission),as.character(excitation))
dataechant[,1]<-fich[,2]
for (i in 2:9)

{
filename2<-paste(dataoutpath,filename,"#",as.character(0),as.character(i),".sp",sep="")
fich<-matrix(scan(filename2,skip=54),ncol=2,byrow=T)
dataechant[,i]<-fich[,2]
}
for (i in 10:45)
 {
filename2<-paste(dataoutpath,filename,"#",as.character(i),".sp",sep="")
fich<-matrix(scan(filename2,skip=54),ncol=2,byrow=T)
dataechant[,i]<-fich[,2]
}
}

#Export to EEM result dir 
write.csv2(dataechant,paste("results/",filename,".csv"))

如果我走在正确的道路上,请告诉我。

0 个答案:

没有答案