[R] How to test if something doesn't exist

arun smartpink111 at yahoo.com
Wed May 22 20:43:44 CEST 2013



#Some cases: 

ifelse(blah!="xyz",paste0(blah,1),blah)
#[1] "x11" "x21" "x31" "x41" "x51"

if(sum(blah=="xyz")>0) blah else paste0(blah,1)
#[1] "x11" "x21" "x31" "x41" "x51"

if(any(blah=="xyz")) blah else as.numeric(factor(blah))
#[1] 1 2 3 4 5
A.K.






----- Original Message -----
From: Joseph Trubisz <jtrubisz at me.com>
To: arun <smartpink111 at yahoo.com>
Cc: 
Sent: Wednesday, May 22, 2013 2:37 PM
Subject: Re: [R] How to test if something doesn't exist

OK...got it...thanks....

Joe


On May 22, 2013, at 2:34 PM, arun <smartpink111 at yahoo.com> wrote:

> 
> any(blah=="xyz") #gives FALSE as mentioned in the preivous email.
> 
> 
> 
> 
> ----- Original Message -----
> From: Joseph Trubisz <jtrubisz at me.com>
> To: arun <smartpink111 at yahoo.com>
> Cc: 
> Sent: Wednesday, May 22, 2013 2:32 PM
> Subject: Re: [R] How to test if something doesn't exist
> 
> Problem is how do you test it?
> In the example, I KNOW it doesn't exist, and I don't want to do a block of code that follows if it does not.
> 
> Example:
> 
> idx<-which(blah[,1]=="xxx")
> if( idx>0) #doesn't work...what does?
> {
> ...
> }
> 
> 
> On May 22, 2013, at 2:29 PM, arun <smartpink111 at yahoo.com> wrote:
> 
>> Hi Joe,
>> Not sure about your expected result
>>   blah<- paste0("x",1:5)
>>   which(blah=="xyz")
>> #integer(0)
>> blah=="xyz"
>> #[1] FALSE FALSE FALSE FALSE FALSE
>>   any(blah=="xyz")
>> #[1] FALSE
>> 
>> 
>>   sum(blah=="xyz")
>> #[1] 0
>> sum(blah=="x1")
>> #[1] 1
>> A.K.
>> 
>> 
>> 
>> ----- Original Message -----
>> From: Joseph Trubisz <jtrubisz at me.com>
>> To: r-help at r-project.org
>> Cc: 
>> Sent: Wednesday, May 22, 2013 2:08 PM
>> Subject: [R] How to test if something doesn't exist
>> 
>> Couldn't exactly explain the subject, so here's  the example:
>> 
>> idx<-which(blah[,1]=="xyz")
>> idx
>> integer(0)
>> 
>> How do I test that idx has a valid value (namely, > 0)?
>> 
>> TiA,
>> Joe
>> 
>> ______________________________________________
>> 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.
>> 
> 




More information about the R-help mailing list