[R] substituting an object not found

Duncan Murdoch murdoch at stats.uwo.ca
Fri Jan 27 14:03:09 CET 2006


On 1/27/2006 7:48 AM, Gabor Csardi wrote:
>> rm(list=ls())
>> a <- 1
>> ifelse(exists("b"), b, a)
> [1] 1
>> b <- 2
>> ifelse(exists("b"), b, a)
> [1] 2

That's not quite right.  ifelse() is meant for vectors of conditions; 
you really want just plain old "if" here:

   if (exists("b")) b else a

For example, with no b in the workspace:

 > a <- 1:10
 > ifelse(exists("b"), b, a)
[1] 1
 > if (exists("b")) b else a
  [1]  1  2  3  4  5  6  7  8  9 10

Duncan Murdoch
> 
> Gabor
> 
> On Fri, Jan 27, 2006 at 04:38:39AM -0800, Mikkel Grum wrote:
>> Is there any function in R like
>>
>> is.not.found(x, y)  
>>
>> meaning if you can't find object x, then use object
>> y??
>>
>>
>> Mikkel Grum
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list