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,

Heitor.

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,

Heitor.

Creating functions in Rstudio is no different from creating them in R.

Here is a guide to doing it in R.

https://swcarpentry.github.io/r-novice-inflammation/02-func-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 : [https://www.sangakoo.com/en/unit/functions-defined-by-parts#:~:text=A%20function%20defined%20by%20parts,value%20of%20the%20independent%20variable.]

R has an ifelse() function.

https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/ifelse

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:

```
#https://www.sangakoo.com/en/unit/functions-defined-by-parts#:~:text=A%20function%20defined%20by%20parts,value%20of%20the%20independent%20variable.%5D
f <- function(x){
dplyr::case_when(
x <= -3 ~ -x-1,
x>-1 & x<1 ~3,
x >=1 ~ x-2,
TRUE ~ NA_real_ # all other cases
)
}
f(-4)
#> [1] 3
f(-2)
#> [1] NA
f(.5)
#> [1] 3
f(1)
#> [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.