How can I create a function defined by parts in RStudio?

Hi everyone,

I want to know what I need to do to create a function defined by parts in RStudio, particularly the structure of the code. The data is not important at the moment, you can show me anything.

Thanks for all,


Creating functions in Rstudio is no different from creating them in R.
Here is a guide to doing it in R.

Thanks for the link, but i wanna do "function defined by parts".

I have no idea what that means, can you describe it or provide some example?

Of course..

Like this: f(x) = 0, if x > 1
x + 2, if x < 0

In this situation, is a exemple.
Here you can learn more about : [,value%20of%20the%20independent%20variable.]

R has an ifelse() function.

I'll try ifelse, but a i dont know if this code could be help me..
I need to show my results in a graph later..

For the example at the link which has 3 parts, I would do something like this:

f <- function(x){
    x <= -3 ~ -x-1,
    x>-1 & x<1 ~3,
    x >=1 ~ x-2,
    TRUE ~ NA_real_ # all other cases

#> [1] 3
#> [1] NA
#> [1] 3
#> [1] -1

Created on 2020-10-04 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.