簡體   English   中英

R-如何組合來自循環輸出的向量

[英]R - how to combine vectors from loop output

我創建了一個函數lpnorm,它為每個輸入返回一個值'x'。

for (t in 1:6){
    M= data[t:(t+3), ]
    lpnorm(M)
    }

當我循環該函數時,它會產生正確的輸出,但是矢量似乎都是獨立的

[1] 0.0003370998
[1] 0.0003379513
[1] 0.0002855089
[1] 0.0003535439
[1] 0.0003683093
[1] 0.0003443804

因此,當我嘗試繪制數據時:

for (t in 1:6){
    M= data[t:(t+3), ]
    plot(t, lpnorm(M))
    }

它們都出現在單獨的圖上

沒有可復制的示例,您可以執行以下操作:

res <- matrix(NA, nrow=6, ncol=2) ##intitialize a matrix to store the results
for (t in 1:6){
    M <- data[t:(t+3), ]
    res[t, ] <- c(t, lpnorm(M)) #for each value of t we save the results in the res matrix
    }

plot(res[,1], res[, 2])

獲得每個lpnorm(M)值的圖的原因是因為您沒有保存每次迭代的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM