[R] Language element manipulation

Prof Brian D Ripley ripley at stats.ox.ac.uk
Fri Jul 28 08:43:50 CEST 2000


On Thu, 27 Jul 2000, 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

Right. You need to use substitute to change ameas to its value.

Try an easier route. I think.

do.graph <- function (meas)
{
    fn <- paste("a", meas, ".dat", sep='')
    themeas <- read.table(fn, header=F)
    ameas <- paste("a", meas, sep='')
    names(themeas) <- c("xpos", ameas)
    meassum <- summary(themeas[themeas$xpos==1, ameas])
...

will do this for you via data-frame indexing.  But if it does not
(there are some subtle R/S differences about), themeas[ameas] works
where ameas is a character variable, in place of themeas$ameas.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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