Dear list, this might be an easy one, but I could figure out a solution (or how to google the right term). Is there any way to test whether all elements of a vector are identical? For numeric vectors I would use sum(diff(vector)==0)==0 but I have character vectors. Any Ideas? Cheers Jannis
How about: length(unique(vector)) >1 ? ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Thu, Dec 16, 2010 at 4:17 PM, Jannis <bt_jannis@yahoo.de> wrote:> identical[[alternative HTML version deleted]]
Thanks Tal. Sometimes easy solutions are close by but hard to find when one is focused on the wrong direction :-)! Jannis Tal Galili schrieb:> How about: > length(unique(vector)) >1 > > ? > > > > ----------------Contact > Details:------------------------------------------------------- > Contact me: Tal.Galili at gmail.com | 972-52-7275845 > Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | > www.r-statistics.com (English) > ---------------------------------------------------------------------------------------------- > > > > > On Thu, Dec 16, 2010 at 4:17 PM, Jannis <bt_jannis at yahoo.de> wrote: > > >> identical >> > >
David Winsemius
2010-Dec-16 14:50 UTC
[R] test whether all elements of a vector are identical
On Dec 16, 2010, at 9:17 AM, Jannis wrote:> Dear list, > > > this might be an easy one, but I could figure out a solution (or how > to google the right term). > > Is there any way to test whether all elements of a vector are > identical??identical> > For numeric vectors I would use > > sum(diff(vector)==0)==0 > > but I have character vectors. Any Ideas? > > Cheers > Jannis > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
baptiste auguie
2010-Dec-16 14:56 UTC
[R] test whether all elements of a vector are identical
Hi, The fastest way seems to be, all(x[1] == x) HTH, baptiste On 16 December 2010 15:17, Jannis <bt_jannis at yahoo.de> wrote:> Dear list, > > > this might be an easy one, but I could figure out a solution (or how to > google the right term). > > Is there any way to test whether all elements of a vector are identical? > > For numeric vectors I would use > > sum(diff(vector)==0)==0 > > but I have character vectors. Any Ideas? > > Cheers > Jannis > > ______________________________________________ > 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. >