substituting an object not found

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

substituting an object not found

Mikkel Grum
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: substituting an object not found

Gábor Csárdi-2
> rm(list=ls())
> a <- 1
> ifelse(exists("b"), b, a)
[1] 1
> b <- 2
> ifelse(exists("b"), b, a)
[1] 2
>

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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

--
Csardi Gabor <[hidden email]>    MTA RMKI, ELTE TTK

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: substituting an object not found

Duncan Murdoch
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
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: substituting an object not found

Gabor Grothendieck
In reply to this post by Mikkel Grum
This does not answer your question directly but this
can relate to calling functions and also to inheritance
in object oriented systems.  The common thread in
1b and 2 is that if a variable is not found in the current
environment R will look into the parent environment so if we
give our variables the same name but put them in
different environments that have a parent/child relationship
then R will use the local variable if its present and the
variable of the same name from the parent if not.
There is also an example here (1a) that uses function args
but that requires one to be explicit.

1a. function args

f <- function(a = b) a
f(1) #1

b <- 2
f() # 2

1b. function free variables

f <- function() { a <- 10; a }
f()  # uses a in function f

a <- 20
g <- function() a
g()  # uses a from global environment

2. oo

In this example,
the parent is the global environment and the child is object
oo.

library(proto)
a <- 1
oo <- proto(f = function(.) .$a)
oo1f()  # 1   It has used a in global environment.

oo$a <- 2
oo$f()  # #2   It has used a in object oo.

On 1/27/06, Mikkel Grum <[hidden email]> 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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: substituting an object not found

Seth Falcon-2
In reply to this post by Mikkel Grum
On 27 Jan 2006, [hidden email] 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??

Along with exists(), you might find mget() useful since it allows you
to specify an ifnotfound value.

--
+ seth

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html