Custom made functions

As you become better at coding, you probably want to start creating your own functions. You can even create functions, that use other functions. Think of it like a lego game, in which you have thousands of functions (legos) that you can use to create whatever you want. Just as the Legos game, creating functions is very simple.

The structure of an R function is as follow:

  1. You have the name you want for your function
  2. What arguments are needed for your function
  3. Between {} you place what you want to do with your arguments
  4. Outline what you want to output from your function

Let’s have an example. Say, I have two numbers (12.78454 and 1.34893439), which I want to first multiple, then divide the result by 2, then estimate the cosine, then multiple by pi, then estimate the ceiling; and I want to do this four times with different starting numbers. So lets start coding this,

a=12.78454              #I place  first number in a variable, so I do not have to retype all numbers
b=1.34893439            #same for second number
Multiple_AB=a*b         #first command I have to do. Create new varaible multiplying a and b
Divide= Multiple_AB/2   #second command is to divide the output by 2
Cosine= cos(Divide)     #third command is to estimate the cosine in the results
PI=Cosine*pi            #four command is to multiply result by pi
Cealing=ceiling(PI)     #finally, estimate the largest integer
Cealing                 #here is the result of that operation above
## [1] -2

If you see the code above, I wrote 8 lines of code to get the result for the first two set of numbers. If I want to do the same for say three other set of numbers, I will have to write 24 lines of code, not to mention the chance for error.

Since the general operation is same for all cases, you can simply create a function, which will reduce the lenght of the code, while ensuring you use exactly the same procedure for all numbers. Let’s do it,

CamilosFunction <- function(argument1, argument2) { #Argument 1 and 2, are the two numbers I have to run in my function
Multiple_AB=argument1*argument2         #first command I have to do
Divide= Multiple_AB/2                   #second command is to divide the output by 2
Cosine= cos(Divide)                     #third command is to estimate the cosine in the results
PI=Cosine*pi                            #four command is to multiply result by pi
Cealing=ceiling(PI)                     #finally, estimate the largest integer
return (Cealing)                        #here is the result of that operation above
}                 

If you see, all I did was to copy my entire process from the first set of values, and replace a and b, for the arguments in my function, add the {} and name my function. I just created a function, called CamilosFunction, that requires two values, to do the process indicated above, and return the result. Lets see,

a=12.78454
b=1.34893439
CamilosFunction (a, b) #here is my function, which run many commands and give me a single result.
## [1] -2

You can now reuse your function with different numbers, each time requiring only one line of code.

CamilosFunction (12.78454,10002.78454) 
## [1] -2
CamilosFunction (120.78454,102.78454) 
## [1] 3
CamilosFunction (912.74,10002.78454) 
## [1] -1

Create your own function now. In which you take three numbers (4,2,3), calculate their average, then multiple the result by 4, then estimate the sin, and finally calculate the square.