Question: Create an R function ` pwfun ( ) ` , which computes values of the piece - wise ( mathematical ) function $ text

Create an R function `pwfun()`, which computes values of the piece-wise (mathematical) function $\text{pwfun}$ defined in the following way:
$$\text{pwfun}(x)=\begin{cases}
-2 x -2 &,\hspace{1.1cm} x<-1\\
\hspace{.5cm}0 &,-1\leq x \leq 1\\
x^2-1 &,\hspace{0.3cm}1< x
\end{cases}$$
Its graph is as follows.
Your R function `pwfun()` should also accept `x` as a vector of arbitrary length and return vector of the same length with entries defined as values of the mathematical function $\text{pwfun}$, evaluated at the corresponding entries of vector `x`.
Here are some input vectors and what you should get as a corresponding output.
`pwfun(-1)`
`0`
`pwfun(c(-2,0.5,3))`
`208`
`x = c(-3,-2,-1,0,1,2,3.2)`
`pwfun(x)`
`4200039.24`
Hint:
- Obviously, one way to solve this is using `if-elseif-else` statment. Apart from that, you can also take a look at the R function `ifelse()`, which you could call twice.
- Be careful: because R has assignment operator `<-`, code lines `a <-1` and `a <-1` do **NOT** have the same meaning!
While `a <-1` checks whether variable `a` is less than -1,
`a <-1` means the same as `a <-1`, which is assigning the value 1 to the variable `a`.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!