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,] 1 7 13 19 25 31
[2,] 2 8 14 20 26 32
[3,] 3 9 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,] 1 2 3 4
>>>[2,] 5 6 7 8
>>>[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 at 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 at 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.
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 A&M University -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at 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,] 1 7 13 19 25 31 [2,] 2 8 14 20 26 32 [3,] 3 9 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,] 1 2 3 4 >>>[2,] 5 6 7 8 >>>[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 at 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 at 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 at 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.
Steven Yen
2014-Oct-29 15:41 UTC
[R] Injecting a column of characters to a matrix of numerics
Hello
I am designing a regression printout, which works out nicely. Then, I
try to inject a column of characters to indicate a discrete regressor
with a dot (.). Then, all numbers seem to turn into characters, in
quotations. Is there a way to do this right? Below, I show the lines
of codes before and after. Thanks.
Steven Yen
---
out<-round(cbind(me,se,t,p),digits)
colnames(out)<-c("estimates","s.e.","|t-value|","p-value")
rownames(out)<-rownames(me)
out
estimates s.e. |t-value| p-value
(Intercept) 0.223263 0.146167 1.527459 0.127173
sex 0.049830 0.039612 1.257973 0.208890
age -0.070423 0.029539 2.384035 0.017433
yrmarry 0.015567 0.005298 2.938126 0.003429
children 0.060525 0.044778 1.351659 0.176993
religius -0.053128 0.014413 3.686260 0.000248
educ 0.003226 0.008453 0.381636 0.702866
occu 0.003915 0.011860 0.330147 0.741404
rating -0.077856 0.014466 5.381925 0.000000
out<-round(cbind(me,se,t,p),digits); out<-cbind(out,disc)
colnames(out)<-c("estimates","s.e.","|t-value|","p-value","disc")
rownames(out)<-rownames(me)
(Intercept) "0.223263" "0.146167" "1.527459"
"0.127173" ""
sex "0.04983" "0.039612" "1.257973"
"0.20889" "."
age "-0.070423" "0.029539" "2.384035"
"0.017433" ""
yrmarry "0.015567" "0.005298" "2.938126"
"0.003429" ""
children "0.060525" "0.044778" "1.351659"
"0.176993" "."
religius "-0.053128" "0.014413" "3.68626"
"0.000248" ""
educ "0.003226" "0.008453" "0.381636"
"0.702866" ""
occu "0.003915" "0.01186" "0.330147"
"0.741404" ""
rating "-0.077856" "0.014466" "5.381925"
"0" ""
My obj does not always come with a logical variable defined. So I do
my.foo <- function(obj,df,digits=5){
if (!is.na("obj$spec$Fisher")) Fisher<-obj$spec$Fisher
...
}
This works when "Fisher" is defined in/passed from obj. When it is
not, I get error:
Error in (!is.na("obj$spec$Fisher")) & Fisher :
operations are possible only for numeric, logical or complex types
I tried exist(Fisher), missing(Fisher)... to no vail. Any idea? Thanks.