Module 1 Basics of R
Module 1 Basics of R
BDS306C
Prepared By,
Dr. Anitha DB
Associate Professor & Head
Department of CSE-Data Science
ATME College of Engineering, Mysuru
ATME College of 1
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
• Introducing R,
• Initiating R,
• Packages in R,
• Environments and Functions,
• Flow Controls, Loops,
• Basic Data Types in R,
• Vectors
ATME College of 2
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.1 Introducing R
• R is a Programming language
• R also refers to the software that is used to run the R program
• Ross Ihaka and Robert Gentleman from University of Auckland created R language in 1990s.
• R is a free open source software
• It is more advanced statistical programming language and it can produce outstanding graphical outpts.
• It is extremely flexible.
Features of R
• R is an interpreted language and not compiled one. This means that all commands typed on the keyboard
are directly executed without need to build the complete program like C,C++ or Java.
• R’s Syntax is very simple.
ATME College of 3
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.2 Installing R
R is available in several forms, essentially for Unix and Linux machines, or some precompiled binaries for
windows, Linux and Macintosh.
The files needed to install R, either from the source or from the precompiled binaries are distributed from the
internet site of the Comprehensive R Archive Network(CRAN) where the instructions for the installation are
also available.
R can be installed from the link http://www.r-project.org using internet connection.
Use the “Download R” link in web page to download the R Executable.
Choose the version of R that is suitable for your operating system.
R scripts can run without the installation of IDE, the R-Studio using R-Console.
Once R installation completed then install R Studio.
ATME College of 4
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.2 Installing R
ATME College of 5
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.2 Installing R
R Studio is an Integrated Development Environment
that consists of a GUI with four parts
1. A text editor,
2. Command line interpreter,
3. Place to display files, plots, packages and help
information
4. Place to display the data being used and variables
used in the program(Environment/History)
ATME College of 6
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
1.3.1 First Program
Open R GUI, find the command prompt and type the command below and hit enter to run the command
>sum(1:5)
[1] 15
The result above shows that the command gives the result 15. That the command has taken the input of
integers from 1 to 5 and has performed the sum operation on them.
In the above command sum() is a function that takes the argument 1:5 which means a vector that consists of
sequence of integers from 1 to 5.
ATME College of 7
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
1.3.2 Help in R
• There are many ways to get help from R.
• If a function name or dataset name known then we can type ? followed by the name.
• If name is not known then we need to type ?? Followed by a term that is related to the search function.
• Keywords, special characters and two separate terms of search need to be enclosed in double or single quotes.
• The symbol # is used to comment a line in R program.
? name or help( ) or help.search()
ATME College of 8
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
1.3.3 Assigning Variables
• The result of the operations in R can be stored for reuse.
• The values can be assigned to the variables using the symbol “< -” or “=“ of which the symbol “<-” is preferred.
• There is no concept of variable declaration in R.
• The variable type is assumed based on the value assigned
Examples
> x<-1:3
>x
[1] 1 2 3
> Y=4:6
>Y
[1] 4 5 6
> x+3*Y-2
[1] 11 15 19
ATME College of 9
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
1.3.3 Assigning Variables
• The variable names consists of letters, numbers, dots and underscore, but a variable name should only starts
with an alphabet.
• The variable name should not be reserve words.
• To create a global variable (Variable available everywhere) we use the symbol “<<-”.
ATME College of 10
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
1.3.3 Assigning Variables
• If assignment and printing of a value has to be done in one line we can do the same in two ways.
• First method, by separating the two statements by a semicolon and
• The second method is by wrapping the assignment in parenthesis() as below.
> L<-sum(4:8);L
[1] 30
> (M<-sum(5:9))
[1] 35
ATME College of 11
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
1.3.4 Basic Mathematical Operations
The “+” operator is used to perform the addition. Examples
> 7:12+12:17
It can be used to add two numbers or add two vectors.
[1] 19 21 23 25 27 29
Vector represents an ordered set of values.
> c(3,1,8,6,7)+c(9,2,5,7,1)
Vectors are mainly used to analyse statistical data.
[1] 12 3 13 13 8
The “:” colon operator creates a sequence .
Sequence is a series of numbers within the given limits.
The “c()” function concatenates the values given within the brackets “(“ and”)”.
Variable names in R are case sensitive.
ATME College of 12
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Examples
1.3 Initiating R > sum(7:10)
[1] 34
1.3.4 Basic Mathematical Operations > mean(7:10)
[1] 8.5
The vectors and c() function in R help us to avoid loops.
> median(7:10)
The statistical function in R can take the vectors as input and produce results. [1] 8.5
> sum(7,8,9,10)
The sum() function takes vector arguments and produces results. [1] 34
Similar to the “+” operator all other operators in R take vectors as inputs and can produce the results.
The subtraction and multiplication operations work as below.
Examples for subtraction Examples for Multiplication
> c(5,6,1,9)-2 > -1.4*-2.3
[1] 3 4 -1 7 [1] 3.22
> c(5,6,1,9)-c(4,2,0,7) > -1:4*3
[1] 1 4 1 2 [1] -3 0 3 6 9 12
ATME College of 13
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
Examples
1.3.4 Basic Mathematical Operations
> identical(2^3,2**3)
The exponentiation operator is represented using the symbol “^” or the “**”.
[1] TRUE
This can be checked using the function identical()
Examples
The division operator is of three types. > 5:9/2
1. The ordinary division is represented using the “/” symbol. [1] 2.5 3.0 3.5 4.0 4.5
2. The integer division operator is represented using the “%/%” symbol. > 5:9%/%2
3. The modulo division operator is represented using the “%%” symbol [1] 2 3 3 4 4
> 5:9%%2
[1] 1 0 1 0 1
ATME College of 14
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
1.3.4 Basic Mathematical Operations
The other mathematical function are the trigonometry functions like sin(), cos(), tan(), asin(), acos(), atan()
and the logarithmic and exponential functions like log(), exp(), loglp(), expml().
All these mathematical functions can operate on vectors as well as individual elements.
Comparison or relational operators Examples
> c(2,4-2,1+1)==2
The operator “= =“ is used for comparing two values. [1] TRUE TRUE TRUE
> 1:5!=5:1
For checking inequalities of values the operator “!=“ is used. [1] TRUE TRUE FALSE TRUE TRUE
> exp(1:3)<20
The other relations operators are the ”<”, “>”, “<=“, “>=“. [1] TRUE TRUE FALSE
> (1:10)^2>=50
The relational operator also take the vectors as input [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
TRUE TRUE TRUE
and operate on them.
ATME College of 15
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.3 Initiating R
• Non-integers cannot be compared using the operator ”==“ because it produces
wrong results due to rounding off error of the floating numbers being > all.equal(sqrt(2)^2,3)
[1] "Mean relative difference: 0.5”
compared.
• To overcome this issue we have the function all.equal().
> isTRUE(all.equal(sqrt(2)^2,3))
Examples [1] FALSE
> sqrt(2)^2==2
[1] FALSE >c(“Week","WEEK","week","weak")=="week"
[1] FALSE FALSE TRUE FALSE
> sqrt(2)^2-2
[1] 4.440892e-16 > c("A","B","C")<"B"
[1] TRUE FALSE FALSE
> all.equal(sqrt(2)^2,2)
[1] TRUE > c("a","b","c")<"B"
[1] TRUE TRUE FALSE
ATME College of 16
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
• R Packages are installed in an online repository called CRAN (Comprehensive R Archive
Network).
• A Package is a collection of R functions and datasets.
• Currently, the CRAN package repository features 10756 available packages.
• The list of all available packages in the CRAN repository can be viewed from the web site
"https:// cran.r-project.org/web/packages/available_packages_by_name.html".
• To find the list of functions available in a package (say the package is "stats") we can use
the command ls("package:stats") or the command library(help = stats) in the command
prompt.
ATME College of 17
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
• A library is a folder in the machine that stores the files for a package.
• If a package is already installed on a machine we can load the same using the library() function.
• The name of the package to be loaded is passed to the library() function as argument without enclosing in
quotes.
• If the package name has to be programmatically passed to the library() function, then we need to set the
argument character.only = TRUE.
• If a package is not installed and if the library() function is used to load the package, it will throw an error
message.
• Alternatively if the require() function is used to load a package, it returns TRUE if the package is already
installed or it returns FALSE if the package is not already installed.
ATME College of 18
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
• We can list and see all the packages that are already loaded using the search() function.
• This list shows the global environment as the first one followed by the recently loaded packages.
• The last two are special environments, namely, "Autoloads" and "base" package.
ATME College of 19
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
• The function installed.packages() returns a data frame with information about all the packages installed in a
machine.
• It is safe to view the results of this using the View() function as it may list hundreds of packages.
• This list of packages also shows the version of the package installed, location on the machine and dependent
packages.
ATME College of 20
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
The function R.home("library”) retrieves the location on the machine that stores all R default packages.
The same result can be accomplished using the .Library command.
The home directory can be listed using the path.expand (“~”) and Sys.getenv("HOME") functions.
ATME College of 21
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
When R is upgraded, it is required to reinstall all the packages as different versions of R needs different
versions of the packages.
The function .libPaths() lists all the R libraries in the installed machine.
The first value listed is the place where the packages will be installed by default.
ATME College of 22
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
• The CRAN package repository contains handful of packages that needs special attention.
• To access additional repositories, type setRepositories() and select the repository required.
• The repositories R-Forge and rforge.net contains the development versions of the packages that appear on the
CRAN repository.
• The function available.packages() lists thousands of packages in each of the selected repository. (Note: can
use the View() function to restrict fetching of thousands of the packages at one go)
ATME College of 23
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.4 Packages in R
There are many online repositories like GitHub, Bitbucket, and Google Code from where many R Packages can
be retrieved.
The packages can be installed using the function install.packages() function by mentioning the name of the
package as argument to this function.
But, it is necessary to have internet connection to install any package and write permission to the hard drive.
To update the latest version of the installed packages, we use the function update.packages() with the argument
ask = FALSE which disallows prompting before updating each package.
To delete a package already installed, we use the function remove.packages() by passing the name of the
package to be removed as argument.
> install.packages("chron”)
ATME College of 24
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
>newenvironment <-new.env()
ATME College of 25
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
An environment can be converted into a list using the function as.list() and a list can be converted into an
environment using the function as.environment() or the function list2env().
ATME College of 27
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
The word frame is used interchangeably with the word environment. The function to refer to parent environment
is denoted as parent.frame(). The variables assigned from the command prompt are stored in the global
environment. The functions and the variables from the R's base package are stored in the base environment.
ATME College of 28
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
ATME College of 29
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
In this cube is the name of the function and x is the argument passed to this function. The content within the curly
braces is the body of the function. (Note: If it is a one line code we can omit the curly braces). Once a function is
defined, it can be called like any other function in R by passing its arguments.
ATME College of 30
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
ATME College of 31
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
ATME College of 32
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
x<-function(a1)
{
a2<-1
y<-function(a1)
{
a2/a1
}
y(a1)
}
print(x(5))
ATME College of 33
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
if (b > a) {
message("b is greater than a")
}
In this example we use two variables, a and b, which are used as a part of the if statement to test whether b is
greater than a. As a is 33, and b is 200, we know that 200 is greater than 33, and so we print to screen that "b is
greater than a".
ATME College of 35
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
a <- 33
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a = = b) {
print ("a and b are equal")
}
In this example a is equal to b, so the first condition is not true, but the else if condition is true, so we print to screen that "a
and b are equal".
ATME College of 36
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
a <- 200
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print("a and b are equal")
} else {
print("a is greater than b")
}
In this example, a is greater than b, so the first condition is not true, also the else if condition is not true, so we go to
the else condition and print to screen that "a is greater than b".
ATME College of 37
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
ATME College of 38
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
ATME College of 39
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
ATME College of 40
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.7 Loops
There are three kinds of loops in R namely
• Repeat
• While
• For
1.7.1 Repeat Loops
The repeat is the easiest loop in R that executes the same code until it is forced to stop.
This repeat is similar to the do while statement in other languages.
A break statement can be given when it is required to break the looping.
Also it is possible to skip the rest of the statements in a loop and executes the next iteration and this is done
by using the next statement.
a<-1
repeat{
print(a)
a<-a+1
if(a==4){
break }}
ATME College of 42
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.7 Loops
1.7.1 Repeat Loops
Repeat with break and next statement
Repeat with break statement
a<-0
a<-1 repeat{
repeat{ a<-a+1
print(a) if(a==4)
a<-a+1 { next
if(a==4){ }
break print(a)
} if(a==6)
} { break
}
}
ATME College of 43
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.7 Loops
1.7.2 While Loops
The while loops are backward repeat loops.
The repeat loop executes the code and then checks for the condition, but in while loops the condition is first
checked and then the code is executed.
So, in this case it is possible that the code may not be executed even once when the condition fails at the
entry itself during the first iteration.
A break statement can be given when it is required to break the looping.
Also it is possible to skip the rest of the statements in a loop and executes the next iteration and this is done
by using the next statement.
ATME College of 44
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.7 Loops
1.7.1 Repeat Loops
while with next statement
While without any break and next statement
a<-0
while(a<6)
{ a<-a+1
a<-0
if(a==4)
while(a<6)
{ next
{ print(a)
}
a<-a+1
print(a)
}
}
Next: With the next statement, we can skip an iteration without
terminating the loop.
When the loop passes the value 4, it will skip it and continue to
loop.
ATME College of 45
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.7 Loops
1.7.2 While Loops
While with break and next statement
While with break statement
a<-0 a<-0
while(a<6){ while(1){
a<-a+1 a<-a+1
if(a==4) if(a==4)
{ break { next
} }
print(a) print(a)
} if(a==8)
{ break
Break:With the break statement, we can stop the loop even if
the while condition is TRUE
}
The loop will stop at 3 because we have chosen to finish the }
loop by using the break statement when a is equal to 4 (a == 4).
ATME College of 46
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.7 Loops
for(i in 1:5)
{
j<-i*i
message("The square value of ",i," is ",j)
}
ATME College of 47
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
1.7 Loops
1.7.3 For Loops
for(i in c(TRUE,FALSE,NA))
{
message("This statement is ",i)
}
a<-c(1,2,3)
b<-c("a","b","c","d")
d<-c(TRUE,FALSE)
l<-list(a,b,d)
for(i in l)
{
message("The value of list is ",i)
}
ATME College of 48
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
1. Numeric
2. Integer
3. Complex
4. Logical
5. Character
2.1.1 Numeric
• Decimal values are called numeric in R.
• It is the default computational data type.
• If we assign a decimal value to a variable x as follows, x will be of numeric type.
ATME College of 49
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
2.1.2 Integer
In order to create an integer variable I R, the as.integer() function is involved as below
ATME College of 50
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
2.1.2 Integer
We can force a numeric value into an integer with the same as.integer() function as below.
ATME College of 51
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
2.1.2 Integer
We can force a numeric value into an integer with the same as.integer() function as below.
The integer values of the logical values TRUE and FALSE are 1 and 0 respectively.
ATME College of 52
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
2.1.3 Complex
A complex number is expressed as an imaginary value i
If we find the square root of -1, it gives an error. But if it is converted into a complex number and then square
root is applied, it produces the necessary result as another complex number.
ATME College of 53
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
2.1.4 Logical
• When two variable are compared, the logical values are created.
• The logical operators are “&”(and), “|”(or) and “!”(negation/not).
ATME College of 54
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
2.1.5 Character
The Character object is used to represent string values in R.
Objects can be converted into character values using the as.character() function.
A paste() function can be used to concatenate two character values
ATME College of 55
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.1 Basic data types in R
2.1.5 Character
However, a readable string can be created using the sprintf() function and this is similar to the C Language syntax.
The substr() function can be used to extract a substring from a given string.
The sub() function is used to replace the first occurrence of a string with another string as below.
ATME College of 56
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.2 Vectors
A sequence of data elements of the same basic type is called a Vector.
Members in a Vector are called as components or members.
The vector() function creates a vector of a specified type and length.
The result is a zero or FALSE or empty string.
The command also produces the same result as the above commands.
ATME College of 57
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.2 Vectors
The seq() function allows to generate sequences.
The function seq.int() also creates sequence from one number to another, but this function provides more options fo
splitting the sequence
ATME College of 58
Department of CSE-DS, ATMECE
Engineering, Mysuru
Module1 : Basics of R
Data Types in R
2.2 Vectors
The function length() can be used to find the length of the vector, that is the number of elements in a vector.
Using this function, it is possible to assign new length to a vector.
If the vector length is extended NA(s) will be added to the end.
ATME College of 59
Department of CSE-DS, ATMECE
Engineering, Mysuru