[英]Vector of cumulative sums in R
我正在嘗試獲取累積總和的向量,也就是說,我有:
# 500 Samples from the U(0,1) Distribution
U<-runif(500,0,1)
# Empty Vector of length 500
F<-rep(0,500)
# Fill the vector with f(U(k))
for ( i in 1:500 ){
F[i] <- sqrt(1-U[i]^2)
}
# Another Empty Vector of length 500
I<-rep(0,500)
# Fill the second empty vector with the sums of F
for ( i in 1:500 ){
I[i]<-cumsum(F[1]:F[i])
}
代碼的最后一行是問題,我希望“ I”成為一個向量,使得I [1] = F [1],I [n] = F [1] + F [2] + ..... + F [n]。 出於某些原因,cumsum函數無法正常工作。 嘗試這樣做有什么問題?
如果我有誤會,請糾正我,但我相信你只是想要這樣:
I <- cumsum(sqrt(1 - U^2))
目前尚不清楚為什么要使用for
循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.