user1631306
user1631306

Reputation: 4470

Select n first rows of a 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

Upvotes: 14

Views: 43729

Answers (2)

Doctor Dan
Doctor Dan

Reputation: 771

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)

Upvotes: 13

Thomas
Thomas

Reputation: 44527

Use the head function:

head(mat, 100)

Upvotes: 25

Related Questions