[R] how to write a periodic function in R?
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Jul 25 17:43:07 CEST 2005
On 7/25/2005 11:22 AM, lma5 at ncsu.edu wrote:
> My question is how to write a periodic function in R.
> for example if there is a function f(x) that
> f(x)=f(x+4), for -2<x<2, f(x)=x
> how to write it in R?
> thanks a lot!
Just write a function that does that, e.g.
f <- function(x) (x + 2) %% 4 - 2
The %% is the mod operator, so (x + 2) %% 4 adds 2 then maps all values
into the range 0 to 4. You'll need a different formula for a different
periodic function.
Use plot(f, from=-7, to=7) to see that this works.
Duncan Murdoch
More information about the R-help
mailing list