我有一个平滑光谱的代码!
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。文件?
发布于 2017-04-23 00:05:27
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。它会将文件保存在文件中,文件名与输入文件相同,前缀为_smoothed,扩展名为.csv。
https://stackoverflow.com/questions/43560775
复制相似问题