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 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

Elsewhere in Gauss, I select the first and third rows and columns of a
by defining a column vector j = [1,0,1,0]. Then, select the rows of a
using j, and then selecting the rows of the transpose of the resulting
matrix using j again. I get the 2 x 2 matrix as desired. Is there a way
to do this in R? below are my Gauss commands. Thank you.

---

j

1
0
1
0

a=selif(a,j); a

1  2  3  4
9 10 11 12

a=selif(a',j); a

1  9
3 11

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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 escreveu:

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

Elsewhere in Gauss, I select the first and third rows and columns of a
by defining a column vector j = [1,0,1,0]. Then, select the rows of a
using j, and then selecting the rows of the transpose of the resulting
matrix using j again. I get the 2 x 2 matrix as desired. Is there a way
to do this in R? below are my Gauss commands. Thank you.

---

j

1
0
1
0

a=selif(a,j); a

1  2  3  4
9 10 11 12

a=selif(a',j); a

1  9
3 11

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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   17   23   29   35
[6,]6   12   18   24   30   36
 (j-matrix(c(0,1,0,1,0,1)))
 [,1]
[1,]0
[2,]1
[3,]0
[4,]1
[5,]0
[6,]1
 ((a[as.logical(j), as.logical(j)]))
 [,1] [,2] [,3]
[1,]8   20   32
[2,]   10   22   34
[3,]   12   24   36

Steven Yen

At 02:49 PM 10/26/2014, Rui Barradas wrote:

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 escreveu:

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

Elsewhere in Gauss, I select the first and third rows and columns of a
by defining a column vector j = [1,0,1,0]. Then, select the rows of a
using j, and then selecting the rows of the transpose of the resulting
matrix using j again. I get the 2 x 2 matrix as desired. Is there a way
to do this in R? below are my Gauss commands. Thank you.

---

j

1
0
1
0

a=selif(a,j); a

1  2  3  4
9 10 11 12

a=selif(a',j); a

1  9
3 11

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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[,j,drop=FALSE]
[,1] [,2]
   [1,]13
   [2,]57
   [3,]9   11
   [4,]   13   15
a[j,j,drop=FALSE] # apply t() to the result if you want the transpose
[,1] [,2]
   [1,]13
   [2,]9   11
The drop=FALSE is not needed in these examples, but prevents single-row
or -column results from be converted to vectors.

See An Introduction to R (under HelpManuals in the Windows GUI,
or look on the R website) for details on these and many other issues.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sun, Oct 26, 2014 at 11:35 AM, Steven Yen sye...@gmail.com wrote:
 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

 Elsewhere in Gauss, I select the first and third rows and columns of a by
 defining a column vector j = [1,0,1,0]. Then, select the rows of a using j,
 and then selecting the rows of the transpose of the resulting matrix using j
 again. I get the 2 x 2 matrix as desired. Is there a way to do this in R?
 below are my Gauss commands. Thank you.

 ---

 j

 1
 0
 1
 0

 a=selif(a,j); a

 1  2  3  4
 9 10 11 12

 a=selif(a',j); a

 1  9
 3 11

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


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

2014-10-26 Thread David L Carlson
Note that you do not have to create the vector of 1's (TRUE) and 0's (FALSE) if 
you know the index values:

 j - c(2, 4, 6)
 a[j, j]
 [,1] [,2] [,3]
[1,]8   20   32
[2,]   10   22   34
[3,]   12   24   36

==
David L. Carlson
Department of Anthropology
Texas AM 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. 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   17   23   29   35
[6,]6   12   18   24   30   36
  (j-matrix(c(0,1,0,1,0,1)))
  [,1]
[1,]0
[2,]1
[3,]0
[4,]1
[5,]0
[6,]1
  ((a[as.logical(j), as.logical(j)]))
  [,1] [,2] [,3]
[1,]8   20   32
[2,]   10   22   34
[3,]   12   24   36

Steven Yen

At 02:49 PM 10/26/2014, Rui Barradas wrote:
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 escreveu:
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

Elsewhere in Gauss, I select the first and third rows and columns of 
a by defining a column vector j = [1,0,1,0]. Then, select the rows of 
a using j, and then selecting the rows of the transpose of the 
resulting matrix using j again. I get the 2 x 2 matrix as desired. Is 
there a way to do this in R? below are my Gauss commands. Thank you.

---

j

1
0
1
0

a=selif(a,j); a

1  2  3  4
9 10 11 12

a=selif(a',j); a

1  9
3 11

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.