Re: [R] substituting an object not found

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Sat 28 Jan 2006 - 00:03:09 EST

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@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
>

R-help@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 Received on Sat Jan 28 00:11:36 2006

This archive was generated by hypermail 2.1.8 : Sat 28 Jan 2006 - 02:19:12 EST