Jenny McLaughlan
Jenny McLaughlan

Reputation: 21

R command to change function of x after a point?

I'm simulating some data in R, plotting x against Y (a rate) and I want x to be increasing linearly up to a point and then level off. That is, Y is a function of x between say, 0.1 and 5, but constant from 5.01 to 10. Is there a simple command which allows for varying x's? I'm sure my lecturer told me about one but I can't remember it... Any help or thoughts would be greatly appreciated!

Upvotes: 0

Views: 52

Answers (1)

NPE
NPE

Reputation: 500893

You could use ifelse:

> f <- function(x) ifelse(x < 5, x**2, 25)
> x <- seq(1, 10, .1)
> plot(x, f(x), type='l')

enter image description here

Upvotes: 3

Related Questions