S12000
S12000

Reputation: 3396

R generate an simple integer matrix with defined number of row and column

How to generate an integer random matrix with value from {1 ... 15} in r with 9 row and 100 column for instance ?

(My question may be basic but for unknown reasons I can't find a solution)

Upvotes: 8

Views: 14120

Answers (1)

flodel
flodel

Reputation: 89057

matrix(sample.int(15, size = 9*100, replace = TRUE), nrow = 9, ncol = 100)

or the more concise version

matrix(sample.int(15, 9*100, TRUE), 9, 100)

but if you are really going for minimum number of characters (I would not recommend):

matrix(sample(15,900,T),9)

Upvotes: 17

Related Questions