# Developing Functions in R: Part I

Functions serve as shortcuts when trying to complete task in R. Instead of having to re-type a series of activities every time you wanted to do them a function saves you the work by completing the task with a simple command. Developing functions is a little complicated and abstract for individuals who are not computer programmers but it is useful to know the basics .

Background

Let’s say you want to do the following in R

1. Round the number to two decimal places
2. Paste a percentage sign after the rounded number
3. Print the results

Sounds complicate. you cannot do all this in the console. Instead you need to use the source editor which is normally located in the upper left corner in RStudio. Below is the script for completing this action.

• ```x <- c(45.56454, 16.78343, 84.341221)
ToPercent <- round(x, digits = 2)
Output <- paste(ToPercent, "%", sep = "")
print(Output)```

After typing this into source editor you need to press ctrl+shift+enter to run it. Here is the output from the console in RStudio

```> source('~/.active-rstudio-document', echo=TRUE)

> x <- c(45.56454, 16.78343, 84.341221)

> ToPercent <- round(x, digits = 2)

> Output <- paste(ToPercent, "%", sep = "")

> print(Output)
 "45.56%" "16.78%" "84.34%"```

Here is what the script means

1. We assigned the variable ‘x’ the values 45.56454, 16.78343, 84.341221
2. We created the variable ‘ToPercent’ in this variable we use the ’round’ function for all the values in the variable ‘x’ and we tell are to round to 2 digits using the ‘digits’ argument.
3. In order to add the ‘%’ we create the variable ‘Output’ in this variable we used the ‘paste’ function to assign a ‘%’ to the results of the ‘ToPercent’ variable and we indicate that we do not want any separation between the ‘%’ and the numbers in ‘ToPercent’. We do this by adding the argument ‘sep’ and typing double quotes “” with no space in between them.

The problem with this script is that you are limited to whatever values you put in the ‘x’ variable. It would be much more convenient to not be limited to these values. You have to constantly change the script which is inefficient.

From Script to Function

In order to transform this script to a function we must make it flexible enough to allow any number into the script and we do this be providing a door into the script through making a few changes in the source editor. See the example below.

• ```MakePercent <- function(x) {
ToPercent <- round(x, digits = 2)
Output <- paste(ToPercent, "%", sep = "")
return(Output)
}```

Here what we did

1. We made the variable ‘MakePercent’. This variable is actually a function because we assigned the term ‘function’ to it and put the argument ‘x’ in parantheses. This turns the variable into a function.
2. After that, w used the ‘ { ‘ the front door to the function factory. Inside the ‘ { ‘ everything should look familiar because this information was in the previous example.
3. The only difference is that instead of using ‘print’ we used ‘return’ to display the results
4. At the end we put another ‘ } ‘  to close the function

We can no put any value in parentheses after the “MakePercent” function and get an answer. However, you can only put one value at a time into the function because there was only one ‘x’ in the “MakePercent” code. If you want to do several values at once you have to first save them as a variable and plug the variable into “MakePercent.

Below are two examples, the first has only one value going into ‘MakePercent’ while the second has a variable with several numbers going into ‘MakePercent’. You must source the script by typing ctrl+shift+enter to load the function into the workspace before doing this example.

```> MakePercent(23.6795)
 "23.68%"```
```> Example <- c(23.4543, 12.4534, 78.78565, 56.67093)
> MakePercent(Example)
 "23.45%" "12.45%" "78.79%" "56.67%"```

This concludes the discussion on functions. There is much to learn later