[R] Plotting a function that includes logical operators
David Winsemius
dwinsemius at comcast.net
Wed Jul 27 13:56:38 CEST 2011
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
More information about the R-help
mailing list