[R] Language element manipulation

Robert Gentleman rgentlem at stat.auckland.ac.nz
Tue Aug 1 00:24:23 CEST 2000


On Thu, Jul 27, 2000 at 09:43:31PM -0500, Stuart Luppescu wrote:
> I am very confused about this. I want to convert a string to a name so I can
> use it to extract an element of a data frame using `$'. Here is my
> (non-working) code:
> 
> do.graph <- function (meas)
>   {
>     fn <- paste("a", meas, ".dat", sep='')
>     themeas <- read.table(fn, header=F)
>     ameas <- as.name(paste("a", meas, sep=''))
>     names(themeas) <- c("xpos", paste("a", meas, sep=''))
>     meassum <- summary(themeas$ameas[themeas$xpos==1])
>    ...
>    }
> So, when this function is called with the argument ``infl'' the second column
> will be named ``ainfl''. In the last line I want to extract the column called
> ``ainfl'' from the data frame ``themeas''. But this is what happens:
> 
> > themeas$ameas
> NULL
> 
> Just grabbing at straws here:
> 
>  themeas$eval(ameas)
> Error: attempt to apply non-function
> 
> Can someone wise me up about this?

  $ is (the?) one operator that does not and cannot evaluate its
  second argument.
  x$a, is asking for a column of x with the name a,
  if you try to evaluate a, then you will capture any variable
  with that name in the environment that $ is called from. This is
  almost never what you want. 
  x[[a]], (for dataframes) does evaluate its argument and hence is
  the thing to use.
> ______________________________________________________________________
> Stuart Luppescu         -=-=-  University of Chicago
> ???? ????????????(EUC)  -=-=-  s-luppescu at uchicago.edu
> http://www.consortium-chicago.org/people/sl/sl.html
> PGP public key: www.consortium-chicago.org/people/sl/pubkey.asc
>        ICQ #21172047  AIM: psycho7070
> QOTD:
>         "She's about as smart as bait."
> >> Sent on 27-Jul-2000 at 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-- 
+-------------------------------------------------------------------------+
| Robert Gentleman              phone : (64-9) 3737-599 ext 3946          | 
| Senior Lecturer               fax :   (64-9) 3737-018                   |
| Department of Statistics      office : Room 206 Maths/Physics           |
| University of Auckland	email : rgentlem at stat.auckland.ac.nz      |
+-------------------------------------------------------------------------+
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list