Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the "[" function to select a row / column of a matrix

Tags:

r

How can the "[" function be used to select a column or a row of a matrix?

x <- matrix(1:4, ncol=2)

As far as I understand, these two lines do the same thing:

x[1,2]
"["(x,1,2) 

Also these two:

x[4]
"["(x,4) 

But how can one rewrite

x[2,]

using "["(...) ?

like image 398
Ape Avatar asked Jan 22 '14 10:01

Ape


People also ask

How do you select a column of a matrix?

To get a specific column of a matrix, specify the column number preceded by a comma, in square brackets, after the matrix variable name. This expression returns the required row as a vector.

How do you select a row in a matrix?

Similar to vectors, you can use the square brackets [ ] to select one or multiple elements from a matrix. Whereas vectors have one dimension, matrices have two dimensions. You should therefore use a comma to separate the rows you want to select from the columns.


1 Answers

Just leave the argument blank

"["(x, 2, )   # second row  
[1] 2 4

"["(x,  ,2)    # second column
[1] 3 4
like image 139
Mark Heckmann Avatar answered Nov 01 '22 16:11

Mark Heckmann