I would avoid eval(substitute(...)) in the function. It makes the function hard to use in more general cases (and hard to understand and subject to giving incorrect answers in some cases).
Instead "quote" (or 'quote') the literal string that you pass into the function. When you pass blue into the function you are passing the name of a dataset called blue, but when you pass in "blue" (or 'blue') you are passing in an unnamed dataset consisting of the word blue. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of Dennis Murphy > Sent: Saturday, March 12, 2011 1:48 AM > To: Tyler Rinker > Cc: [email protected] > Subject: Re: [R] Passing a character argument onto a function > > Hi: > > One answer is eval(substitute(...)): [Note: it's a good idea > NOT to use the > same name on different objects - R is pretty good about > distinguishing TEST > the function from TEST the data frame, but you could be > playing with fire if > both objects have the same class but have different contents. > This is why I > renamed your function.] > > Tfun <- function(DV,IV,group1) { > g1 <- eval(substitute(DV[IV == g], list(g = group1))) > p <- mean(g1) > list(g1,p) > } > > with(TEST, Tfun(frequency, color, 'blue')) > [[1]] > [1] 3 4 3 > > [[2]] > [1] 3.333333 > > HTH, > Dennis > > > On Fri, Mar 11, 2011 at 7:49 PM, Tyler Rinker > <[email protected]>wrote: > > > > > I am a new R user and am beginning to employ function creation in my > > statistical work. I am running into a problem when I want > to pass on a > > character (text) to the function as an argument. I have a > simple example > > below to demonstrate this problem. I cannot seem to find a > fix in my R book > > or in the blog posts. I'm sure this has been covered > before but my newbie > > status means I lack the R vocabulary to even search for > this problem (I've > > tried for a few days to no avail). Someone has already > attempted to explain > > this to me. I learn best by seeing. Could someone rewrite > my code so that > > the function works. The function is very simple as is the > data set so it > > should be pretty easy for an experienced R user to correct. > The problem is > > that R doesn't transfer the "blue" subgroup from the argument to the > > function. I am excited with the potential of R and look > forward to your > > help. > > > > I am a Windows user running R 2.12.2 > > > > > CODE for TEST FUNCTION > > > TEST<-function(DV,IV,group1) { > > + g1<-DV[IV=="group1"] > > + p<-mean(g1) > > + list(g1,p) > > + } > > > > > > > > R's OUTPUT > > > TEST(frequency,color,blue) > > [[1]] > > integer(0) > > [[2]] > > [1] NaN > > > > > > The DATA FRAME > > TEST<-read.table("TEST.csv", header=TRUE, sep=",",na.strings="999") > > > attach(TEST) > > > > color frequency > > 1 blue 3 > > 2 blue 4 > > 3 blue 3 > > 4 green 5 > > 5 green 2 > > 6 green 4 > > 7 green 5 > > 8 green 1 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > [email protected] 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. > > > > [[alternative HTML version deleted]] > > ______________________________________________ > [email protected] 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. > ______________________________________________ [email protected] 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.

