Dear Sirs, I am a new user of the R package. When I try to use the curve function it confuses me.> curve(x^2)Works fine.> curve(x)Makes a complaint I don't understand. Why is x^2 valid and x is not? I check the documentation of curve, and it says the first argument must be an expression containing x.> expression(x)Is an expression containing x.> curve(expression(x))Makes a different complaint and mentions different lengths of x and y (but I use no y here). I understand that plotting the function y(x) = x is rather silly, but I want to know what I am doing wrong, for the sake of my understanding of how R works. Thank you for support. Abhilash B. [[alternative HTML version deleted]]
I think there is trouble because expr in curve(expr) may be the name of a function, and it's ambiguous whether 'x' should be interpreted as a mathematical expression involving x, or the name of a function. Here are some examples that work: curve(I(x)) curve(1*x) On Sun, 2011-06-05 at 12:07 -0500, Abhilash Balakrishnan wrote:> Dear Sirs, > > I am a new user of the R package. When I try to use the curve function it > confuses me. > > > curve(x^2) > Works fine. > > > curve(x) > Makes a complaint I don't understand. Why is x^2 valid and x is not? > > I check the documentation of curve, and it says the first argument must be > an expression containing x. > > > expression(x) > Is an expression containing x. > > > curve(expression(x)) > Makes a different complaint and mentions different lengths of x and y (but I > use no y here). > > I understand that plotting the function y(x) = x is rather silly, but I want > to know what I am doing wrong, for the sake of my understanding of how R > works. > > Thank you for support. > Abhilash B. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
On 11-06-05 1:07 PM, Abhilash Balakrishnan wrote:> Dear Sirs, > > I am a new user of the R package. When I try to use the curve function it > confuses me. > >> curve(x^2) > Works fine. > >> curve(x) > Makes a complaint I don't understand. Why is x^2 valid and x is not?curve() is a convenience function, and it tries to guess what you mean. Sometimes it gets it wrong. In the first case, it is clear you want to graph x^2. In the second it guesses you have a function named x and want to graph that. You don't, so it fails. Probably it could try again after the first failure, but I'd guess there will always be strange cases where it does weird things. Duncan Murdoch> > I check the documentation of curve, and it says the first argument must be > an expression containing x. > >> expression(x) > Is an expression containing x. > >> curve(expression(x)) > Makes a different complaint and mentions different lengths of x and y (but I > use no y here). > > I understand that plotting the function y(x) = x is rather silly, but I want > to know what I am doing wrong, for the sake of my understanding of how R > works. > > Thank you for support. > Abhilash B. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
I accidentally did not respond to the list. ---------- Forwarded message ---------- From: Abhilash Balakrishnan <balaabhil@gmail.com> Date: Sun, Jun 5, 2011 at 8:44 PM Subject: Re: [R] Question about curve function To: Matt Shotwell <matt@biostatmatt.com> Dear Mr. Shotwell, Thank you for the explanation. You seem to be right. In particular I tried the following:> x <- function(x) { x } > curve(x)Works! So, now x is interpreted as a function, and is plotted accordingly. I also found that this works too:> curve(I)With your explanation I now understand why it works. Thank you for support. Abhilash B. On Sun, Jun 5, 2011 at 1:11 PM, Matt Shotwell <matt@biostatmatt.com> wrote:> I think there is trouble because expr in curve(expr) may be the name of > a function, and it's ambiguous whether 'x' should be interpreted as a > mathematical expression involving x, or the name of a function. Here are > some examples that work: > > curve(I(x)) > curve(1*x) > > On Sun, 2011-06-05 at 12:07 -0500, Abhilash Balakrishnan wrote: > > Dear Sirs, > > > > I am a new user of the R package. When I try to use the curve function > it > > confuses me. > > > > > curve(x^2) > > Works fine. > > > > > curve(x) > > Makes a complaint I don't understand. Why is x^2 valid and x is not? > > > > I check the documentation of curve, and it says the first argument must > be > > an expression containing x. > > > > > expression(x) > > Is an expression containing x. > > > > > curve(expression(x)) > > Makes a different complaint and mentions different lengths of x and y > (but I > > use no y here). > > > > I understand that plotting the function y(x) = x is rather silly, but I > want > > to know what I am doing wrong, for the sake of my understanding of how R > > works. > > > > Thank you for support. > > Abhilash B. > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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 > > and provide commented, minimal, self-contained, reproducible code. > > >[[alternative HTML version deleted]]