R
Programming
A Step-by-Step Guide for Absolute Beginners
Daniel Bell
Please note the information contained within this document is for educational and entertainment purposes only. No warranties of any kind are expressed or implied. Readers acknowledge that the author is not engaging in the rendering of legal, financial, medical or professional advice. Please consult a licensed professional before attempting any techniques outlined in this book.
By reading this document, the reader agrees that under no circumstances is the author responsible for any losses, direct or indirect, which are incurred as a result of the use of the information contained within this document, including, but not limited to, errors, omissions, or inaccuracies.
All rights reserved. No part of this book may be used or reproduced in any manner whatsoever without written permission except in the case of brief quotations em- bodied in critical articles or reviews.
Under no circumstances will any legal responsibility or blame be held against the publisher for any reparation, damages, or monetary loss due to the information herein, either directly or indirectly.
.
ISBN: 9781696769648
Imprint: Independently published
Publisher: KDP Amazon Publishing
Second Edition: May 2020
First Edition: October 2019
Book and Cover design by Angela W.T
Copyright 2020 Daniel Bell
All rights reserved.
Acknowledgments
Foremost, I would like to express my sincere gratitude to my family, my wife Genevieve and my son Adan for the continuous support in my everyday life, for their patience, motivation, enthusiasm. Besides my family, I would like to thank my friends and colleagues: Prof. Jule Villepreux, Hugo D. and Dr. James Rivera, for their encouragement, insightful comments, and hard questions. I thank my fellow lab mates: Gang Yu, Ting Fan, Djibrilla Diallo, Juan Sanchez, for the stimulating discussions, for the sleepless nights we were working together before deadlines, and for all the fun we have had. Last but not least, I would like to thank my parents Guy and Ezra, for giving birth to me at the first place and supporting me spiritually throughout my life.
CONTENTS
Today, the need for statistical, data analysis and scientific research skills is on the rise. Organizations are generating huge volumes of data on a daily basis. This data is very rich with information and if processed well, an organization can enjoy a competitive advantage over its competitors. The data has hidden trends, patterns, and relationships that can help the organization make sound decisions as far as operations are concerned. Through statistics, organizations can calculate various measures from their data. This is good for making sound conclusions about the performance of the organization. Scientific research is good for making new discoveries. R is a computer programming language that comes with features good for use in performing such tasks.
The R programming language is well-known for its simple syntax, making it the best language for anyone who is a beginner in data analysis, statistical computing, and scientific research. Other than the syntax, there are many R packages that can be used to perform a wide variety of tasks. One only needs to install the package and begin to use it. R is also a great tool for anyone who needs to implement machine learning algorithms programmatically. The language comes with a number of packages that can be used for data visualization. This book is an excellent guide for you to learn R and know how to use it to perform tasks related to data analysis, statistical computing, and scientific research.
1 R Basics
What is R?
R is both a programming language and an environment used in data analytics, statistical computing, and scientific research. It is amongst the best programming languages for researchers, data analysts, statisticians, and marketers. They use R to retrieve, clean, analyze, visualize and then present their data. Some of the reasons behind the increasing popularity in the use of R are an easy to use interface and more expressive syntax.
R is interpreted rather than being compiled. This means that we have the R interpreter. R is available for free under the GNU General Public License, and its pre-compiled binaries are available for various operating systems including Windows, Linux, and Mac OS. R was developed by Ross Ihaka and Robert Gentleman at the University of Auckland in New Zealand. The name R was derived from the first name of the two developers of the language, Ross and Robert.
R has been extensively used in performing data analysis tasks. This is because it is good for computing the various statistical measures from both small and huge sets of data. The language has numerous libraries that can be used for data processing. Other than that, R is a good tool for data visualization. It makes it easy for us to represent data using graphs and charts. This makes it easy for one to derive patterns, trends, and relationships from datasets.
R Environment Setup
Before you can begin to write and run your R programs, you need to install the R. We will guide you on how to install R in both Windows and Linux.
Installation on Windows
First, you need to download R and store it in one of your local directories. The R software for Windows can be downloaded from the following URL:
https://cran.r-project.org/bin/windows/base/
The download will come in an executable form (.exe), so you just have to double click it to launch the installation. During the installation, you can choose the default settings so that the process may be easy for you. For those using Windows 32-bit, only the 32-bit version will be installed, but for those using the 64-bit version, both the 32-bit and 64-bit versions will be installed.
Once the installation is completed, launch R from the Start button and then All Programs. You will be presented with the R console as shown below:
.
It is now possible for you to run R commands on the console.
Installation on Linux
Begin by downloading the right version of R for your Linux system. The installation steps vary from distribution to distribution, but you can find these in the provided links. However, for those who need to have R faster, use the yum command and install R as shown below:
$ yum install R
With the above command, the R will be installed together with its standard packages. You can launch its console as shown belo w :
$ R
Installing R packages
Once you have R installed on your system, you may need to perform tasks using special packages. This is the case when you are performing data analysis tasks. Suppose you need to visualize your data. You need to install packages that are specialized in data visualization. The installation can be done by running the following command on the R console:
install.packages ( "package_name" )
For example, to install a package named plotrix, we can run the following command:
install.packages ( "plotrix" )
The package will then be installed and integrated within your R environment.
There are two ways through which you can write and run your r programs:
Let us discuss them:
R Command Prompt
After R has been installed on your system, it is easy for you to start its command prompt from All Programs o n windows. For Linux users, run the R command on the Linux terminal. Once the r command prompt > is launched, you can begin to type your program. For example, you can invoke the print() function to print the Hello World text on the prompt as shown below: