Hi All, There are several replies to the question below, but I think there must exist a better way of doing so. I just want to check whether all the elements of a vector are same. My vector has one million elements and it is highly likely that there are distinct elements in the first few itself. For example:
> x = c(1,2,rep(1,100000)) I want the answer as FALSE, which is clear from the first two observations itself and we don't need to check for the rest. Does anybody know the most efficient way of doing this? Regards Utkarsh From: Francisco J. Zagmutt <gerifalte28_at_hotmail.com <mailto:gerifalte28_at_hotmail.com?Subject=Re:%20%5BR%5D%20Testing%20if%20all%20elements%20are%20equal%20in%20a%20vector/matrix>> Date: Tue 30 Aug 2005 - 06:05:20 EST 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" <[email protected]> / />To: <[email protected]>, <[email protected]> / />Subject: Re: [R] Testing if all elements are equal in a vector/matrix / />Date: Mon, 29 Aug 2005 15:49:20 -0400 / /> / >See ?identical <http://tolstoy.newcastle.edu.au/R/help/05/08/11201.html#11202qlink1> /> / />-----Original Message----- / />From: [email protected] / />[mailto:[email protected]] On Behalf Of Vincent Goulet / />Sent: Monday, August 29, 2005 3:35 PM / />To: [email protected] / />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 <mailto:Vincent.Goulet_at_act.ulaval.ca?Subject=Re:%20%5BR%5D%20Testing%20if%20all%20elements%20are%20equal%20in%20a%20vector/matrix> http://vgoulet.act.ulaval.ca / /> / />______________________________________________ / />[email protected] mailing list / />https://stat.ethz.ch/mailman/listinfo/r-help / />PLEASE do read the posting guide! / />http://www.R-project.org/posting-guide.html / /> / />______________________________________________ / />[email protected] mailing list / />https://stat.ethz.ch/mailman/listinfo/r-help / />PLEASE do read the posting guide! / />http://www.R-project.org/posting-guide.html / [[alternative HTML version deleted]]
______________________________________________ [email protected] 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.

