# Create a vector function from a scalar function in R

## Create a vector function from a scalar function in R

You would have to Vectorize the function `f`

to get it to apply over a vector:

```
f = Vectorize(f)
print(f(bins))
```

Note that you could have also just used `curve`

with `sapply`

:

```
curve(sapply(x, f), from=-5, to=5)
```

Finally, if you wrote the function with `ifelse`

like so:

```
f = function(x) {
ifelse(x >= -5 & x <= -3, 3*(1-(x+4)^2)/8, ifelse(x>=3 & x<=5, 3*(1-(x-4)^2)/8, 0))
}
```

That would allow it to work on vectors without needing `Vectorize`

.

It would be a lot faster to write the function so it were entirely vectorized:

```
f <- function(x){ 0 + (x >=-5 & x <= -3)*(3*(1-(x+4)^2)/8) +
+ (x >= 3 & x <= 5)*( 3*(1-(x-4)^2)/8) }
```

And from the symmetry further simplification is possible (I think):

```
f <- function(x){ 0 + ( abs(x) <= 5 & abs(x) >= 3)*( 3*(1-(abs(x) -4)^2)/8) }
```