[19117] in s-news-athena
[S] need help writing a function
daemon@ATHENA.MIT.EDU (Julian Taylor)
Sun Sep 5 21:39:43 1999
From: Julian Taylor <jtaylor@maths.adelaide.edu.au>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <14291.6781.464948.774956@staff.maths.adelaide.edu.au>
Date: Mon, 6 Sep 1999 11:05:57 +0930 (CST)
To: "Paul Schwarz" <Paul.Schwarz@cornell.edu>
Cc: <s-news@wubios.wustl.edu>
In-Reply-To: <001401bef743$d5c1a5e0$96a95f18@schwarz.twcny.rr.com>
Paul Schwarz writes:
>
> Hello,
>
> I suspect that there is a simple solution to this programming problem,
> but so far it eludes me. I am trying to write a function that allows me
> to plot variables from a dataframe without specifying the name of the
> data frame (yes, I know that I could attach the data frame but please
> bear with me). The difficulty appears to be related to working with the
> names of data objects versus the objects themselves. The first approach
> I tried was to preserve both the dataframe name and the variable name as
> components of the data object. E.g.,
>
> plotit <- function( y ) {
> df <- mydataframe
> boxplot( df$y )
> }
>
> This seems to fail because df$y get evaluated as mydataframe$y, and the
> substitution of y never happens.
>
> The second approach I tried was to paste the dataframe name and the
> variable name together into a single string. However, I was unable to
> figure out how to coerce this string, which represents the name of the
> object, into a reference to the actual object. For example:
>
> plotit <- function( y ) {
> df <- "mydataframe"
> yvar <- paste( df, deparse( substitute( y ) ), sep = "$" )
> boxplot( yvar )
> }
>
> This fails because yvar is a string and not a reference to an object.
>
Correct. So we make it a reference to an object and evaluate as
one. Change your assignment line to the following.
eval(parse(text = paste("df", deparse(substitute( y )), sep= "$")))
--
-----
Julian Taylor
Postgraduate Statistics
Adelaide University S.A.
email: jtaylor@maths.adelaide.edu.au
-----
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news