Ape
Ape

Reputation: 1169

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

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 "["(...) ?

Upvotes: 5

Views: 873

Answers (1)

Mark Heckmann
Mark Heckmann

Reputation: 11441

Just leave the argument blank

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

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

Upvotes: 5

Related Questions