0% found this document useful (0 votes)
4 views20 pages

Introduction to R Programming Basics

This document serves as an introduction to the R programming language, covering its installation, basic features, and essential commands. It emphasizes that prior programming knowledge is not required and outlines expected learning outcomes for users. Key topics include downloading R, case sensitivity, accessing help, and the use of R packages and functions.

Uploaded by

dilshadsaifi5486
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views20 pages

Introduction to R Programming Basics

This document serves as an introduction to the R programming language, covering its installation, basic features, and essential commands. It emphasizes that prior programming knowledge is not required and outlines expected learning outcomes for users. Key topics include downloading R, case sensitivity, accessing help, and the use of R packages and functions.

Uploaded by

dilshadsaifi5486
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT 1

INTRODUCTION TO R

Structure
1.1 Introduction Listing and Removing R Objects

Expected Learning Outcomes Recalling Previous Commands

1.2 Downloading and Installing R Listing, Installing and Removing


R Packages
1.3 Running and Quitting R
The typeof() and class()
1.4 Case Sensitivity of the
Functions
Language and Help on R
R Manuals
1.5 Some Other Important
Aspects of R Commands History

Assignment Operators 1.6 Summary


Writing Comments 1.7 Terminal Questions
Commands Separators 1.8 Solutions/Answers
Incomplete Commands

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.

We would recommend to study the course introduction pages to get aware


with the development of R language and its contributors, which has
tremendously benefited the world.

Expected Learning Outcomes


After completing this unit, you should be able to:
❖ download, install and run the R programming software;
❖ find help on the R functions and constants;
❖ understand R objects and classes;
❖ understand case sensitivity of the language;
❖ differentiate between complete and incomplete R commands;
❖ list and remove the R objects used in your R session; and
❖ access R manuals.

1.2 DOWNLOADING AND INSTALLING R


The most convenient way to download R in your system is to obtain base
distribution from the R Website, which is as follows:
[Link]
When you will go to the above link (assuming you have access to the internet)
it will take you to the following page:

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.

Then click on base under subdirectories to download R. The base R


distribution consists of many classical and modern statistical techniques. The
statistical techniques which are not supplied as part of base R (referred as
base R distribution) may be downloaded with the help of several packages
available. A number of packages that are supplied with base R are known as
“standard” and “recommended” packages. That is why in this course main
importance is given to these packages and in the MSTL-011 (Statistical
Computing Using R-I) course we tried our best to solve the problems using
these R base distribution packages.
So, when you click on base under subdirectories, you will be directed to the
following page. Then you can click on “Download R-4.3.1 for Windows” to
download R.

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.

1.3 RUNNING AND QUITTING R


To run the R software, you just need to double-click on the R icon on your
desktop (during installation you were asked whether to create an R icon on the
desktop or not), while in Unix versions you type R at the operating system
prompt. Both will bring up the R console with its prompt. In Window system the
prompt is ‘ > ’, and it may be different in other operating systems.
Note that any syntactically error free command written in front of prompt ‘ > ’ in
R console gets evaluated, when we press the ENTER key. R Graphical User
Interface (RGui) also supports a test editor, which can be access as follows:
Go to menu bar → click on File → click on New script.
When you click on the New script, a Untitled - R Editor will be opened which
is known as R script in which you can write the R commands that you want to
evaluate. After writing the commands you can save it (the cursor should be on
the R script while saving the file) by clicking on File and thereafter Save as or
13
Fundamentals of R Language

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:

The information written on the R Console pages is extremely important, as it


consists of information about its licence, contributors, getting citations details,
demonstrations, way of seeking help and quitting R. The license() or
license() functions are used to get details on R distribution licence details.
The details of the contributors can be accessed using the contributors()
function (we recommend accessing the contributors page to see the complete
information). For the illustration purpose, we now run this contributors()
function on R console as follows:
14
Introduction to R

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.

