繁体   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