R 中累积和的向量

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 循环。