尝试将多个jpeg写入r中的文件时出错



我有600多个WAV文件正在尝试绘制并写入一个文件。我做了一个循环来做这个,但它给出了一个错误,上面写着"中的错误audiomoth13_allfiles@left:尝试获得插槽";"左";从基类的对象("列表"(中没有插槽;

下面是我产生错误的代码。

for (audiomoth13wavfiles in seq_along(audiomoth13_allfiles)) {
audiomoth13_allfiles<-lapply(audiomoth13wavfiles,readWave)
snd<-audiomoth13_allfiles@left 
fs<-audiomoth13_allfiles@samp.rate
dur<-length(snd)/audiomoth13_allfiles@samp.rate
snd<-snd-mean(snd)
timearray<-(0:(5760000-1))/audiomoth13_allfiles@samp.rate
jpeg(file= paste0("C:/ACLF_Audiomoth_13/ACLF_Audiomoth_13/plots",names(audiomoth13wavfiles) 
[audiomoth13wavfiles],".jpeg"))
plot(timearray,snd,type='l',xlab='Time',ylab='Amplitude')
dev.off()} 

无论我是否将audiomoth13_allfiles&lt-lapply(audiomoth13wavfiles,readWave(消息灵通的audiomoth13_allfiles包含作为带有插槽的S4对象的所有文件

有别的办法吗?谢谢

您正在将for循环与lapply合并,这有点多余。尝试类似于下面显示的内容(或者使用for循环而不是lapply(。这个想法是循环遍历所有.wav文件的索引。通过这种方式,您可以对文件名和使用readWave读取的文件使用相同的索引。

library(tuneR)
wavfiles <- list.files(pattern="*.wav")
lapply(seq_along(wavfiles), function(x){
wav <- readWave(wavfiles[x])
snd <- wav@left
fs <- wav@samp.rate
dur <- length(snd)/fs
snd <- snd - mean(snd)
timearray <- seq(0, dur, length.out=length(snd))
jpeg(file=paste0(tools::file_path_sans_ext(wavfiles[x]), "_plot.jpg"))
plot(snd~timearray, type='l', xlab='Time', ylab='Amplitude')
dev.off()}
)

最新更新