循环错误-值未输入列

时间:2018-07-21 06:20:11

标签: r for-loop

我有一个数据框,其中的一列包含一堆声音文件文件名(.wav)。我想测量每个声音文件中的内容,并在对应文件名旁边的第二个新列中列出每个测量值。在我的代码中,每个度量均未放入第二列。如果我使用for循环的每一行并分别用值1、2、3等代替i来独立运行,则结果数据帧将正确输入输出测量值。

我在下面编写了一个玩具示例,没有一些wav文件就无法运行它,但也许仅凭代码就可以发现问题:

library(seewave); library(tuneR)
setwd("D:/wavs")

#make a dataframe containing a column of wav filenames
file_list <-data.frame(c("wav1.wav", "wav2.wav", "wav3.wav"))
colnames(file_list)[1] <-"filelist" #give it a sensible column name
file_list$filelist <-as.character(file_list$filelist) #convert from factor
file_list$mx <-NA #a new empty vector for the measurement results
str(file_list) #this is how it will look before adding measurements

#now read in each wav file, measure something,
#and put the outcome in the empty cell next to that corresponding filename.
for (i in length(file_list$filelist)){
  a <-as.character(file_list$filelist[[i]]) #this seemed to be a requirement, that 'wav1.wav' etc be a character
  temp <-readWave(a) #read the file using tuneR package
  mx <-max(range(temp@left)) #take some measurement from the left channel
  file_list$mx[[i]] <-mx #put it in a new column next to the original filename
  rm(mx); rm(temp); rm(a) #kill unnecessary things before starting again, for just in case
} 

不用说,我已经在网络和Stackoverflow上搜索了没有成功的指导,并尝试了很多方法(例如,使用}next{)。也许我需要类似以下内容:file_list2$mx[i,] <-mx。也许对某人来说容易点?谢谢,我一直很感激SO方面的帮助。

2 个答案:

答案 0 :(得分:1)

您的代码唯一的问题是,您尚未迭代for循环。根据您的代码,for循环仅运行一次,即i = 3。正确的代码如下:

def $(Closure closure) {
    closure()
}

希望这可以帮助您!

答案 1 :(得分:1)

while (((c = getchar ()) != EOF) && (c!='\n')) 解决方案如何?

Kotlin