Plotting a function that includes logical operators

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Plotting a function that includes logical operators

dean123
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!
Reply | Threaded
Open this post in threaded view
|

Re: Plotting a function that includes logical operators

David Winsemius

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.
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Plotting a function that includes logical operators

dean123
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
Reply | Threaded
Open this post in threaded view
|

Re: Plotting a function that includes logical operators

Duncan Murdoch-2
On 27/07/2011 9:21 AM, dean123 wrote:
> 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?

The problem is that your function will be called with a vector of values
for t (by plot(), or some function plot() calls).  This is very common
in R, and if you want other functions to work well with yours, you
should handle it.

A quick and dirty workaround is to define test as you did, then call

test2 <- Vectorize(test)

The Vectorize() function creates a new function which we name test2 that
will call test() in a loop.  Usually you don't want to do this because
it's not very efficient compared to many other solutions, but it's easy
to do.

Duncan Murdoch

______________________________________________
[hidden email] 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.