0

我有这个用于平滑光谱的代码!

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。文件?

4

1 回答 1

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扩展名。

于 2017-04-22T16:05:27.140 回答