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$ameasNULL Just grabbing at straws here: themeas$eval(ameas) Error: attempt to apply non-function Can someone wise me up about this? ______________________________________________________________________ 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
It's hard to do with $. I think you need something like get('$')(themeas,ameas) It's easier to use subscripting if themeas is a dataframe: themeas[,ameas] Duncan Murdoch On Thu, 27 Jul 2000 21:43:31 -0500 (CDT), you 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? >______________________________________________________________________ >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 >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 > NULLRight. 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 28 Jul 2000, Prof Brian D Ripley wrote:> will do this for you via data-frame indexing. But if it does not > (there are some subtle R/S differences about), themeas[ameas] worksI meant to type themeas[[ameas]], sorry. -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._