[R] test whether all elements of a vector are identical

David Winsemius dwinsemius at comcast.net
Thu Dec 16 16:19:27 CET 2010


On Dec 16, 2010, at 9:56 AM, baptiste auguie wrote:

> Hi,
>
> The fastest way seems to be,
>
> all(x[1] == x)

I'm sure that is more efficient than this methods:

patt <- paste("^",x[1],"$", sep="")
length( grep(patt, x) == length(x) )

... but sometimes it helps to have strategies that might generalize to  
notions of "begins with" or "ends with" rather than "identical".

-- 
David.
>
>
> 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.
>>
>
> ______________________________________________
> 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



More information about the R-help mailing list