[R] vectors with equal values

arun smartpink111 at yahoo.com
Thu Apr 18 16:08:21 CEST 2013


Hi,
Try:
 vec1<-c(1,1,1,1,1,1,1,1,1)
if(all(vec1==1)) "xxxxxxxxx" else  "yyyyyyyyyyy"
#[1] "xxxxxxxxx"
 vec2<-c(rep(1,4),2)
 if(all(vec2==1)) "xxxxxxxxx" else  "yyyyyyyyyyy"
#[1] "yyyyyyyyyyy"

#or
if(length(unique(vec1))==1) "xxxxxxxxx" else  "yyyyyyyyyyy"
#[1] "xxxxxxxxx"
  if(length(unique(vec2))==1) "xxxxxxxxx" else  "yyyyyyyyyyy"
#[1] "yyyyyyyyyyy"


A.K.

>Hi. 
>
>I need some help. I have a vector like c(1,1,1,1,1,1,1,1,1), for example. I wanto to do in R some like this: 
>
>if (all values equals) { 
>"xxxxxxxxxxxxxxxxxx" 
>} else { 
>"yyyyyyyyyy" 
>} 
>
>How can I write the condition "all values equal" in R? 
>
>Thank you



More information about the R-help mailing list