[R] compare a vector and a row of a matrix

Ista Zahn izahn at psych.rochester.edu
Sun Sep 26 18:21:13 CEST 2010


Hi,
You can use all.equal, like this:


all.equal(c(1,1), mtrx[1,], check.attributes=FALSE)

If you want to check each row of the matrix (I wasn't clear if you
wanted this) you can do something like


check.equal <- function(x, y)
{
  isTRUE(all.equal(y, x, check.attributes=FALSE))
}

apply(mtrx, 1, check.equal, y=c(1,1))

HTH,
Ista
On Sun, Sep 26, 2010 at 3:25 PM, xinxin xx <xxgreat at hotmail.com> wrote:
>
>
>
> From: xxgreat at hotmail.com
> To: r-help-bounces at r-project.org
> Subject: compare a vector and a row of a matrix
> Date: Sun, 26 Sep 2010 23:23:52 +0800
>
>
>
>
>
>
>
>
> Hi Everyone:
>
>   I am trying to compare a vector and rows of a matrix
>   for example
>  > xn <- c(1,2,4,4,5,5,5,6)
>  >yn <- c(1,2,5,7,1,2,3,1)
>  >mtrx <- cbind(xn, yn)
>  when I tried, say, > c (1,4), the result was TRUE, TRUE. I think the reason is that 1 is compared to xn and 4 is compared to yn seperately.
>  Could anyone tell me how I can get a single result of the comparson between a vector and a row of the matrix?
>  for example, c(1,1) is one row of the matrix but c(1,4) is not. I tried to write a loop but it seems long for this simple problem
>
> Thank you very much!!!
>
>        [[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.
>
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list