How can I write random numbers in a matrix which are divisible by 5 in R? For instance, 20 20 85 45 55 5 15 20 90 10
CodePudding user response:
Generate some (20) random integers (say between 1 and 10):
x <- sample(1:10, size=20, replace=T)
Multiple those integers by 5
x <- x * 5
Put them in a matrix
x <- matrix(x, nrow=4, ncol=5)
Or do it all in one go:
matrix(sample(1:10,20,T)*5, 4, 5)
CodePudding user response:
for example a matrix with 4 rows:
matrix(sample(seq(0,100,5),20, rep=T), 4)
[,1] [,2] [,3] [,4] [,5]
[1,] 45 80 90 60 5
[2,] 95 35 0 15 65
[3,] 40 55 30 75 85
[4,] 70 20 50 10 25
CodePudding user response:
All the multiples of 5 in your range in a random order
set.seed(123)
sample(1:10)*5
matrix(sample(1:10)*5)
[,1]
[1,] 40
[2,] 10
[3,] 25
[4,] 35
[5,] 15
[6,] 20
[7,] 5
[8,] 45
[9,] 50
[10,] 30