I am trying to plot the following function over the range 0-100 test <- function(t){{ if (t<=10) x<-t*0 else x<-2*t } x } when I use plot(test,0,100) the GUI produces the following; "Warning message: In if (t <= 10) x <- t * 0 else x <- 2 * t : the condition has length > 1 and only the first element will be used" I am confused as when I evaluate the function for individual t it gives the correct response i.e. 0 for t<=10 and 2*t for t>10... This is what I wish the function to do however, I am not able to plot the function. Any help would be most appreciated! -- View this message in context: http://r.789695.n4.nabble.com/Plotting-a-function-that-includes-logical-operators-tp3697732p3697732.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2011-Jul-27 11:56 UTC
[R] Plotting a function that includes logical operators
On Jul 27, 2011, at 3:01 AM, dean123 wrote:> I am trying to plot the following function over the range 0-100 > > test <- function(t){{ > if (t<=10) > x<-t*0 > else x<-2*t > } > x > } >Two problems I see. The first is the one described fairly clearly by the error message: Read: ?"if" It is not designed to handle vectors. You most certainly should read: ?ifelse> when I use plot(test,0,100) the GUI produces the following; >The second is that you have created a function to which you have offered no arguments. Typing plot(test, 0, 100) is not passing any arguments to 'test'. It is giving test, 0, and 100 to 'plot' and since they are unnamed you are futher expecting R to associate test with x, 0 with y and 100 with whatever the third argument plot might be. (You should look it up.) ?plot You need to learn to form you commands as function_name(arg_name=arg_value, ...) and realize that function calls can also be arguments so once you have a working 'test' it might be successful to use this plot( seq(100), test(seq(100) ) See read more of Introduction to R and work all the examples. -- David.> "Warning message: > In if (t <= 10) x <- t * 0 else x <- 2 * t : > the condition has length > 1 and only the first element will be used" > > I am confused as when I evaluate the function for individual t it > gives the > correct response i.e. 0 for t<=10 and 2*t for t>10... This is what I > wish > the function to do however, I am not able to plot the function. > > Any help would be most appreciated! > > > -- > View this message in context: http://r.789695.n4.nabble.com/Plotting-a-function-that-includes-logical-operators-tp3697732p3697732.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
Hi David, I am trying to define t as a single numeric value and not as a vector.. I want the function to realise that if I call test(t) and t is less than equal 10 return zero, if t greater than 10 return 2*t. Am i missing some code in which to define t a numeric value instead of a vector? Also, I thought that by defining test <- function(t) {...} I was defining t as the argument of the function? Thanks, Dean -- View this message in context: http://r.789695.n4.nabble.com/Plotting-a-function-that-includes-logical-operators-tp3697732p3698464.html Sent from the R help mailing list archive at Nabble.com.
Maybe Matching Threads
- How to get an object name from C?
- [PATCH v2 0/3] mllib: Various fixes and changes to Getopt module.
- [PATCH v4 0/2] mllib: Various fixes and changes to Getopt module.
- [PATCH v3 0/3] mllib: Various fixes and changes to Getopt module.
- Proposal for an improved API for Sizer (and ToolBar)