Re: [R] matrix evaluation using if function

From: ivan <i.petzev_at_gmail.com>
Date: Sun, 01 May 2011 20:37:53 +0200

Hi,

thank you very much, both methods worked perfectly.

Regards

On Fri, Apr 29, 2011 at 4:17 PM, Berend Hasselman <bhh_at_xs4all.nl> wrote:

>
> David Winsemius wrote:
> >
> > On Apr 29, 2011, at 4:27 AM, ivan wrote:
> >
> >> Hi All,
> >>
> >> I am trying to create a function which evaluates whether the values
> >> (which
> >> are equal to one) of a matrix are the same as their mirror values.
> >> Consider
> >> the following matrix:
> >>
> >>> n<-matrix(cbind(c(0,1,1),c(1,0,0),c(0,1,0)),3,3)
> >>> colnames(n)<-cbind("A","B","C");rownames(n)<-cbind("A","B","C")
> >>> n
> >> A B C
> >> A 0 1 0
> >> B 1 0 1
> >> C 1 0 0
> >>
> >> Hence, since n[2,1] and n[1,2] are 1 and the same, the function should
> >> return the name of the row of n[2,1]. I used the following function:
> >>
> >> for (i in length(rownames(n))) {
> >>
> >> for (j in length(colnames(n))){
> >>
> >> if(n[i,j]==n[j,i]){
> >>
> >> rownames(n)[[i]]->output} else {}
> >>
> >> }
> >>
> >> }
> >>
> >>> output
> >> NULL
> >>
> >> The right answer would have been "B", though.
> >
> > Can you explain why "A" would not be an equally good answer to satisfy
> > your problem set up?
> >
> > > which(n == t(n) & col(n) != row(n) , arr.ind=TRUE)
> > row col
> > B 2 1
> > A 1 2
> > > rownames(which(n == t(n) & col(n) != row(n) , arr.ind=TRUE) )
> > [1] "B" "A"
> >
> > # Which would seem to be the correct answer, but
> > # This adds an additional constraint and also insures no diagonal
> > elements
> >
> > > rownames(which(n == t(n) & col(n) != row(n) & lower.tri(n),
> > arr.ind=TRUE) )
> > [1] "B"
> >
>
> Wouldn't this do it too (dsince the diagonal is set to false by
> lower.tri)?:
>
> rownames(which(n == t(n) & lower.tri(n), arr.ind=TRUE))
>
> Berend
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/matrix-evaluation-using-if-function-tp3483188p3483785.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

        [[alternative HTML version deleted]]



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. Received on Thu 05 May 2011 - 06:25:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 05 May 2011 - 07:00:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive