I want to create an empty integers arrays

Hello everyone, please I need help with this code.

Inside the following loop, I want to create three empty integer arrays to save the value of f, x and y inside it. I want the arrays are unknown size because my code uses to generate random samples from distributions, in the f=rpois(1,4) I generate one value from Poisson distribution if the value from this command is (1) so, in x = rexp(f,1/2) I will generate one value from exponential distribution and in y= rexp(f,1/4) I will generate one value from exponential distribution but if the result from this f=rpois(1,4) command is (2) so I will generate two values from x and two values from y and so on. So the size of x and y arrays depends on the value of f and f is random (if f is 3 then x are 3 values and y are 3 values if f is 4 then x are 4 values and y are 4 values ....).

count=0

    for (j in 1:10000000) 
{
      f = rpois(1,4)
      
      if(f == 0)
        {
        count = count + 1
        next
        }
    
        xx = rexp(f,1/2)
        yy = rexp(f,1/4)
}

Please help.

Thank you in advance,

You could work with lists in the following way :

set.seed(2021)

L1 <- list()
for (i in 1:20) {
  f <- rpois(1, 2)
  if (f == 0) {
    L2 <- list(-99)
  } else {
    L2 <- list(rexp(f, 1 / 2))
  }
  L1 <- append(L1, L2)
}
str(L1)
#> List of 20
#>  $ : num [1:2] 1.135 0.839
#>  $ : num 0.545
#>  $ : num [1:3] 0.562 1.52 1.262
#>  $ : num [1:5] 7.768 0.27 1.28 1.399 0.022
#>  $ : num [1:4] 0.194 0.372 0.289 1.221
#>  $ : num [1:4] 1.26 5.643 1.251 0.209
#>  $ : num 0.333
#>  $ : num [1:2] 3.54 0.591
#>  $ : num 1.18
#>  $ : num [1:4] 7.067 6.879 1.293 0.168
#>  $ : num [1:2] 4.488 0.125
#>  $ : num 3.77
#>  $ : num [1:2] 2.435 0.0343
#>  $ : num [1:2] 4.52 2.55
#>  $ : num [1:3] 4.27 1.57 6.61
#>  $ : num -99
#>  $ : num [1:2] 5.334 0.101
#>  $ : num [1:2] 1.699 0.773
#>  $ : num [1:6] 0.185 2.655 0.506 3.912 1.683 ...
#>  $ : num [1:4] 0.49 0.914 1.022 0.781
Created on 2021-09-02 by the reprex package (v2.0.0)

Thank you so much for helping.

Thank you for the help but I want the result of f storage on the list, x in the other list, and y also in the other list. I want all of them individually.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.