[R] Referring to an object by a variable containing its name: 6 failures

Duncan Murdoch murdoch.duncan at gmail.com
Sat Sep 17 21:04:06 CEST 2011


On 17/09/2011 2:15 PM, Joshua Wiley wrote:
> Hi Andrew,
>
> On Sat, Sep 17, 2011 at 12:04 AM, andrewH<ahoerner at rprogress.org>  wrote:
> >  Dear Folks --
> >  The anonymous poster ("rmailbox") is perfectly correct.  I had forgotten you
> >  could use names in this way.  When referring to rows or columns by name
> >  rather than by number, I usually use either attach() or the $ operator,
> >  neither of which works here. If anyone understands why data.df[,colName]
> >  works in this setting but datadf$colName and the use of as.symbol(colName)
> >  after attach(data.df) do not work, i would love an explanation, because I
> >  sure don't.
>
> Perhaps this will be a roughly correct rule of thumb to help: `$`
> assumes it is being passed a variable name,

I'd call it an element name, or component name...

>   `[` (and ilk) assume they
> are passed a quoted name or a variable containing a variable name.

I'd be more general:  an expression evaluating to a character vector of 
names.  (Or a logical or integer vector, of course.)

> Six example:
>
> d<- list(a = 1:10)
> myname<- "a"
>
> d$a
> d$"a"
> d$myname
> d[[a]]
> d[["a"]]
> d[[myname]]

or d[[letters[1]]], for an example of a more general expression.

Duncan Murdoch

> note that each does not work in one case but which case is different.
>
>
> >
> >  Thanks, Timothy, for helping to clarify what I was trying to do. You are
> >  exactly right, and your analogy to the $$ command in PHP – a command that
> >  works -- was thereby more perfect than my analogy to things in R which do
> >  not work.
> >
> >  Elk's suggestion to use the get() function was very welcome, as I had never
> >  really understood what get() was for, and this is a great use that often
> >  arises. However, for this purpose, get() is somewhat capricious in its
> >  effectiveness. “get(colName)” works as the operand of class(), length(),
> >  mode(), and summary(), but it does not work for typeof(), where it returns
> >  this error:
> >  "Error in eval(substitute(expr), data, enclos = parent.frame()) :  numeric
> >  'envir' arg not of length one"
> >  And it does not work for str(), where it treats the variable name as a
> >  character string rather than a symbol.
>
> I do not understand your question here---get() seems to work as I
> would expect.  That said I would caution you (nay, beat you over the
> head with countless logs until my limbs lacked strength to continue)
> to avoid using attach() for things like datasets, at least until you
> are comfortable enough with R and its lexical scoping that you no
> longer see any use to using it and wonder why you ever did.  attach()
> makes copies of data and adds it to the search path, and I would not
> be surprised if some of your surprises were related to confusions
> about why things worked or did not work.  I.e., typing:
>
> a
>
> should not work in my example above, but had I done:
>
> attach(d)
>
> then:
>
> a
>
> would work, but
>
> d[["a"]]
>
> would not be referring to the same thing as:
>
> a
>
> attach() also sets users up to think that they can go around typing
> the name of variables contained in lists, data.frames or some other
> structure and it will just work.  To complicate matters, you are
> trying to use all these things in a function, which has its own
> environment.  Anyway, here are some examples with get() (although I
> find myself using get() about as often as I find myself using attach):
>
> ## look at the search path
> search()
>
> ## rather than attach() I will use with:
>
> with(d, get(a))
> with(d, get("a"))
> with(d, get(myname))
>
> ## before you mention all the retyping:
> with(d, {
>    x<- a + 5
>    x2<- rnorm(10) ^ x
>    mean(data.frame(a, x, x2))
> })
>
> The only reason I had to retype with() was that I was expecting some
> of the calls to produce errors and that halts execution.  In the other
> with() (which I argue is an appropriate alternative to attach() to
> save typing), note that x and x2 are not assigned to the global
> environment, when the function is finished executing and it is
> discarded, they are discarded with it----good for temporary
> calculations.
>
> >
> >  Again, I do nut understand what distinguishes the functions for which Elk's
> >  solution works from those for which it does not. Does anybody know? Ideas
> >  welcome.
>
> Happy to give more ideas if you give the examples that work/do not work.
>
> >
> >  --and thanks again for all the help.
>
> Cheers!
>
> Josh
>
> >
> >  andrewH
> >
> >
> >  --
> >  View this message in context: http://r.789695.n4.nabble.com/Referring-to-an-object-by-a-variable-containing-its-name-6-failures-tp3817129p3819813.html
> >  Sent from the R help mailing list archive at Nabble.com.
> >
> >  ______________________________________________
> >  R-help at r-project.org 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.
> >
>
>
>



More information about the R-help mailing list