Hi,
I am new with R.
Need Help on this. I would like to create a matrix of a random integer.
Example:
Currently, I am using "sample" function. This will create a matrix of integer but some integer values by row are similar.
3 2 4 1
4 1 3 2
1 3 1 3
2 4 2 4
Actually, I would like to generate this type of matrix:
3 2 4 1
4 1 3 2
1 3 2 4
2 4 1 3
Any idea of doing this. Really appreciate any help. Thanks.
Welcome to the community!
If I understand correctly, you want a permutation of 1, 2, 3, 4 in each row. You can do something like this:
set.seed(seed = 34824)
t(x = replicate(n = 4,
expr = sample(x = 1:4)))
#> [,1] [,2] [,3] [,4]
#> [1,] 4 1 3 2
#> [2,] 2 4 3 1
#> [3,] 4 3 2 1
#> [4,] 4 2 1 3
Created on 2019-07-08 by the reprex package (v0.3.0)
However, there is no guarantee that two rows won't be identical, and there is also no guarantee that each column will also be a permutation. Do you have these constraints too?