Hi, Consider the following> x = c(1,2) > y = c(3,4) > d = data.frame(cbind(x,y)) > d$x[1] 1 2> d$"x"[1] 1 2> > foo = function(val)+ { + return(d$val) + }> > bar = function()+ { + return(d$"x") + }> > foo("x")NULL> bar()[1] 1 2 I'm a little surprised that R accepts both the form d$x and d$"x", but I'm mostly wondering why foo("x") doesn't work. Thanks, Faheem.
Try this foo1 = function(val) { return(d[val]) } foo1("x") Good luck miltinho astronauta brazil On 8/6/08, Faheem Mitha <faheem@email.unc.edu> wrote:> > > Hi, > > Consider the following > > x = c(1,2) >> y = c(3,4) >> d = data.frame(cbind(x,y)) >> d$x >> > [1] 1 2 > >> d$"x" >> > [1] 1 2 > >> >> foo = function(val) >> > + { > + return(d$val) > + } > >> >> bar = function() >> > + { > + return(d$"x") > + } > >> >> foo("x") >> > NULL > >> bar() >> > [1] 1 2 > > I'm a little surprised that R accepts both the form d$x and d$"x", but I'm > mostly wondering why foo("x") doesn't work. > Thanks, Faheem. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On Wed, 6 Aug 2008, Faheem Mitha wrote:> > Hi, > > Consider the following > >> x = c(1,2) >> y = c(3,4) >> d = data.frame(cbind(x,y)) >> d$x > [1] 1 2 >> d$"x" > [1] 1 2 >> >> foo = function(val) > + { > + return(d$val) > + } >> >> bar = function() > + { > + return(d$"x") > + } >> >> foo("x") > NULL >> bar() > [1] 1 2 > > I'm a little surprised that R accepts both the form d$x and d$"x", but I'm > mostly wondering why foo("x") doesn't work.It does work: it returns d$val as instructed. From the help Usage: x$name name: A literal character string or a name (possibly backtick quoted). or see the version in ?`$.data.frame`. So you are 'a little surprised' that the function works as described on the help page! Please explain to us how you can to that conclusion. I think you are looking for [[ (on the same help page as $).> Thanks, Faheem.-- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Wed, Aug 06, 2008 at 11:53:25AM -0400, Faheem Mitha wrote:> > Hi, > > Consider the following > >> x = c(1,2) >> y = c(3,4) >> d = data.frame(cbind(x,y)) >> d$x > [1] 1 2 >> d$"x" > [1] 1 2 >> >> foo = function(val) > + { > + return(d$val) > + } >> >> bar = function() > + { > + return(d$"x") > + } >> >> foo("x") > NULL >> bar() > [1] 1 2 > > I'm a little surprised that R accepts both the form d$x and d$"x", but > I'm mostly wondering why foo("x") doesn't work. > Thanks, Faheem.To get the behaviour you were expecting, the definition should have been foo <- function(val) d[[val]] Look at the indexing help page help("$") It says Usage: <...> x$name <...> name: A literal character string or a name... and then further down, in the section on indexing lists 'x$name' is equivalent to 'x[["name", exact = FALSE]]'. So, d$val looks for an element called 'val' and doesn't find one. d[["val"]] would do the same. However d[[val]] looks for an object called 'val', and then tries to use its value to index into d. Dan> > ______________________________________________ > 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.