Overview of Lesson

In this lesson, we’ll work with using functions. We will then consider how to write your own simple functions. ### Using Functions

Functions are shortcuts that allow you to do things that would otherwise take multiple coding steps to complete. For instance, we have already worked through the function matrix() in lesson 1_2. Let’s go over the basics of functions.

#Say I want to use the function matrix.
#The function matrix is called upon by writing matrix().
#The function matrix takes different arguments to work. 
#To see these arguments, we can type ?matrix.
#We see under usage that matrix takes the forms

#matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE,
#       dimnames = NULL)

A<-matrix(data=c(1,2,3,4,5,6), nrow=2, ncol=3,byrow = TRUE) 
  #to data= we provide an optional data vector
  #nrow= the desired number of rows
  #ncol= the desired number of columns
  #byrow= is a logical, set to FALSE by default specifying the matrix is filled by column
  #dimnames= NULL or a list of length 2 giving the row and column names. A list of one is rownames.

Functions all take different argument, so you are responsible for fully understanding a function before you use it!

Writing your own simple functions

To write your own function we use the function() command. Functions always take the following form.

functionname <- function (argument1, argument2, argument3,...) {
  run all the code found here
  return(returnobject)
}

Most of the objects in that function are simply placeholder. For instance, functionname should be replaced by any name you want to call that function. As always, this can be anything.

More importantly, is the arguments. The only important thing is the order in which the function takes the arguments. Otherwise, you can also name them whatever you want. Note that you must refer to these arguments exactly the same throughout the function, but also that these “objects” exist only within the local function environment.

We then tell the function to run a series of lines of code by taking the arguments defined in the first part of the function. We tell the function to end and return command to the user by using the return() function. The best way to learn about writing functions is to learn to read them. An example below.

Full sample function

Let’s say I have two vectors. Say these vectors represent the number of accident in three neighborhoods, Beta, Gamma, Delta in city Alpha. Each vector contains the number of accidents over seven days. The mayor has asked you to provide a plot recording the highest number of accidents across neighborhoods in the city.

Here’s an example function that does just that.

#The data:
Var1<-round(rnorm(7,10,3))
Var2<-round(rnorm(7,10,3))
Var3<-round(rnorm(7,10,3))

myfunction <- function(vect1, vect2, vect3, mynames) { #this function takes 4 parameters: vect1, vect2, vect3 mynames

df <- data.frame(vect1, vect2, vect3)                  #take first two parameters and convert into a data frame, df

names(df) <- mynames                            #takes third parameter and use it as var names for dataframe

newvector <- apply(df, 1, max)                  #take df and the max value of each row to create new vec

df$newvector<-newvector

boxplot(df$newvector)                              #creates a boxplot of the highest values

}


myfunction(Var1, Var2, Var3, c('Beta', 'Gamma','Delta'))

In te next lesson we will look at creating and using dataframes.