Re: [R] Compare three or more values?

From: Marc Schwartz <marc_schwartz_at_me.com>
Date: Wed, 23 Mar 2011 13:48:10 -0500

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



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. Received on Wed 23 Mar 2011 - 18:53:32 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 Mar 2011 - 19:00:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive