Aolin Gong
Aolin Gong

Reputation: 11

R: Fill a Matrix with Random Number with no repeat vertically or horizontally

I would like to generate a matrix in Random Number with no repeat vertically or horizontally, just as this question described in the following link: link to the similar questions

Only I want to do it in R.

Any ideas? Many thanks!

I need to specify the questions better.

I am randomizing the same integers both vertically and horizontally. Eg: meaning, if I am randomizing 1-4, then I want something as below:

1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3

Upvotes: 0

Views: 132

Answers (1)

Vinícius Félix
Vinícius Félix

Reputation: 8811

#Random sequence from 1 to 9

x <- sample(x = 1:9,size = 9,replace = FALSE)


#Create matrix from x, with 3 rows and 3 cols

matrix(x,nrow = 3,ncol = 3)


     [,1] [,2] [,3]
[1,]    2    1    7
[2,]    5    8    3
[3,]    6    4    9

Upvotes: 2

Related Questions