2017-05-06 7 views
0

私はオーディオファイルのリスト内の個々の発声の持続時間を測定する関数を書いています。私はこれらの通話時間が、ファイルごとにまとめられた数値リストとして保存される必要があります。タスクを印刷するために私の関数を書くとき、私はそれらを望むようにリストを出力しますが、それらを外部変数に保存しません。しかし、私がプリント機能を残しておけば、どのファイルから来たのかを特定せずにすべての呼び出しを1つのリストに保存します。何か案は?ありがとう。印刷出力を数値リストとしてRに保存しますか?

入力:

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

出力:

[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

入力:

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 
} 

出力:

[[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 
+3

入力データはどのように見えますか?どこから 'timer'があり、ベクトル化されていますか? [あなたの例を再現可能にする](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610)を編集する必要があります。より一般的には、 'lapply'は' for'ループよりも使いやすいですが、あなたが望むように結果を構造化することができます。 – alistaire

+0

こんにちは。私はあなたがここで救うことが何を意味するかを正確に理解していません。ファイルの読み方について詳しく説明できますか? – DJJ

+0

'lapply(x、timer、threshold = 2、msmooth = c(400,90)、dmin = 0.1、plot = FALSE)のような良い提案を少し拡張すると、リストが返されます。これは、(テストされていない)追加のパラメータを渡すxの各要素に対してタイマーを呼び出す – epi99

答えて

0

私は暗闇の中で撮影しています。私の推測はこれと同じです。関数では最後のオブジェクトが返されることに注意してください。必要に応じて、コマンドreturnで指定することもできます。あなたの最初のコードでは、xをループしましたが、結果を保存せずに何かを出力するように関数に指示します。

2番目のコードでは、結果をリストに保存して返しますが、探している出力の種類はわかりません。

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)

あなたが次のことを行うことができますループを書くことを避けるために。

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)

0

たぶん、あなたはこのような何か書くことができます: 期間は継続時間を計測する機能であり、どのような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 
} 
+0

これは完全に機能しました!どうもありがとうございます!! –

関連する問題