我有这个代码用于平滑光谱!
list_tot <- list.files(path = ".", pattern="*.txt")
num <- as.integer(length(list_tot))
library(data.table)
DT_final_tot <- fread(file = list_tot[1])
setnames(DT_final_tot, c("Raman shift (cm-1)", list_tot[1]))
x <-DT_final_tot[[1]]
y <-DT_final_tot[[2]]
smooth_spectra <- smooth.spline(x,y, spar = NULL)
plot(x,y, type = "l", main="raw spectra", col="green")
lines(smooth_spectra,type = "l")
plot(smooth_spectra,type = "l", main="smooth spectra ")
我已将代码应用于该文件夹的第一个文件!如何将其应用于所有文件,如何将平滑后的光谱保存为txt。文件?
答案 0 :(得分:1)
library(data.table)
list_tot <- list.files(path = ".", pattern="*.txt")
num <- as.integer(length(list_tot))
for(fname in list_tot) {
DT_final_tot <- fread(file = fname)
setnames(DT_final_tot, c("Raman shift (cm-1)", fname))
x <-DT_final_tot[[1]]
y <-DT_final_tot[[2]]
smooth_spectra <- smooth.spline(x,y, spar = NULL)
plot(x,y, type = "l", main="raw spectra", col="green")
lines(smooth_spectra,type = "l")
plot(smooth_spectra,type = "l", main="smooth spectra ")
dump(c("smooth_spectra"), file=paste0(tools::file_path_sans_ext(fname), "_smoothed", ".csv"))
}
您应该引入for
循环来迭代list_tot
。它会将smooth_spectra
保存在文件中,其名称与输入文件相同,并带有_smoothed
前缀和.csv
扩展名。