0

I am writing a function that will measure the durations of individual vocalizations in a list of audio files. I need these call durations to be saved as numeric lists organized by file. When I write my function to print the task, it will print the lists as I want them but won't save them to an external variable. However, when I leave the print function out, all of the calls will save to one list without specifying which file they came from. Any ideas? Thanks.

Input:

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)
  }
}

Output:

[1] 0.1035461 4.1581914 1.4687190
[1] 0.2317160 0.3616587 0.3316719 0.2598854 0.2117248 0.2162683 0.1635642 1.0295460
[1] 0.1035461 4.1581914 1.4687190
[1] 0.2283603 0.1571119 0.1023054
[1] 0.2795895 0.2531787
[1] 0.7232425 1.0376167 0.5624210 0.1235691 0.3389063

OR

Input:

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
}

Output:

[[1]]
[1] 0.1035461

[[2]]
[1] 4.158191

[[3]]
[1] 1.468719

[[4]]
[1] 0.231716

[[5]]
[1] 0.3616587

[[6]]
[1] 0.3316719

[[7]]
[1] 0.2598854

[[8]]
[1] 0.2117248

[[9]]
[1] 0.2162683

[[10]]
[1] 0.1635642

[[11]]
[1] 1.029546

[[12]]
[1] 0.1035461

[[13]]
[1] 4.158191

[[14]]
[1] 1.468719

[[15]]
[1] 0.2283603

[[16]]
[1] 0.1571119

[[17]]
[1] 0.1023054

[[18]]
[1] 0.2795895

[[19]]
[1] 0.2531787

[[20]]
[1] 0.7232425

[[21]]
[1] 1.037617

[[22]]
[1] 0.562421

[[23]]
[1] 0.1235691

[[24]]
[1] 0.3389063
alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 3
    What does your input data look like? Where is `timer` from, and is it vectorized? You need to edit [to make your example reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610). More generally, `lapply` is more use than a `for` loop, as you can structure your result however you'd like. – alistaire May 06 '17 at 16:32
  • Hello welcome to SO. I don't understand exactly what you mean by save here. Can you describe in more details how you read the files? – DJJ May 06 '17 at 16:35
  • To expand a bit on @alistaire good suggestion something like `lapply(x, timer, threshold = 2, msmooth = c(400,90), dmin = 0.1, plot = FALSE)` will return a list. this will invoke timer, for each element of x passing the additional parameters (not tested) – Andrew Lavers May 06 '17 at 16:40

2 Answers2

0

I'm shooting in the dark. My guess would be this one. Remember that in a function will return the last object. You can specify it with the command return as well if you wish. In your first code, you looped through x but don't store the results and tell the function to output anything.

In your second code you store the results in a list and return it but I don't know exactly what kind of ouput you are looking for.

callduration2 <- function(x) { res <- matrix(nrow=length(x)) for (i in x) { res[i] <- 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 } return(res) }

myresults <- callduration2(x)

To avoid writing the loop you could do the following.

sapply(x,function(i) 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)

DJJ
  • 2,481
  • 2
  • 28
  • 53
0

Maybe you could write something like this: duration is a function to measure the duration, and what the callduration returns is a vector of the durations, with names the same as the parameter to 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
}
Consistency
  • 2,884
  • 15
  • 23