jeudi 20 décembre 2018

dynamic number of columns in a data table with unique random draws

Suppose I have the following matrix, for arbitrary J:

set.seed(1)
J=2
n = 100
BB = data.table(r=1:n)
BB[, (paste0("a",seq(J))) := rnorm(n,1,7) ]

So the output is...

> BB
       r           a1           a2
  1:   1  -3.38517668  -3.38517668
  2:   2   2.28550327   2.28550327
  3:   3  -4.84940029  -4.84940029
      ...

How come the two columns are identical and now different rnorms?




Aucun commentaire:

Enregistrer un commentaire