Compare three or more values?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Compare three or more values?

Beale, Holly (NIH/NHGRI) [F]
Is there a less cryptic way to compare three or more values?

allTheSame<-c("red","red","red","red")
notAllTheSame<-c(132,132,132,999)

all.identical <- function(vectorToTest){
    cIdentical=sum(vectorToTest %in% vectorToTest[1])
    return(cIdentical==length(vectorToTest))
    }

all.identical(allTheSame)
all.identical(notAllTheSame)

Thanks in advance,
Holly

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Compare three or more values?

Erik Iverson-3
Holly,

try

 > length(unique(x)) == 1

where x is your vector of interest. But think about
how you want NA values to be treated, and also think about
R FAQ 7.31 if dealing with floating point numbers.

--Erik


Beale, Holly (NIH/NHGRI) [F] wrote:

> Is there a less cryptic way to compare three or more values?
>
> allTheSame<-c("red","red","red","red")
> notAllTheSame<-c(132,132,132,999)
>
> all.identical <- function(vectorToTest){
>     cIdentical=sum(vectorToTest %in% vectorToTest[1])
>     return(cIdentical==length(vectorToTest))
>     }
>
> all.identical(allTheSame)
> all.identical(notAllTheSame)
>
> Thanks in advance,
> Holly
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Compare three or more values?

Marc Schwartz-3
In reply to this post by Beale, Holly (NIH/NHGRI) [F]
On Mar 23, 2011, at 11:01 AM, Beale, Holly (NIH/NHGRI) [F] wrote:

> Is there a less cryptic way to compare three or more values?
>
> allTheSame<-c("red","red","red","red")
> notAllTheSame<-c(132,132,132,999)
>
> all.identical <- function(vectorToTest){
>    cIdentical=sum(vectorToTest %in% vectorToTest[1])
>    return(cIdentical==length(vectorToTest))
>    }
>
> all.identical(allTheSame)
> all.identical(notAllTheSame)
>
> Thanks in advance,
> Holly


See ?unique

allTheSame <- c("red","red","red","red")
notAllTheSame <- c(132,132,132,999)

> length(unique(allTheSame)) == 1
[1] TRUE
 
> length(unique(notAllTheSame)) == 1
[1] FALSE


Note that this is fine for character and integer values, but should not be expected to work for floats.

In the latter case, see ?all.equal and R FAQ 7.31: Why doesn't R think these numbers are equal?

HTH,

Marc Schwartz

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Compare three or more values?

Beale, Holly (NIH/NHGRI) [F]
In reply to this post by Erik Iverson-3
Erik:

(head smack) Of course! Thank you.

On 3/23/11 2:47 PM, "Erik Iverson" <[hidden email]> wrote:

>Holly,
>
>try
>
> > length(unique(x)) == 1
>
>where x is your vector of interest. But think about
>how you want NA values to be treated, and also think about
>R FAQ 7.31 if dealing with floating point numbers.
>
>--Erik
>
>
>Beale, Holly (NIH/NHGRI) [F] wrote:
>> Is there a less cryptic way to compare three or more values?
>>
>> allTheSame<-c("red","red","red","red")
>> notAllTheSame<-c(132,132,132,999)
>>
>> all.identical <- function(vectorToTest){
>>     cIdentical=sum(vectorToTest %in% vectorToTest[1])
>>     return(cIdentical==length(vectorToTest))
>>     }
>>
>> all.identical(allTheSame)
>> all.identical(notAllTheSame)
>>
>> Thanks in advance,
>> Holly
>>
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.