Calling Pre-built Functions in R | Beginning R Programming – Part 9

R is loaded with pre-built functions to help you carry out routine data science tasks. Whether it’s data manipulation, modeling, or doing calculations, there’s likely a package containing a collection of pre-built functions to help you implement a task. Learn how to call a function in R, and how to install and load an R package to make use of its pre-built functions. We’ll use some of these pre-build functions during the data science bootcamp

Most if not all routine data science tasks
have been packaged with pre-built functions in R
so we don’t need to rewrite these tasks from scratch
whether it’s data manipulation, modeling
or doing calculations on the data. There
is likely a package containing a
collection of pre-built functions to
help you implement a task
speeding up the process so you
can be a bit more productive
Whether you realize it or not
we have already used some pre-built
functions in R
So for example “read.CSV”
this is a pre-built function that takes
inputs such as the CSV file into a
directory and some other optional inputs
such as specifying whether the file does
or does not have header columns
So, for example, we could just give it the file
So here we’re calling the function and
giving it the required minimum
input or argument
and this pre-built function
which reads in the CSV file is applied
to the given input reads “file.CSV”
So when you want to apply the function on
to a given input or basically when you
want to use it you are calling that function
using this function “read.CSV”
allows us to read in the data in a
single command line saving us a bunch of
time so we don’t need to write the
program from scratch to locate the file
to open it to interpret and read it and
make it a data frame
so “read.CSV” is part of
R’s utils package which you’ll see here
and this comes with R and contains
a standard set of functions that are for
common tasks. “Base” is another R package
that also comes with R and offers a
standard set of functions
Now when you want to do
something outside the
standard functionality in R we can
install a package to help implement a
specific task
So, for example, our task might be
to model on some data using a
decision tree or machine learning algorithm
We can then use the “rpart” package
for this and it will give us a bunch
of functions designed to help us
carry out this specific task
So to do this we first need to install and
download the package and then load it
into R and then call the functions
within that package
So, for example, the first step
would be to install the package
Takes a moment to download
Okay great once it’s installed we will
load it into R
Okay great and now we
can start using this package the
functions within this package such as
the “rpart” function within the “rpart” package
so for example
gonna use it for modeling and gonna
call this function here
okay cool
So to see what kind of inputs or arguments
we need to give a function so
to know what is the required inputs we
could look up the documentation for this
now you can look up R documentation
for the package which lists all the
available functions that that package
offers so you might want to see
everything that’s included in base R
for example
or you can look up the function itself
either way the documentation
explains what the function does and the
kind of inputs you give it
it also includes some examples
of how to use the function in that package
So the documentation
can be accessed through a
simple command lines
and I’ll show you what I mean here
So we want to look up the package
and you either see this pop up on your
screen or in this bottom right panel in
Rstudio here under the “help” tab
we can see all the available packages
sorry, all available functions within this
package if we just click on the “rpart”
function here we can see the kind
of inputs that are needed for this function
you can look up the documentation
for a particular function
rather than the whole package if you
like so it’s a similar command
just give it the name of the function
and it goes straight to the documentation
on this function
now you’re ready to make the
most of R’s pre-built functions which
you’ll be using a lot to help carry out
many different data science tasks
In the next video I’ll introduce you to
control statements and why they’re useful

Download the Data Set
How to Install R

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

Part 8: 
Understanding Factors in R

Full Series:
Beginning R Programming

More Data Science Material:
[Video] Natural Language Processing in Minutes
[Blog] Building Up Our Data Scientists: From Learner to Full License
[Blog] Unleash the Potential of Recommender Systems


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>