Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 995
S12000 Avatar asked Jun 04 '13 10:06

S12000


People also ask

How do I print the number of rows and columns in R?

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.

How do you create a matrix from a data set in R?

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.

How do I manually create a matrix in R?

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.


1 Answers

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)
like image 129
flodel Avatar answered Oct 04 '22 15:10

flodel