Vincent Goulet
2005-Aug-29 19:35 UTC
[R] Testing if all elements are equal in a vector/matrix
Is there a canonical way to check if all elements of a vector or matrix are the same? Solutions below work, but look hackish to me.> x <- rep(1, 10) > all(x == x[1]) # == operator does not provide for small differences[1] TRUE> isTRUE(all.equal(x, rep(x[1], length(x)))) # ugly[1] TRUE Best, Vincent -- Vincent Goulet, Associate Professor ??cole d'actuariat Universit?? Laval, Qu??bec Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca
Doran, Harold
2005-Aug-29 19:49 UTC
[R] Testing if all elements are equal in a vector/matrix
See ?identical -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Vincent Goulet Sent: Monday, August 29, 2005 3:35 PM To: r-help at stat.math.ethz.ch Subject: [R] Testing if all elements are equal in a vector/matrix Is there a canonical way to check if all elements of a vector or matrix are the same? Solutions below work, but look hackish to me.> x <- rep(1, 10) > all(x == x[1]) # == operator does not provide for small differences[1] TRUE> isTRUE(all.equal(x, rep(x[1], length(x)))) # ugly[1] TRUE Best, Vincent -- Vincent Goulet, Associate Professor ??cole d'actuariat Universit?? Laval, Qu??bec Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Doran, Harold
2005-Aug-29 20:06 UTC
[R] Testing if all elements are equal in a vector/matrix
Yes, and I mistakenly thought this was comparing two objects, and that is not the case. -----Original Message----- From: Francisco J. Zagmutt [mailto:gerifalte28 at hotmail.com] Sent: Monday, August 29, 2005 4:05 PM To: Doran, Harold; vincent.goulet at act.ulaval.ca; r-help at stat.math.ethz.ch Subject: Re: [R] Testing if all elements are equal in a vector/matrix Hi Doran The documentation for isTRUE reads 'isTRUE(x)' is an abbreviation of 'identical(TRUE,x)' so actually Vincent's solutions is "cleaner" than using identical :) Cheers Francisco>From: "Doran, Harold" <HDoran at air.org> >To: <vincent.goulet at act.ulaval.ca>, <r-help at stat.math.ethz.ch> >Subject: Re: [R] Testing if all elements are equal in a vector/matrix >Date: Mon, 29 Aug 2005 15:49:20 -0400 > >See ?identical > >-----Original Message----- >From: r-help-bounces at stat.math.ethz.ch >[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Vincent Goulet >Sent: Monday, August 29, 2005 3:35 PM >To: r-help at stat.math.ethz.ch >Subject: [R] Testing if all elements are equal in a vector/matrix > > >Is there a canonical way to check if all elements of a vector or matrix >are the same? Solutions below work, but look hackish to me. > > > x <- rep(1, 10) > > all(x == x[1]) # == operator does not provide for small differences >[1] TRUE > > isTRUE(all.equal(x, rep(x[1], length(x)))) # ugly >[1] TRUE > >Best, > >Vincent >-- > Vincent Goulet, Associate Professor > ??cole d'actuariat > Universit?? Laval, Qu??bec > Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! >http://www.R-project.org/posting-guide.html
Patrick Burns
2005-Aug-29 21:22 UTC
[R] Testing if all elements are equal in a vector/matrix
How about diff(range(x)) < tolerance Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Vincent Goulet wrote:>Is there a canonical way to check if all elements of a vector or matrix are >the same? Solutions below work, but look hackish to me. > > > >>x <- rep(1, 10) >>all(x == x[1]) # == operator does not provide for small differences >> >> >[1] TRUE > > >>isTRUE(all.equal(x, rep(x[1], length(x)))) # ugly >> >> >[1] TRUE > >Best, > >Vincent > >
Whit Armstrong
2005-Aug-29 21:45 UTC
[R] Testing if all elements are equal in a vector/matrix
or perhaps length(unique(x))==1 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Patrick Burns Sent: Monday, August 29, 2005 5:22 PM To: vincent.goulet at act.ulaval.ca Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Testing if all elements are equal in a vector/matrix How about diff(range(x)) < tolerance Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") Vincent Goulet wrote:>Is there a canonical way to check if all elements of a vector or matrix>are the same? Solutions below work, but look hackish to me. > > > >>x <- rep(1, 10) >>all(x == x[1]) # == operator does not provide for small differences >> >> >[1] TRUE > > >>isTRUE(all.equal(x, rep(x[1], length(x)))) # ugly >> >> >[1] TRUE > >Best, > >Vincent > >______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Robin Hankin
2005-Aug-30 10:16 UTC
[R] Testing if all elements are equal in a vector/matrix
Hi library(magic) ?minmax [ the basic idea is min(x) == max(x) ] best wishes Robin On 29 Aug 2005, at 20:35, Vincent Goulet wrote:> > Is there a canonical way to check if all elements of a vector or > matrix are > the same? Solutions below work, but look hackish to me. > > >> x <- rep(1, 10) >> all(x == x[1]) # == operator does not provide for small differences >> > [1] TRUE > >> isTRUE(all.equal(x, rep(x[1], length(x)))) # ugly >> > [1] TRUE > > Best, > > Vincent > -- > Vincent Goulet, Associate Professor > ??cole d'actuariat > Universit?? Laval, Qu??bec > Vincent.Goulet at act.ulaval.ca http://vgoulet.act.ulaval.ca > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting- > guide.html >-- Robin Hankin Uncertainty Analyst National Oceanography Centre, Southampton European Way, Southampton SO14 3ZH, UK tel 023-8059-7743