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

______________________________________________
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.

Reply via email to