1.4 CASE SENSITIVITY OF THE LANGUAGE AND


HELP ON R
In this section, we shall discuss the case sensitivity of the R language and the
methods of taking help on any constant, reserved words, data sets and
functions.
Case Sensitivity:
R is a case sensitive language. By case sensitivity, we mean the ability to
distinguish between lower and upper version of letters. Due to the case
sensitivity, A and a, T and t are different letters. With the same argument, the
object/variable names GOOD and GoOD, Blessed, blessed and BLESSED are
different. For the illustration purpose, we now assign a value 10 to a variable A
and print its value using upper-case and lower-case letters as follows:
#Assigning a value 10 to A
> A <- 10

#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

#Printing nAME and NaMe


> print(nAME)
Error in print(nAME) : object 'nAME' not found

> 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

#Seeking help on a data set


> help(USArrests)

• If you want to take help on R constants, say pi, LETTERS, letters,


[Link] and others, then it can be done by supplying the name of the
R constants as an argument to the help function as follows:
#Seeking help on a R constant
> help(LETTERS)

• In case if you want to take help on any R operators or control flow


constructs such as +, -, [, ]], if, for and others. Then it can be done by
writing these operators, symbols and reserved words in double quotes as
follows:
#Seeking help on an operator
> help("[[") #In double quotes
Note: Even if you are not connected to the internet, then also you can access
R Documentation pages via help() function.
Next, we discuss the use of [Link]() function. This function is
particularly useful, when we do not know the exact name of the function and
only recall a subpart of the function or data set or keywords names. This
function only accepts a character string as its argument. As an alternative to
this, we can also use a more convenient way of finding help, which is using
‘ ?? ’ operator in front of the subpart of name of the function. For the
illustration purpose, suppose we want to seek help on the rowMeans()
function, but we can only recall a subpart rowMea of it, so we proceed to take
help in the following manner:
#Seeking help
> ??rowMea

#An alternative approach


> [Link]("rowMea") #In double quotes

When the [Link]() function command is executed, the search result


consisting of the details of the packages, which contains functions having
substring "rowMea" will be displayed. The same can be verified from the
following screenshot.

18
Introduction to R

SAQ 3
Write a command to get help on the if reserved word (used in conditional
statements).

1.5 SOME OTHER IMPORTANT ASPECTS OF R


In this section we shall discuss variables and objects, assignment operators,
incomplete commands, R packages, R manuals and other important aspects
of R.
In any computer programming language, variables provide the way to access
the data stored in memory. Data stored in the memory can not be directly
accessed, we need data structures to store and access the data. R provides a
number of data structures referred to as objects to assign (or save) and
access the stored data. R supports a number of objects namely, vectors,
matrices, arrays, lists, data frames, functions, expressions etc. In addition to
this the data type of an R object (for vectors, matrices and arrays) can be
"numeric", "integer", "character", "logical", "complex",
"factor". The vectors, matrices and arrays objects consist of data of a
single type, say either numeric or character or any other type. But in lists and
data frames, different types of data can be saved under one name. The
function and expression objects are used in Session 2 of MSTL-011 lab
course. The details on each one the objects are discussed in coming units.
Note: R also supports a special type of object called NULL, which is used to
indicate that the object is absent. The NULL object has no type and is different
from a vector or list of zero length.
Furthermore, any name assign the R objects or specific variables in R should
consist of only A–Z (capital letters), a-z (small letters) and 0-9 (digits), ‘ . ’ (a
dot), and ‘ _ ’ (underscore). The name of any object or variable should always
start with a letter or underscore. If it starts with underscore then try to use the
second character as a letter. There is no limitation on the length of the R
names. The file names can be any valid object name in R. Next, we discuss
other important aspects one-by-one.

1.5.1 Assignment Operator


