mercredi 17 mars 2021

Save output in multiple for loops with if-else

enter image description here

Please see the above equation. As you can see 0<= i <j <= n. I wrote the following command in R. When i=0, we consider X_0 = 0. I generated two observations n=2 and I manually calculated the values. They are, 4.540201, 1.460604. However, my r codes overwrite the results. I got this output 1.460604 1.460604. I couldn't figure it out. What is the reason for that?

rm(list = ls())
n = 2
set.seed(2)
x = rexp(n, 1)
xo = sort(x)
xo
value1 = NULL
for(k in 1:n){
  for(j in 1:n){
    for(i in 0:j-1){
      if(i==0){
        value1[k] =  ((n - j + 1)*sum(xo[i+1] - 0))
      }
      else{
        value1[k] =   ((n - j + 1)*sum(xo[i+1] - xo[i]))
      }
    }
    
  }
}

value1

Aucun commentaire:

Enregistrer un commentaire