微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我有超过 600 个 WAV 文件,我正在尝试绘制和写入文件。我做了一个循环来做到这一点,但它给出了一个错误,说 “audiomoth13_allfiles@left 中的错误:试图从基本类的对象中获取插槽“左” ("list") 没有插槽"

下面是我产生错误代码

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

还有其他方法可以做到这一点吗?谢谢。

解决方法

您将 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()}
)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。