Assignment operators are used to assign a value to a variable. The following
three different assignment operators are available in R:
<- Left assignment operator
= Leftwards assignment operator
-> Right assignment operator
You can use any of these assignment operators to assign the values or data
to any R objects and variables. For the illustration purpose, we now assign the
values 5, 10 and 15 to x, y and z variables using the all the three operators
and print them using the print() function as follows:
#Assigning the values 5, 10 and 15 to x, y and z
> x <- 5
19
Fundamentals of R Language
> y = 10
> 15 -> z

#Printing the assigned values


> print(x)
[1] 5
> print(y)
[1] 10
> print(z)
[1] 15

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.

1.5.2 Writing Comments


Comments in any programming language plays the following two important
roles.
1. It helps the user in explaining the R code to other people. Analogously, it
facilitates the programmer to make the R code more readable.
2. There may be situations in which the user would like to prevent the
execution of certain code parts or commands (Generally while testing
the code), that time as well comments play great role.
Comments in R starts with ‘ # ’ and can be put anywhere in the programme.
When any R code gets executed, it ignores the line or R commands which
starts with ‘ # ’ (hence prevents execution). For the illustration purpose, we
now create a variable named pincode to save the pin code of head office of
IGNOU, then we can use a comment in front of it to specify the location to
which it belongs as follows:
#Assigning pin code
> pincode <- 110068 #IGNOU headquarter pin code

#Printing pin code


> print(pincode)
20 [1] 110068
Introduction to R

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

We get an error message as the assignment command is not evaluated and


therefore x is not found.
Note: Throughout the MST-015 and MSTL-011 courses, the lines starting with
‘ # ’ written before the R commands are the unexecuted commands, written to
give clear understanding of the code part.

1.5.3 Commands Separator


Two or more commands in R programme can be separated by using the
following two ways:
1. Using the Enter key.
2. Using semi-colon ‘ ; ’ .
In the previous subsection, we have used the enter key to separate the R
commands. Now we show how semi-colon can be used as command separator.
See the following example.
#Assigning and printing the variables
> x <- 5; y = 10; 15 ->z
> print(x); print(y); print(z)
[1] 5
[1] 10
[1] 15

1.5.4 Incomplete Commands


If a R command written by the user is syntactically incomplete at the end of a
line, then by default R will gives ‘ + ’ prompt instead of ‘ > ’ prompt on second
and subsequent lines. This continues until the command is syntactically
completed by the user. This ‘ + ’ prompt can be skipped by pressing Esc key in
the keyboard.
Consider the following example, in which the written command is not
complete. The right parenthesis is missing. Until the parenthesis is added to
complete the command, we will continuously get ‘ + ’ prompt instead of ‘ > ’
prompt.
#Illustrating the incomplete command
> (4+6-1
+
+ )
[1] 9 21
Fundamentals of R Language

1.5.5 Listing and Removing R objects


The R objects, which are currently available in the R workspace can be listed
using ls() function. For example, after assigning the variables x, y and z
now we use this function to get the list of used objects as follows:

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

To remove specific objects, say objects named x and y, we supply their


names as arguments to the rm() function as follows:
#Removing x and y
> rm(x, y)

Next, we list the remaining objects (leftovers).


#Lising all object in the workspace
> ls()
[1] "z"

Hence, using the rm() function the x and y objects are now removed
successfully and only z object is left.

1.5.6 Recalling Previous Commands


We can recall and edit the R commands using the vertical forward, vertical
backward, horizontal right and horizontal left arrows as follows:
1. Vertical forward and backward keys (↑ and ↓) (also referred as up-arrow
and down-arrow keys) can be used to scroll forward and backward
through a command history to locate a particular command.
2. After locating the command, the horizontal right and left arrows (→ and
←) can be used to move the cursor within the command for editing
purpose.
It should be noted that command can be edited either by deleting characters
with DEL key and or adding other characters.

1.5.7 Listing, Installing and Removing R Packages


