Re: [R] Selecting rows/columns of a matrix

2014-10-26 Thread David L Carlson
Anthropology Texas A&M University -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Steven Yen Sent: Sunday, October 26, 2014 1:57 PM To: Rui Barradas; r-help Subject: Re: [R] Selecting rows/columns of a matrix Rui Thanks. Thi

Re: [R] Selecting rows/columns of a matrix

2014-10-26 Thread William Dunlap
Use logical vectors and the "[" operator: x[condition] can be read as 'x such that condition [is true]'. E.g., > a<-matrix(1:16, 4,4,byrow=T) > j <- c(TRUE, FALSE, TRUE, FALSE) > a[j,,drop=FALSE] [,1] [,2] [,3] [,4] [1,]1234 [2,]9 10 11 12 > a[,

Re: [R] Selecting rows/columns of a matrix

2014-10-26 Thread Steven Yen
Rui Thanks. This works great. Below, I get the 2nd, 4th, and 6th rows/columns: > (a<-matrix(1:36,6,6)) [,1] [,2] [,3] [,4] [,5] [,6] [1,]17 13 19 25 31 [2,]28 14 20 26 32 [3,]39 15 21 27 33 [4,]4 10 16 22 28 34 [5,]5 11 1

Re: [R] Selecting rows/columns of a matrix

2014-10-26 Thread Rui Barradas
Sorry, that should be t(a[as.logical(j), as.logical(j)]) Rui Barradas Em 26-10-2014 18:45, Rui Barradas escreveu: Hello, Try the following. a[as.logical(j), as.logical(j)] # or b <- a[as.logical(j), ] t(b)[as.logical(j), ] Hope this helps, Rui Barradas Em 26-10-2014 18:35, Steven Yen es

Re: [R] Selecting rows/columns of a matrix

2014-10-26 Thread Rui Barradas
Hello, Try the following. a[as.logical(j), as.logical(j)] # or b <- a[as.logical(j), ] t(b)[as.logical(j), ] Hope this helps, Rui Barradas Em 26-10-2014 18:35, Steven Yen escreveu: Dear I am interested in selecting rows and columns of a matrix with a criterion defined by a binary indicato

[R] Selecting rows/columns of a matrix

2014-10-26 Thread Steven Yen
Dear I am interested in selecting rows and columns of a matrix with a criterion defined by a binary indicator vector. Let matrix a be > a<-matrix(1:16, 4,4,byrow=T) > a [,1] [,2] [,3] [,4] [1,]1234 [2,]5678 [3,]9 10 11 12 [4,] 13 14 15 16