[R] writing function ( 'plot' and 'if') problem
=== myf=function(ds=1){ x=rnorm(10) y=rnorm(10) { #start of if if (ds==1) { list(x,y) } else (ds==2) { plot(x,y) } } # end of if } # end of function === Hi All, the problem i am having here is, that I want to be able to control the display, lf ds=1, i want to just have a list, but it seem to always plot... Thanks. casper -- View this message in context: http://n4.nabble.com/writing-function-plot-and-if-problem-tp1839091p1839091.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] writing function ( 'plot' and 'if') problem
Tena koe Caspar Try removing the (ds==2) - I'm not sure how you expect R to interpret that. HTH Peter Alspach -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- project.org] On Behalf Of casperyc Sent: Wednesday, 14 April 2010 10:42 a.m. To: r-help@r-project.org Subject: [R] writing function ( 'plot' and 'if') problem === myf=function(ds=1){ x=rnorm(10) y=rnorm(10) { #start of if if (ds==1) { list(x,y) } else (ds==2) { plot(x,y) } } # end of if } # end of function === Hi All, the problem i am having here is, that I want to be able to control the display, lf ds=1, i want to just have a list, but it seem to always plot... Thanks. casper -- View this message in context: http://n4.nabble.com/writing-function- plot-and-if-problem-tp1839091p1839091.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
Re: [R] writing function ( 'plot' and 'if') problem
what i am triyng to do is when ds=1 give me a list ds=2 plot Thanks casper -- View this message in context: http://n4.nabble.com/writing-function-plot-and-if-problem-tp1839091p1839125.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] writing function ( 'plot' and 'if') problem
On Apr 13, 2010, at 5:41 PM, casperyc wrote: === myf=function(ds=1){ x=rnorm(10) y=rnorm(10) { #start of if if (ds==1) { list(x,y) } else (ds==2) ?if That seems odd. I would expect this function to return TRUE or FAKSE rather than proceeding to a plot function. There is not usually a condition following the else{} step. -- David. { plot(x,y) } } # end of if } # end of function === Hi All, the problem i am having here is, that I want to be able to control the display, lf ds=1, i want to just have a list, but it seem to always plot... Thanks. casper -- View this message in context: http://n4.nabble.com/writing-function-plot-and-if-problem-tp1839091p1839091.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
Re: [R] writing function ( 'plot' and 'if') problem
On Apr 13, 2010, at 5:41 PM, casperyc wrote: === myf=function(ds=1){ x=rnorm(10) y=rnorm(10) { #start of if if (ds==1) { list(x,y) } else (ds==2) { plot(x,y) } } # end of if } # end of function === Hi All, the problem i am having here is, that I want to be able to control the display, lf ds=1, i want to just have a list,a Perhaps taking out the entirely extraneous condition for else and linking up the else clause: myf=function(ds=1){ x=rnorm(10) y=rnorm(10) #start of if if (ds==1) { list(x,y) } else { plot(x,y) } } # end of if myf(1) [[1]] [1] -0.5738623 0.6937577 -1.0801386 0.9465693 1.0054440 -1.5523713 1.5912727 0.5426036 1.8980418 -0.5922542 [[2]] [1] -1.4519795 -0.1556495 -2.5242081 -0.6671736 0.5947123 -0.6051199 -1.3711271 0.1647910 0.1616131 -0.2563056 -- David. but it seem to always plot... Thanks. casper -- View this message in context: http://n4.nabble.com/writing-function-plot-and-if-problem-tp1839091p1839091.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.
Re: [R] writing function ( 'plot' and 'if') problem
The spurious condition (ds==2) is actually interpreted as the action taken by the else statement. Then the braced plot statement is seen as the next statement after the conclusion of the if () {} else () statement. [[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.
Re: [R] writing function ( 'plot' and 'if') problem
On Apr 13, 2010, at 10:58 PM, RICHARD M. HEIBERGER wrote: The spurious condition (ds==2) is actually interpreted as the action taken by the else statement. Then the braced plot statement is seen as the next statement after the conclusion of the if () {} else () statement. I did take out the condition, and it still failed so I then took out the cr's. -- David. __ 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.