A number of packages are supplied as part of base R distribution. You do not
load them (or call them) while using any built-in functions or data sets
available in these packages. Generally, all R functions and datasets comes as
part of R packages. These functions and datasets will be available to use if
22 and only if a package is completely loaded. Sometimes the packages have
Introduction to R

dependencies, which means installation of specific packages need the


installation of the other dependent packages first. So, no specific commands
need to be given to download dependencies. They will be downloaded
automatically, when a specific package installation command is executed.
Next, we discuss the method of installing a new package in your R software. If
you are connected to the internet, then the package installation task can be
completed by using the [Link]() command. In the parenthesis
‘ ( ) ’ of this function, we should write the name of the package as character
string (in double quotes), which we are interested to install.
For the illustration purpose, we now explain the method of installing the MASS
package. To do so, we should write "MASS" as a function argument to
[Link]() function as follows:

#Installing a package named MASS


> [Link]("MASS")

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.

Alternatively, an already installed package can be loaded using the


library() or require() function. Now we illustrate the method of loading
a package name MASS (which is already installed) using these two functions:
#Loading the MASS package
> library(MASS)
Or,
# Loading the MASS package
> require(MASS)

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.

1.5.8 The typeof() and class() Functions


Type of a R object can be seen using the typeof() function. It determines
the storage type or R internal type of a R object. The class() function is
used to get the class of an R object. It is especially useful in object-oriented
style of programming. It is sometimes necessary to check the class of an
encountered object, just to check whether it can be used as an argument to a
particular function or not. Many functions accept a particular type of objects
only.
The typeof() and class() functions come as part of base package. To
have more clarity of these functions, let us see the class and type of different
types of objects discussed in upcoming units to have a bird eye view:

Object Class Type or Storage mode


numeric vector "numeric" "double"
integer vector "integer" "integer"
character vector "character" "character"
logical vector "logical" "logical"
Vary according to type of matrix
matrix
"matrix" elements. It can be
"array" "integer", "numeric",
"logical", "character"
list "list" "list"
data frame "[Link]" "list"
function "function" "closure"
expression "expression" "expression"

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

1.5.10 Commands History


The history() function available in the utils package can be used to get
commands history. It shows only last 25 commands from the command history
as the default value of its [Link] argument is 25. If you wish to see the all
the previous commands then you can assign it as Inf (reserved word for
infinity) as follows:
#Accessing history
> history([Link] = Inf)

Additionally, there are functions such as savehistory() and


loadhistory() available in the same package, which can be used to save
and load the commands history. For the illustration purpose a file name
filename with extension .Rhistory can be save and load as follows:
#To save the commands history
> savehistory(file = "[Link]")

#To load the commands history


> loadhistory(file = "[Link]")

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

• Commands are grouped together using braces ( { } ).


• The Vertical forward key ( ↑ ) is used to recall previously used
commands and the vertical backward key (↓) is used to get back.
• The Escape key (Esc) is used to cancel a command. It is generally used
as a saviour from incomplete commands.
• Getting ‘ + ’ sign after pressing the enter key indicates that the command
is not complete.
• Comments in R starts with Hashmark (‘ # ’).
• The following operators serves as assignment operators:
‘ <- ’, ‘ -> ’ and ‘ = ’
For the development of this course we have used ‘ <- ’ as an
assignment operator.

1.7 TERMINAL QUESTIONS


1. State whether the following statements are TRUE or FALSE:
(i) Once a package is installed in your R Software, it cannot be
uninstalled.
(ii) The class() and typeof() functions are used for same purpose.
(iii) ‘ == ’ is an assignment operator.
(iv) The Escape key to used to cancel a incomplete command.
2. Fill in the blanks:
(i) The typeof() function determines the ….. of any object.
(ii) An installed package can be removed using the ………function.
(iii) Comments in R starts with ….. .
3. How we can access the R manuals?
4. Write the purpose for which the citation() and contributors()
functions are used.
5. How we can load an installed package in 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

You might also like