call object from character?

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

call object from character?

chuck.01
Let say I have an object (I hope my terminology is correct) a
a <- 12
> a
[1] 12

And "a" has been assigned the number 12, or whatever
And lets say I have a character "call_A"
call_A <- "a"
>call_A
[1] "a"

What is the function "F" that allows this to happen:
> F( call_A )
[1] 12
Reply | Threaded
Open this post in threaded view
|

Re: call object from character?

plangfelder
On Wed, Apr 18, 2012 at 7:25 PM, chuck.01 <[hidden email]> wrote:

> Let say I have an object (I hope my terminology is correct) a
> a <- 12
>> a
> [1] 12
>
> And "a" has been assigned the number 12, or whatever
> And lets say I have a character "call_A"
> call_A <- "a"
>>call_A
> [1] "a"
>
> What is the function "F" that allows this to happen:
>> F( call_A )
> [1] 12


Use get:

> a = 12
> get("a")
[1] 12

HTH,

Peter

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: call object from character?

Jorge I Velez
In reply to this post by chuck.01
Perhaps the following?

get(call_A)

See ?get for more details.

HTH,
Jorge.-

On Wed, Apr 18, 2012 at 10:25 PM, chuck.01 <> wrote:

> Let say I have an object (I hope my terminology is correct) a
> a <- 12
> > a
> [1] 12
>
> And "a" has been assigned the number 12, or whatever
> And lets say I have a character "call_A"
> call_A <- "a"
> >call_A
> [1] "a"
>
> What is the function "F" that allows this to happen:
> > F( call_A )
> [1] 12
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/call-object-from-character-tp4569686p4569686.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: call object from character?

chuck.01
In reply to this post by plangfelder
I figured as much.
Thank you greatly.


plangfelder wrote
On Wed, Apr 18, 2012 at 7:25 PM, chuck.01 <[hidden email]> wrote:
> Let say I have an object (I hope my terminology is correct) a
> a <- 12
>> a
> [1] 12
>
> And "a" has been assigned the number 12, or whatever
> And lets say I have a character "call_A"
> call_A <- "a"
>>call_A
> [1] "a"
>
> What is the function "F" that allows this to happen:
>> F( call_A )
> [1] 12


Use get:

> a = 12
> get("a")
[1] 12

HTH,

Peter

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: call object from character?

glsnow
In reply to this post by chuck.01
Almost always when people ask this question (it and its answer are FAQ
7.21) it is because they want to do things the wrong way (just don't
know there is a better way).

The better way is to put the variables that you want to access in this
way into a list, then you can easily access the objects in the list by
name (or position, or all of them, etc.):

mylist <- list(a=12)
call_A <- 'a'
mylist[[call_A]]

Adding more objects to the list is easier than creating new data
objects in the global environment, and if you want to do something
with all those objects (copy, delete, rename, etc.) then you have 1
list to work with rather than a bunch of separate objects.  If you
want to do the same operation on all the objects (the common follow-up
question) then if they are in a list you can use lapply, sapply, or
vapply and it is much simpler than looping and getting.

On Wed, Apr 18, 2012 at 8:25 PM, chuck.01 <[hidden email]> wrote:

> Let say I have an object (I hope my terminology is correct) a
> a <- 12
>> a
> [1] 12
>
> And "a" has been assigned the number 12, or whatever
> And lets say I have a character "call_A"
> call_A <- "a"
>>call_A
> [1] "a"
>
> What is the function "F" that allows this to happen:
>> F( call_A )
> [1] 12
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/call-object-from-character-tp4569686p4569686.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.