rmatrixrandom-seedpanel-data

Create a matrix with a random number of observations for each group-period


I want to create a matrix for N groups and T time periods. For each combination of T-N, I want to have a random number of lines.

The random number of lines for each N-T is given by round(runif(1,2,4)).

The goal is to have as input :

set.seed(123)
ngroup = 2
tperiod = 2

Using round(runif(1,2,4)) 4 times gives 3,4,3,4, so the output should have :

And as the output should be :

mat = cbind(c(1,1,1,1,1,1,1,2,2,2,2,2,2,2), c(1,1,1,2,2,2,2,1,1,1,2,2,2,2))
mat
 [,1] [,2]
 [1,]    1    1
 [2,]    1    1
 [3,]    1    1
 [4,]    1    2
 [5,]    1    2
 [6,]    1    2
 [7,]    1    2
 [8,]    2    1
 [9,]    2    1
[10,]    2    1
[11,]    2    2
[12,]    2    2
[13,]    2    2
[14,]    2    2

Where the 1st column is the group ID, and the 2nd the time ID.


Solution

  • Option 1: index the result of expand.grid.

    set.seed(123)
    N <- 1:2
    T <- 1:2
    combos <- length(N)*length(T)
    expand.grid(list(T = T, N = N))[rep.int(1:combos, round(runif(combos, 2, 4))),2:1]
    #>     N T
    #> 1   1 1
    #> 1.1 1 1
    #> 1.2 1 1
    #> 2   1 2
    #> 2.1 1 2
    #> 2.2 1 2
    #> 2.3 1 2
    #> 3   2 1
    #> 3.1 2 1
    #> 3.2 2 1
    #> 4   2 2
    #> 4.1 2 2
    #> 4.2 2 2
    #> 4.3 2 2
    

    Option 2: rep.int(rep(.

    set.seed(123)
    r <- round(runif(length(N)*length(T), 2, 4))
    data.frame(
      N = rep.int(rep(N, each = length(T)), r),
      T = rep.int(rep(T, length(N)), r)
    )
    #>    N T
    #> 1  1 1
    #> 2  1 1
    #> 3  1 1
    #> 4  1 2
    #> 5  1 2
    #> 6  1 2
    #> 7  1 2
    #> 8  2 1
    #> 9  2 1
    #> 10 2 1
    #> 11 2 2
    #> 12 2 2
    #> 13 2 2
    #> 14 2 2