Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select n first rows of a matrix

Tags:

r

matrix

How can I select, say top 100 rows of a matrix in R? All I found is using subset which requires condition parameter. All I need to make smaller matrix by using only first n number of rows with same number of columns

like image 919
user1631306 Avatar asked Jul 23 '13 19:07

user1631306


People also ask

How do you select the first row of a matrix?

If you want to select all elements of a row or a column, no number is needed before or after the comma, respectively: my_matrix[,1] selects all elements of the first column. my_matrix[1,] selects all elements of the first row.

How do you sample rows of a matrix in R?

To take a random sample from a matrix in R, we can simply use sample function and if the sample size is larger than the number of elements in the matrix replace=TRUE argument will be used.

How do you get the first row of a matrix in python?

To print the first row use l[0] , to get columns you will need to transpose with zip print(list(zip(*l))[0]) .

How do you reference a row of a matrix?

An asterisk is occasionally used to refer to whole rows or columns in a matrix. For example, ai,∗ refers to the ith row of A, and a∗,j refers to the jth column of A.


2 Answers

Use the head function:

head(mat, 100)
like image 197
Thomas Avatar answered Oct 16 '22 00:10

Thomas


The simplest way to do it would be a[1:100,] (unless there are fewer than 100 rows, in which case head(a,100) works better)

like image 44
Doctor Dan Avatar answered Oct 16 '22 01:10

Doctor Dan