Introduction to R Programming Basics
Introduction to R Programming Basics
INTRODUCTION TO R
Structure
1.1 Introduction Listing and Removing R Objects
1.1 INTRODUCTION
This unit provides an introduction to the main features of R language. Also, we
do not assume any familiarity of the learner with the computer programming
while learning from this unit. The present unit sets the ground work for the
other units. It explains the procedures of downloading, installing and running
R. It also explains some of the important basic concepts related to R language
which are objects, classes of objects, case sensitivity of the language and so
forth. Most importantly, it explains how to find help on R constants, reserved
words, data sets and functions, which leads to the path of getting the answer
to your queries.
Many books on R programming language assumes that you are familiar with R
fundamentals, such as syntax, functions, operators, data types and so on. The
speciality of MST-015 (Introduction to R Software) course is that, it does not
require prior knowledge of any computing software. The R programming
language is discussed here from the scratch.
Note that, R is a free (open source) interpreted language. It is specially
designed for handling statistical computations and for graphical representation
of data. It also provides interface to other languages and debugging facilities. 9
*Dr. Taruna Kumari, School of Sciences, Indira Gandhi National Open University, New Delhi
Fundamentals of R Language
Nowadays R is used by enormous people daily to perform data analysis. R
has now become a tough competitor to almost all the commercial statistical
software’s.
10
Introduction to R
From the screenshot you can observe that several important information’s
related to Download, R Project, R Foundation, Help with R, FAQs
(Frequently asked questions), R Manuals and others are available on this
Website.
To download R, click on CRAN (Comprehensive R Archive Network) (under
Download), then you will be directed to a list consisting of CRAN mirror site
organized by country. You need to select a site near to you.
In some of the books, you may find to download R directly from the following
link, which direct you to the download R by selecting CRAN mirror site as
Austria.
[Link]
After selecting the CRAN Mirrors, you will be directed to the following
downloading page:
11
Fundamentals of R Language
On the CRAN page you will find some precompiled binary distributions of the
base system and contributed packages for Linux, macOS and Windows
operating systems. Choose one of the suitable options from the available
options under “Download and Install R” to download R. Here, we are
explaining the method of downloading R for Windows, as all the commands
written in this course are executed (or evaluated) on Windows.
12
Introduction to R
This page gets updated time to time and you will always find the latest version
of R to download on your system (right now R-4.3.1 is the latest version of R,
which is available to download).
After downloading R (see the location where downloads are saved), run the
setup program on PC by double-clicking on the downloaded application .exe
file (see the screenshot of the downloaded application with opened properties
shown below). Then follow the instructions and wait to get it installed
successfully (click on Finish to complete the installation process).
Note: Alternatively, you can get the set-up from your friends or known persons
and run on your PC to install it.
In case, if you are installing R for the macOS, then click on “Download R for
macOS” under “Download and Install R”. Then click on the .pkg file for the
latest version of R, download it and install it by double-clicking on the .pkg file.
Or otherwise, if you are installing R for the Linux, then click on “Download R
for Linux” under “Download and Install R”. The major Linux distributions like
Debian, Redhat, Ubuntu etc have packages for installing R. You just need to
use the system’s package manager to download and install the package.
Note: To download R, you can also type on Google “Download R” and get all
the important links, which helps you to download R on your system.
SAQ 1
What is the basic difference between an interpreted language and a compiled
language. Also, give an example of each one of them.
Save to save the file (save it as you save any other file, like word and excel).
The created file will be saved with .R extension. It can be later accessed as
follows:
Click on File → click on Open script → go to the location (where it is saved)
and select the required file to open.
Furthermore, a number of commands written on the R script editor can be
evaluated by firstly selecting them and then pressing Ctrl+R, which means
pressing control key with R. Or otherwise, if you want to run only a single
command then you can put the cursor at that R command (which you want to
evaluate), then press Ctrl+R. The R script editor is mainly useful when you
want to save retyping, and these files are easily manageable.
Note: The icon on desktop will be visible with its version. If you have not opted
for the creation of icon on desktop, then you can go to Programs and then to
R and thereafter find R icon and double-click on it to run R. Furthermore, it is
always better to visit the CRAN page to get latest version of R.
Note that in a Window system users interact with R through R console. When
we double-click on the R icon, the following page will appear:
As the paragraph also suggests the method to get the citation details on any
used package. We now try to get citation details for the base package using
citations() function by supplying base in double quotes (as character
string) in the following manner:
Other functions which are shown in the paragraph written on R console page
are demo(), q(), help() and [Link](). The demo() function is a
user-friendly interface for running some demonstrated R scripts, thus as the
name suggest it is for demonstration purpose. For more clarification, you can
run the following command:
#Getting demonstration on graphics package
> demo("graphics")
Quitting R:
We can quit R, by writing the command q() at the prompt. As you press enter
you will be asked if you want to save the current workspace or not (you can 15
Fundamentals of R Language
respond yes, no or cancel). If you want to resume your current work later at
the point you are leaving it then you can select yes otherwise no. You can
also cancel the quitting request by selecting cancel option.
Note: An alternative way to interact with R is using RStudio, which can be
downloaded from the following link:
[Link]
The RStudio can be downloaded and installed for all the operating system for
which R software is downloadable. Like R software, it also supports a script
editor where we can write complex programs. But for this course, we
recommend the use of the R Software.
SAQ 2
Write a command to get the citation details on the lattice package.
#Printing A
> print(A)
[1] 10
#Printing a
> print(a)
Error in print(a) : object 'a' not found
Hence, it is verified that upper- and lower-case letters are different in R, i.e., R
is a case sensitive language. Consider another example in which we assign a
character string "OM" to a variable named name and print it by combining
upper and lower characters of the variable name as follows:
#Assigning a character string
> name <- "OM"
#Printing name
> print(name)
[1] "OM"
16
Introduction to R
> print(NaMe)
Error in print(NaMe) : object 'NaMe' not found
Hence, name, nAME and NaMe are not same due to the case sensitivity of the
language.
Help on R:
Recall that in the written paragraph on the R console it was mentioned that get
“ help() for online help, or [Link]() for an HTML browser interface to
help”. Actually, R has a built-in help facility, which can be easily accessed
using the help() function or by using ‘ ? ’ operator. For the illustration
purpose suppose that we are interested in finding help on a function named
prod(), then it can be achieved either by using the help(prod) command
or by writing ?prod as follows:
#Seeking help
> help(prod)
> ?prod #An alternative
Note: To get help using ‘ ? ’ write the name of the function without parenthesis
‘ ( ) ’ after ‘ ? ’.
When the help() function command is executed, the R Documentation page
consisting of the details on the function and its arguments together with the
examples and other necessary details will pop-up as follows:
Hence, from the help page we get that the prod() function is available in the
base package and it is used to compute the product of all the elements
present in its arguments.
Next, we seek help on reserved word (maybe referred as keywords), R
constants and data sets using help() function as follows:
• When you want to take help on a built-in data set, say USArrests data
set, then it can be done by writing the following command: 17
Fundamentals of R Language
18
Introduction to R
SAQ 3
Write a command to get help on the if reserved word (used in conditional
statements).
Note: (i) The print() function is used to print an R object and it is discussed
in detail in the Unit 4 of MST-015 course.
(ii) If an expression is evaluated in R, say x+y (which is 15), then until unless
its value is assigned to some variable the value will be lost. So, if you want to
reuse any value further, better to assign it to some variable.
(iii) The two assignment operators, ‘ <- ’ and ‘ = ’ are used interchangeably. In
this course we will use ‘ <- ’ assignment operator for the assignment purpose.
As ‘ <- ’ operator is quite convenient and preferred by many book writers,
therefore, from this point onwards, we use this operator for variable
assignment purpose.
(iv) The ‘ <- ’ assignment operator consists of two characters ‘ < ’ (less than)
and ‘ - ’ (minus), occurring strictly side-by-side. It should be remembered that
there should not be any blank space in-between both the characters.
(v) In some reference books you may find ‘ = ’ as assignment operator. But do
not confuse between ‘ = ’ and ‘ == ’ operators. The first one is the assignment
operator and the second one is the relational operator.
It is important to note that in the beginning of the output [1] is written, it can be
read as “first value of the first line of the output”. It is generally useful when we
have vectors of several elements, which you will observe in coming units and
lab sessions.
Furthermore, the statement (IGNOU headquarter pin code) written after ‘ # ’ is
not executed. Similarly, ‘ # ’ can be used before any R command as follows:
#Preventing execution of a assignment command using ‘ # ’
> #x ->1
> x
Error: object 'x' not found
There may be situations when we would like to remove some specific or may
be all objects used in R workspace. This can be achieved using the rm()
function. Note that, all the objects from the work space can be removed using
the following command:
#To remove all the objects available to use in the workspace.
rm(list = ls()) #Not executed
Hence, using the rm() function the x and y objects are now removed
successfully and only z object is left.
Alternatively, we can also use the R menu bar to install a package. To do so,
we use the following path
Go to menu bar → click on Pakages → click on [Link](s) → double-
click to select a CRAN mirror for use in this session (A place close to your place)
→ double click on the package which you want to install.
The number of packages installed in your R software can be viewed using the
[Link]() function. We can also see the available packages
from the menu bar of the RGui as follows:
Go to menu bar → click on Packages → click on Load packages → Select a
package which is to be loaded from the list.
Note: (i) Any data set or function available in a specific package can also be
accessed using the double colon ‘ :: ’ operator. For example the ships data
set available in the MASS package can be accessed as MASS::ships.
(ii) The currently loaded package in your session can be accessed using the
search() function. 23
Fundamentals of R Language
Moreover, we can remove a installed package from the library (where packages
are stored) using the [Link]() function, which is available in the
utils package.
Note: The mode() function give information about the mode of an object in
the sense of 1Becker et al. (1988). It is compatible with other implementations
of the S language. The [Link]() function (same as typeof()) is
much useful as compared to mode() function, as it returns the storage mode
of its arguments in the sense of Becker et al. (1988).
1.5.9 R Manuals
There are several manuals available on R language written by R core team,
which can be accessed from the menu bar of the R software as follows:
Go to menu bar → click on Help → click on Manuals (in PDF)
1 Richard A. Becker, John M. Chambers and Allan R. Wilks (1988), The New S Language.
24 Chapman & Hall, New York. This book is often called the “Blue Book”.
Introduction to R
So, the first manual is “An Introduction to R”, which will give you an
introduction to the R language, its objects, data types, function and other
important information. Each manual consists of some important aspects of the
R language, which can be accessed according to the requirement of the
learner.
The Manuals on R can also be accessed from the CRAN page using the
following link:
[Link]
Note: In addition to manuals, the menu bar and CRAN page can also be
accessed to read the “FAQ on R” and “FAQ on Windows” (since I am
working on Window operating system). Here, FAQ stands for Frequently
Asked Questions. Note that, R has the following three collection of answers to
FAQ, which can be access using the following link:
[Link]
25
Fundamentals of R Language
SAQ 4
Suppose that when you run the ls() function command, you get the following
objects in your working environment.
> ls()
[1] "A" "data" "Name" "x" "xy"
Write a command to remove the data and Name objects:
Note: Learners are advised to visit and read the CRAN page carefully to get R
history and other important details.
1.6 SUMMARY
The main points discussed in this unit are as follows:
• We have discussed the method of downloading, installing, running and
quitting R.
• Methods of taking help on R reserved words, functions, data sets and R
constants are discussed.
• Case sensitivity of the language and way of accessing the contributions
of R core team is discussed in this unit.
• Other important aspects such as assignment operators, way of writing a
comment, editing a written command, R packages and so forth, are also
discussed in this unit.
Points to remember when working on R console:
• The enter key is used to run or evaluate a typed command (written after
prompt ‘ > ’) on R console.
• Semicolon (‘ ; ’) is the command separator.
26
Introduction to R
1.8 SOLUTIONS/ANSWERS
Self-Assessment Questions (SAQs)
1. The main difference between the Interpreted languages and Compiled
languages is that, interpreted language converts the commands (source
code) into machine code line by line. So, it means a single can be run in
an interpreted language but in compiled language you need to write
entire program first, then the entire source code (as a program) will be
run in a single command (By source code we mean set of commands
written in any programming language).
The C programming language is an example of compiled language and
R is an example of an interpreted language.
27
Fundamentals of R Language
2. The citation details on the lattice package can be obtained using the
following command:
citation(lattice)
3. Help on the reserved word if can be obtain using the following
command:
help("if")
4. We can use the rm() function to remove the objects named data and
Name as follows:
rm(data, Name)
Terminal Questions (TQs)
1. (i) FALSE
(ii) FALSE
(iii) FALSE
(iv) TRUE
2. (i) R internal type or storage mode
(ii) [Link]()
(iii) #
3. Refer Subsection 1.5.9.
4. Refer Section 1.3.
5. To load a package named pack, we use the library() and
require() functions as follows:
library(pack) or require(pack)
28