Hi, I'm a new user trying to switch from SAS, so sorry for the beginner's question: Suppose I have a dataframe DF that contains variables X,Y,Z. I am trying to write a function like this: myplot <- function(varname){xyplot(varname ~ Y, group = Z, data = DF)}. The problem is then how to enter X into my function. If I write myplot("X") I get an error because the argument is a string and xyplot can make nothing out of it. If I write myplot(X) I also get an error that tells me the object X does not exist. Thanks for your help, Diego [[alternative HTML version deleted]]
You need to reference the data.frame or append it. myplot(DF$X) should work or append(DF) myplot(X) --- Diego Gruber <diego.gruber at gmail.com> wrote:> Hi, > > I'm a new user trying to switch from SAS, so sorry > for the beginner's > question: Suppose I have a dataframe DF that > contains variables X,Y,Z. I am > trying to write a function like this: > > myplot <- function(varname){xyplot(varname ~ Y, > group = Z, data = DF)}. > > The problem is then how to enter X into my function. > If I write myplot("X") > I get an error because the argument is a string and > xyplot can make nothing > out of it. If I write myplot(X) I also get an error > that tells me the object > X does not exist. > > Thanks for your help, > > Diego > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Thanks a lot for your suggestions. Referencing the dataset solved the problem. I had actually tried it before but since I was also subsetting the datset within the function (something I didn't mention in my question) the objects were not of the same length and that caused me trouble. Your comment made me realize that. Thanks Hadley for pointing me out to ggplot2, looks like a very complete graphics package and if it simplifies the programming it must be extremely useful. I'll make sure to take a closer look at it. Best regards, Diego [[alternative HTML version deleted]]