puzzles with assign()

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

puzzles with assign()

David.Epstein
I'm trying to get code along the following lines to work:
temp.name <- paste(TimePt,'df',sep='.') # invent a relevant name/symbol as a character string.
assign(temp.name,IGF.df[IGF.df$TPt==TimePt,]) # this works. The relevant variable is now a data frame
lm(b ~ Strain+BWt+PWt+PanPix, data=temp.name)) # this gives an error, namely
Error in eval(predvars, data, env) : invalid 'envir' argument

I think it's obvious what I want to achieve, but how is it done? I tried
data=as.name(temp.name)
but that also didn't work. I can't find anything relevant in "Introduction to R".

Here is a secondary question:
While trying to understand what assign() does, I looked up help(assign) and found the example
a <- 1:4
assign("a[1]", 2)
a[1] == 2          #FALSE
get("a[1]") == 2   #TRUE
Could someone explain this puzzling example, or point me to an explanation of environments and how to operate with them?

Thanks

Reply | Threaded
Open this post in threaded view
|

Re: puzzles with assign()

Duncan Murdoch-2
David.Epstein wrote:

> I'm trying to get code along the following lines to work:
> temp.name <- paste(TimePt,'df',sep='.') # invent a relevant name/symbol as a
> character string.
> assign(temp.name,IGF.df[IGF.df$TPt==TimePt,]) # this works. The relevant
> variable is now a data frame
> lm(b ~ Strain+BWt+PWt+PanPix, data=temp.name)) # this gives an error, namely
> Error in eval(predvars, data, env) : invalid 'envir' argument
>
> I think it's obvious what I want to achieve, but how is it done? I tried
> data=as.name(temp.name)
> but that also didn't work. I can't find anything relevant in "Introduction
> to R".
>  

That's because constructing names like this is generally a bad idea.  
But you can do it; you use get() to get the object whose name is in
temp.name.  So put data=get(temp.name) into your lm() call.

> Here is a secondary question:
> While trying to understand what assign() does, I looked up help(assign) and
> found the example
> a <- 1:4
> assign("a[1]", 2)
>  

This creates an object with name "a[1]".  That's not usually a legal
name, but assign() can still create a variable with that name.
> a[1] == 2          #FALSE
>  

This tries to find the 1st element of an object named a.
> get("a[1]") == 2   #TRUE
>  

This gets the object with the weird name.

> Could someone explain this puzzling example, or point me to an explanation
> of environments and how to operate with them?
>  

See the R Language Definition.  There's a section on environments, and
mention of them in a number of other places.

Duncan Murdoch
> Thanks
>
>
>

______________________________________________
[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: puzzles with assign()

David.Epstein
Duncan Murdoch-2 wrote
  ...<snip>...
That's because constructing names like this is generally a bad idea.  
But you can do it; you use get() to get the object whose name is in
temp.name.  So put data=get(temp.name) into your lm() call.
I had another useful answer (sent privately) from Patrick Burns. His "R inferno" seems like a helpful source of information. Here's a quote from page 12 that has been very helpful in simplifying my code and making it more comprehensible. The key seems to be to index a list by character strings.
Patrick Burns wrote
A fairly common question from new users is: “How do I assign names to a
group of similar objects?” Yes, you can do that, but you probably don’t want
to—better is to vectorize your thinking. Put all of the similar objects into one
list. Subsequent analysis and manipulation is then going to be much smoother.
Thanks to both.
David