[R] Oddity: I seem to have a variable in a dataframe that doesn't show in colnames() - can anyone advise?

chrishold at psyctc.org chrishold at psyctc.org
Sun May 29 21:56:44 CEST 2011


I thought it might have been that but stupidly didn't search on "$" and
I can now see that the one partial match I tried would have been
ambiguous so "$" hadn't resolved it.  Patrick Burns tells me I could
have found this in the wonderful "R inferno" and I'm sure I could have,
probably have, read that in other things, perhaps things I read way back.

What a reminder that you/we ... OK, _I_ ... can use R for some 13 years
or so now but still not known or forget crucial things like that.

This seems to me a good example of the sort of thing that R uses that
can be useful but can also be a trap for part-timer.  I keep getting
tripped up by R moving things to factors and me not realising that, so
now I've opted to go for options(stringsAsFactors=FALSE) so I can force
myself to retain explicit control of that.  I also repeatedly stumble on
aspects of date handling in R.

Every time I stumble in this sort of way I can see the reasons why R is
designed as it is and the power and efficiency in it and my respect for
the core team grows.  However, it can still make the learning curve
steep & hard.  Thanks to r-help for providing the free tour guide up
Everest (or into the inferno)!

Specific thanks to you both for pointing this one out and apologies if
this is just wasted bandwidth.

Chris

Phil Spector sent the following  at 29/05/2011 16:06:
> Chris -
>     If you check the documentation for the "$" operator, for example by
> typing
> 
> help("$")
> 
> you'll find (among a lot of other information):
> 
>     name: A literal character string or a name (possibly backtick
>           quoted).  For extraction, this is normally (see under
>           ¡Environments¢) partially matched to the ¡names¢ of the
>           object.
> 
> So when you use the "$" operator (but not "[" or "[["), partial
> matching is performed.  For example:
> 
>> x = data.frame(PHQ9=1:10)
>> x$PHQ
>  [1]  1  2  3  4  5  6  7  8  9 10
>> x[,'PHQ']
> Error in `[.data.frame`(x, , "PHQ") : undefined columns selected
>> x[['PHQ']]
> NULL
> 
> So if you don't want this "feature", you can use brackets instead
> of the dollar sign for extraction.
> 
>                     - Phil Spector
>                      Statistical Computing Facility
>                      Department of Statistics
>                      UC Berkeley
>                      spector at stat.berkeley.edu


-- 
Chris Evans <chris at psyctc.org> Skype: chris-psyctc
Consultant Psychiatrist in Psychotherapy, Notts. PDD network;
Professor, Psychotherapy, Nottingham University
*If I am writing from one of those roles, it will be clear. Otherwise*
*my views are my own and not representative of those institutions    *
If you have difficulty Emailing me on this address or getting a reply,
send again but cc to:       chris dot evans at nottshc dot nhs dot uk
and to:                     c dot evans at nottingham dot ac dot uk



More information about the R-help mailing list