How to Write a Function in R: Beginning R Programming – Part 11

Functions allow us to reuse code, saving us from having to re-write the same code again and again. Learn how to write a function in R starting with the main components of a function and then creating a simple function in R.

A function is reusable code where we
want to apply the same code
to different inputs
This saves you from having to
write the same code again and again
where you only need to change the inputs
but the functionality or the code
is the same
we discussed using pre-built
functions in R but have not yet
discussed creating your own custom
function in R if you’re required to for
your own particular need
in our video on controlled statements
we used the “for” “if” and “else” statements
to tag our data as ‘high’ or ‘low-med’
income and store those
tags in a new column vector so that we
can add this to our income data frame
but what if in future we are working
with different data frames and we create
new columns that we would like to add to
those data frames
we will be working
with many different data sets so it’s
likely we will want to apply the same
functionality onto any data frame and
column vector
So what we need to do is
create a function for this
So here’s an example of
what that function would look like
most functions require a name
of some sort
and they also require some kind of
inputs, some kind of processing
of those inputs, and usually return some
kind of output after having processed
the inputs
This is the basic makeup of
most functions
So we first give our function
a name and call…so we can use
this to call the function later like we
did in the pre-built functions video
we called the
“read.CSV” function by its name “read.CSV”
and then gave it the required inputs
In our function here we have called it
‘add.vect.df’ and we will use it’s name
later when we’re ready to call the function
or use it
We also specify that
it’s a function that we’re creating here
with the given inputs of a data frame
and a vector
which can be named anything by
the user but ‘df’ and ‘vect’ take the
place if the users data frame name and
the vector name
We call the functions
when calling the functions the
user of our function will need to first
input the name of their data frame and
then the name of their column vector
core functionality is that we use R
‘cbind’ here to basically add the vector
onto a data frame and tie it to variable
called ‘new.df’
and then we return ‘new.df’
or the resulting data frame after
having added a new column
onto the data frame
So now if we call this function after
running our function created above
so we’ll run this
and we call it
So we use the name to call it
and give it the required inputs so our
data frame is income and our column
vector is ‘income.level’
we run this
you’ll see it’s done
what it’s supposed to do
it’s added this
column onto our data frame
So now using this function we
can give it any required inputs so any
data frame and any new column vector we
wish to add so we don’t have to rewrite
this code or hard code this each time we
want to you know apply this function to
a different data frame and vector
And that completes
our introduction to R series
you’re now ready to hit the ground
running the first day of the bootcamp
but remember any new skill
requires some practice
so revise these learning exercises
if you need and we look forward
to building up your data science skills

Data Set
How to Install R

Part 10: 
Control Statements in R: For Loop, If, and Else

Start at the Beginning: 
“Hello World” Program

Full Series:
Beginning R Programming

More Data Science Material:
[Video] How to do the Titanic Kaggle competition in R
[Video] Web Scraping in R Part 1: Writing your Script
[Video Series] Introduction to dplyr
[Video Series] Introduction to text analytics in r


Rebecca Merrett
About The Author
- Rebecca holds a bachelor’s degree of information and media from the University of Technology Sydney and a post graduate diploma in mathematics and statistics from the University of Southern Queensland. She has a background in technical writing for games dev and has written for tech publications.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>