Skip to content
Back to formatted view

Raw Message

Message-ID: <43137C86.8000902@pburns.seanet.com>
Date: 2005-08-29T21:22:14Z
From: Patrick Burns
Subject: Testing if all elements are equal in a vector/matrix
In-Reply-To: <200508291535.21230.vincent.goulet@act.ulaval.ca>

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
>  
>