将打印输出保存为R中的数字列表?

将打印输出保存为R中的数字列表?,r,list,function,R,List,Function,我正在编写一个函数,用于测量音频文件列表中单个发声的持续时间。我需要将这些通话持续时间保存为按文件组织的数字列表。当我编写函数来打印任务时,它会按我的需要打印列表,但不会将它们保存到外部变量中。但是,当我退出print函数时,所有调用都将保存到一个列表中,而不指定它们来自哪个文件。有什么想法吗?谢谢 输入: callduration2 <- function(x) { for (i in x) { print(timer(i, threshold = 2, msmooth

我正在编写一个函数,用于测量音频文件列表中单个发声的持续时间。我需要将这些通话持续时间保存为按文件组织的数字列表。当我编写函数来打印任务时,它会按我的需要打印列表,但不会将它们保存到外部变量中。但是,当我退出print函数时,所有调用都将保存到一个列表中,而不指定它们来自哪个文件。有什么想法吗?谢谢

输入:

callduration2 <- function(x) {
  for (i in x) {
      print(timer(i, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)$s.end - timer(i, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)$s.start)
  }
}
callduration <- function(x) {
  output9 <- list()
  for (i in x) {
    i.t <- timer(i, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)
    output9 <- append(output9, i.t$s.end - i.t$s.start) 
  }
  output9
}

输入:

callduration2 <- function(x) {
  for (i in x) {
      print(timer(i, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)$s.end - timer(i, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)$s.start)
  }
}
callduration <- function(x) {
  output9 <- list()
  for (i in x) {
    i.t <- timer(i, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)
    output9 <- append(output9, i.t$s.end - i.t$s.start) 
  }
  output9
}

我在黑暗中拍摄。我猜是这个。请记住,在函数中,将返回最后一个对象。如果愿意,也可以使用命令
return
指定它。在第一个代码中,您循环了x,但没有存储结果并告诉函数输出任何内容

在您的第二个代码中,您将结果存储在一个列表中并返回它,但我不知道您到底在寻找哪种输出


callduration2也许你可以这样写:
duration是一个度量持续时间的函数,callduration返回的是持续时间向量,其名称与callduration的参数相同

duration <- function(x) {
    t <- timer(x, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)
    t$s.end - t$s.start
}

callduration <- function(xs) {
    durations <- sapply(xs, duration)
    names(durations) <- names(xs)
    durations
}

duration您的输入数据是什么样子的?
计时器从哪里来,它是否矢量化?你需要编辑。一般来说,
lappy
for
循环更有用,因为您可以根据自己的喜好构造结果。您好,欢迎使用SO。我不太明白你说的“保存”是什么意思。你能更详细地描述一下你是如何读取这些文件的吗?要在@alistaire上展开一点,好的建议类似于
lapply(x,timer,threshold=2,msmooth=c(400,90),dmin=0.1,plot=FALSE)
将返回一个列表。这将为通过附加参数(未测试)的x的每个元素调用计时器