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)
The ncol() function in R programming R programming helps us with ncol() function by which we can get the information on the count of the columns of the object. That is, ncol() function returns the total number of columns present in the object.
Convert a Data Frame into a Numeric Matrix in R Programming – data. matrix() Function. data. matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix.
To create a matrix in R you need to use the function called matrix(). The arguments to this matrix() are the set of elements in the vector. You have to pass how many numbers of rows and how many numbers of columns you want to have in your matrix. Note: By default, matrices are in column-wise order.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With