R-Programming
UNIT-III: Introduction to R: Introduction, Installing R and Data Types in R, Programming
using R: Operators, Conditional Statements, Looping, Scripts, Function creation.
What is R
R is a popular programming language used for statistical computing
and graphical presentation.
Its most common use is to analyze and visualize data.
Why Use R?
It is a great resource for data analysis, data visualization, data science
and machine learning
It provides many statistical techniques (such as statistical tests,
classification, clustering and data reduction)
It is easy to draw graphs in R, like pie charts, histograms, box plot,
scatter plot, etc
It works on different platforms (Windows, Mac, Linux)
It is open-source and free
It has many packages (libraries of functions) that can be used to solve
different problems
R programming language is not only a statistic package but also allows
us to integrate with other languages (C, C++). Thus, you can easily
interact with many data sources and statistical packages.
The R programming language has a vast community of users and it’s
growing day by day.
Features of R Programming Language
1. Statistical Features of R:
Basic Statistics: The most common basic statistics terms are the
mean, mode, and median. These are all known as “Measures of
Central Tendency.” So using the R language we can measure central
tendency very easily.
Static graphics: R is rich with facilities for creating and developing
interesting static graphics. R contains functionality for many plot types
including graphic maps, mosaic plots, biplots, and the list goes on.
Probability distributions: Probability distributions play a vital role in
statistics and by using R we can easily handle various types of
probability distribution such as Binomial Distribution, Normal
Distribution, Chi-squared Distribution and many more.
2. Programming Features of R:
R Packages: One of the major features of R is it has a wide
availability of libraries. R has CRAN(Comprehensive R Archive
Network), which is a repository holding more than 10, 0000 packages.
Distributed Computing: Distributed computing is a model in which
components of a software system are shared among multiple
computers to improve efficiency and performance. Two new
packages ddR and multidplyr used for distributed programming in R
were released in November 2015.
Disadvantages of R:
In the R programming language, the standard of some packages is
less than perfect.
Although, R commands give little pressure to memory management.
So R programming language may consume all available memory.
In R basically, nobody to complain if something doesn’t work.
Formal Definition of R:
R is an open-source programming language that is widely used as a
statistical software and data analysis tool. R generally comes with the
Command-line interface. R is available across widely used platforms
like Windows, Linux, and macOS. Also, the R programming language is
the latest cutting-edge tool.
It was designed by Ross Ihaka and Robert Gentleman at the
University of Auckland, New Zealand, and is currently developed by
the R Development Core Team. R programing language is an
implementation of the S(Statistical) programming language. R made
its first appearance in 1993.
Programming in R:
Since R is much similar to other widely used languages syntactically, it
is easier to code and learn in R.
Programs can be written in R in any of the widely used IDE like R
Studio, Rattle, Tinn-R, etc.
( for online editor datacamp.com)
After writing the program save the file with the extension .r.
To run the program use the following command on the command line:
R file_name.r
Example:
# R program to print Welcome to R Programming!
# Below line will print "Welcome to R Programming!"
cat("Welcome to R Programming!")
Output:
Welcome to R Programming!
Printing in R:
1. Unlike many other programming languages, you can output code in R without
using a print function. To output text in R, use single or double quotes:
Ex: “Hello World”
2. However, when your code is inside an R expression (for example inside
curly braces {} ), use the print() function to output the result.
Ex: for (x in 1:10) {
print(x)
}
3. print("HelloWorld")
Output:”Hello World”
4. print("HelloWorld", quote=FALSE)
Output: Hello World
5. myString<-“Hello World!”
print(myString)
Output: Hello World!
6. To output numbers, just type the number (without quotes):
Ex: 1
2
3
4
7. To do simple calculations, type numbers together with operator:
Ex: 5+4
Output: 9
Comments in R:
Comments can be used to explain R code, and to make it more
readable. It can also be used to prevent execution when testing
alternative code.
Comments starts with a #. When executing the R-code, R will ignore
anything that starts with #.
Singleline Comments:
# This is a comment
"Hello World!"
Multiline Comments:
# This is a comment
# written in
# more than just one line
"Hello World!"
R Variables:
Variables are containers for storing data values.
Variable Names: A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
Rules for R variables are:
A variable name must start with a letter and can be a combination of
letters, digits, period(.) and underscore(_).
If it starts with period(.), it cannot be followed by a digit.
A variable name cannot start with a number or underscore (_)
Variable names are case-sensitive (age, Age and AGE are three
different variables)
Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)
Ex: myvar,my_var, myVar, MYVAR, myvar2,.myvar- Legal variable names
2myvar, my-var, my var, _my_var, my_v@ar, TRUE- Illegal variable names
Creating Variables in R
R does not have a command for declaring a variable.
A variable is created the moment you first assign a value to it.
To assign a value to a variable, use any of these =, ->, <- signs.
To output (or print) the variable value, just type the variable name:
Ex: name <- "abc"
age <- 20
name # output "abc"
age # output 20
R is a dynamically typed language, i.e. the variables are not
declared with a data type rather they take the data type of the R-
object assigned to them. This feature is also shown in languages like
Python and PHP.
Ex: var_x <- "Hello"
cat("The class of var_x is ",class(var_x),"\n")
var_x <- 34.5
cat(" Now the class of var_x is ",class(var_x),"\n")
var_x <- 27L
cat(" Next the class of var_x becomes ",class(var_x),"\n")
Output:
The class of var_x is character
Now the class of var_x is numeric
Next the class of var_x becomes integer
Declaring and Initializing Variables: R supports three ways of variable
assignment:
using equal operator(=)- data is copied from right to left.
using leftward operator(<-)- data is copied from right to left.
using rightward operator(->)- data is copied from left to right.
Syntax
variable_name = value #using equal to operator
variable_name <- value #using leftward operator
value -> variable_name #using rightward operator
Ex: # R program to illustrate Initialization of variables
var1 = "hello" # using equal to operator
print(var1)
var2 <- "hello" # using leftward operator
print(var2)
"hello" -> var3 # using rightward operator
print(var3)
Output:
[1] "hello"
[1] "hello"
[1] "hello"
Multiple Variables
R allows you to assign the same value to multiple variables in one line:
Ex: # Assign the same value to multiple variables in one line
var1 <- var2 <- var3 <- "Orange"
# Print variable values
var1
var2
var3
Output:
[1] "Orange"
[1] "Orange"
[1] "Orange"
Print / Output Variables
Compared to many other progamming languages, you do not have to use a
function to print/output variables in R. You can just type the name of the
variable:
Ex: name <- "Joe"
name # auto-print the value of the name variable
However, R does have a print() function available if you want to use it.
Ex: name <- "Joe"
print(name) # print the value of the name variable
Concatenate Elements
You can also concatenate, or join, two or more elements, by using
the paste() function.
To combine both text and a variable, R uses comma (,):
Ex: text <- "awesome"
paste("R is", text)
Output: R is awesome
Ex: text1 <- "R is"
text2 <- "awesome"
paste(text1, text2)
Output: R is awesome
For numbers, the + character works as a mathematical operator:
Ex: num1 <- 5
num2 <- 10
num1 + num2
Output: 15
Variable Assignment:
The variables can be assigned values using leftward, rightward and
equal to operator.
The values of the variables can be printed
using print() or cat() function.
The cat() function combines multiple items into a continuous print
output.
Ex: var.1 = c(0,1,2,3) # Assignment using equal operator.
var.2 <- c("learn","R") # Assignment using leftward operator.
c(TRUE,1) -> var.3 # Assignment using rightward operator.
print(var.1)
cat ("var.1 is ", var.1 ,"\n")
cat ("var.2 is ", var.2 ,"\n")
cat ("var.3 is ", var.3 ,"\n")
When we execute the above code, it produces the following result −
Output: [1] 0 1 2 3
var.1 is 0 1 2 3
var.2 is learn R
var.3 is 1 1
Note − The vector c(TRUE,1) has a mix of logical and numeric class. So logical
class is coerced to numeric class making TRUE as 1.
Important Methods for Variables:
R provides some useful methods to perform operations on variables.
These methods are used to determine the data type of the variable,
finding a variable, deleting a variable, etc.
Following are some of the methods used to work on variables:
1. class() function: This built-in function is used to determine the data
type of the variable provided to it. The variable to be checked is passed to
this as an argument and it prints the data type in return.
Syntax:class(variable)
Ex: var1 = "hello"
print(class(var1))
Output:
[1] "character"
2. ls() function:
This builtin function is used to know all the present variables in the
workspace. This is generally helpful when dealing with a large number of
variables at once and helps prevents overwriting any of them.
Syntax: ls()
Ex:var1 = "hello" # using equal to operator
var2 <- "hello" # using leftward operator
"hello" -> var3 # using rightward operator
print(ls())
Output:
[1] "var1" "var2" "var3"
3. rm() function:
This is again a built-in function used to delete an unwanted variable within
your workspace. This helps clear the memory space allocated to certain
variables that are not in use thereby creating more space for others. The
name of the variable to be deleted is passed as an argument to it.
Syntax: rm(variable)
Ex: var1 = "hello" # using equal to operator
var2 <- "hello" # using leftward operator
"hello" -> var3 # using rightward operator
rm(var3) # Removing variable
print(var3)
Output:
Error in print(var3) : object 'var3' not found
Execution halted
R-Data Types
Each variable in R has an associated data type.
Each data type requires different amounts of memory and has some
specific operations which can be performed over it.
R has the following basic data types and the following table shows
the data type and the values that each data type can take.
Basic Data Values Example
Types
Numeric Set of all real numbers 10.5, 55, 787
Integer Set of all integers 1L, 55L, 100L, where the
letter "L" declares this as
an integer
Logical (boolean) TRUE and FALSE TRUE or FALSE
Complex Set of complex numbers 9 + 3i, where "i" is the
imaginary part
Character “a”, “b”, “c”, …, “@”, “#”, “$”, “k", "R is exciting",
(string) …., “1”, “2”, …etc "FALSE", "11.5"
Raw A raw data type is used to
holds raw bytes
Example:
# numeric
x <- 10.5
class(x)
# integer
x <- 1000L
class(x)
# complex
x <- 9i + 3
class(x)
# character/string
x <- "R is exciting"
class(x)
# logical/boolean
x <- TRUE
class(x)
Output:
[1] "numeric"
[1] "integer"
[1] "complex"
[1] "character"
[1] "logical"
Example:
x=4
y=3
z=x>y
print(z)
print(class(z))
Output:
[1] TRUE
[1] "logical"
Example:
variable_raw<- charToRaw("Learning r programming")
cat(variable_raw,"\n")
cat("The data type of variable_char is ",class(variable_raw),"\n\n")
Output:
4c 65 61 72 6e 69 6e 67 20 72 20 70 72 6f 67 72 61 6d 6d 69 6e 67
The data type of variable_char is raw
Finding the data type of an object:
To find the data type of an object you have to use class() function.
The syntax for doing that is you need to pass the object as an
argument to the function class() to find the data type of an object.
Syntax: class(object)
Example
print(class(TRUE))
print(class(3L))
print(class(10.5))
print(class(1+2i))
print(class("12-04-2020"))
Output:
[1] "logical"
[1] "integer"
[1] "numeric"
[1] "complex"
[1] "character"
Type verification:
To do type verification, we need to use the prefix “is.” before the
data type as a command.
The syntax for that is, is.data_type() of the object you have to verify.
Syntax: is.data_type(object)
Example
print(is.logical(TRUE))
print(is.integer(3L))
print(is.numeric(10.5))
print(is.complex(1+2i))
print(is.character("12-04-2020"))
print(is.integer("a"))
print(is.numeric(2+3i))
Output:
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
Coerce or convert the data type of an object to another:
Using this we can change or convert the data type of one object to
another.
To perform this action we have to use the prefix “as.” before the
data type as the command.
The syntax for doing that is as.data_type() of the object which we
want to coerce.
Syntax: as.data_type(object)
Example
print(as.numeric(TRUE))
print(as.complex(3L))
print(as.logical(10.5))
print(as.character(1+2i))
print(as.numeric("12-04-2020"))
Output:
[1] 1
[1] 3+0i
[1] TRUE
[1] "1+2i"
[1] NA
Warning message:
In print(as.numeric("12-04-2020")) : NAs introduced by coercion
Note: All the coercions are not possible and if attempted will be
returning an “NA” value.
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations.
R language is rich in built-in operators and provides following types of
operators.
Types of Operators:
We have the following types of operators in R programming −
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
Miscellaneous Operators
Basic Operations
a <- c(7+4,7-4,7*4,7/4) # elemental arithmetic operations
>a
[1] 11.00 3.00 28.00 1.75
> length(a) # return vector length
[1] 4
> c(min(a),max(a)) # calculate minimum and maximum value of the vector
[1] 1.75 28.00
> which.min(a) # determine the location (index) of the minimum
[1] 4
> which.max(a) # determine the location (index) of the maximum
[1] 3
> sort(a) # sort vector values
[1] 1.75 3.00 11.00 28.00
> sum(a) # calculate sum of all vector values
[1] 43.75
> cumsum(1:10) # calculate cumulative sum
[1] 1 3 6 10 15 21 28 36 45 55
> cumprod(1:5) # calculate cumulative product
[1] 1 2 6 24 120 720 5040 40320
> mean(a) # calculate the mean value
[1] 10.9375
> median(a) # calculate the median value
[1] 7
>names(sort(-table(a)))[1] #For calculating mode
[1]NULL
> var(a) # calculate the variance
[1] 146.1823
> sd(a) # calculate the standard deviation
[1] 12.09059
> quantile(a, 0.25) # calculate first quantile (prob=25%)
25%
2.6875
There is a command to get basic statistical information in a simple
way:
> summary(a)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.750 2.688 7.000 10.940 15.250 28.000
Arithmetic Operators
Arithmetic operations simulate various math operations, like addition,
subtraction, multiplication, division and modulo using the specified operator
between operands, which maybe either scalar values, complex numbers or
vectors. The operations are performed element wise at the corresponding
positions of the vectors.
Operator Name Syntax Example
+ Addition X+Y 10+2=12
- Subtraction X-Y 10-2=8
* Multiplication X*Y 10*2=20
/ Division X/Y 10/2=5
^ Exponent X^Y 10^2=100
%% Modulus(remainder) X%%Y 10%%2=0
%/% Integer X%/%Y 10%/%2=5
division(quotient)
Example:
vec1 <- c(2, 5.5)
vec2 <- c(8, 3)
cat ("Addition of vectors :", vec1 + vec2, "\n")
cat ("Subtraction of vectors :", vec1 - vec2, "\n")
cat ("Multiplication of vectors :", vec1 * vec2, "\n")
cat ("Division of vectors :", vec1 / vec2, "\n")
cat ("Power operator :", vec1 ^ vec2, “\n”)
cat ("Modulo of vectors :", vec1 %% vec2, "\n")
cat ("Integer division of vectors :", vec1 %/% vec2, "\n")
Output:
Addition of vectors : 10 8.5
Subtraction of vectors : -6 2.5
Multiplication of vectors : 16 16.5
Division of vectors : 0.25 1.833333
Power operator : 256 166.375
Modulo of vectors : 2 2.5
Integer division of vectors : 0 1
Relational Operators
The relational operators carry out comparison operations between
the corresponding elements of the operands.
Each element of the first vector is compared with the corresponding
element of the second vector.
The result of comparison is a Boolean value.
Operator Name Syntax
== Equal X==Y
!= Not equal X!=Y
> Greater than X>Y
< Less than X<Y
>= Greater than or equal to X>=Y
<= Less than or equal to X<=Y
Example:
vec1 <- c(5.5,6,9)
vec2 <- c(2.5,14,9)
cat ("Vector1 less than Vector2 :", vec1 < vec2, "\n")
cat ("Vector1 less than equal to Vector2 :", vec1 <= vec2, "\n")
cat ("Vector1 greater than Vector2 :", vec1 > vec2, "\n")
cat ("Vector1 greater than equal to Vector2 :", vec1 >= vec2, "\n")
cat ("Vector1 not equal to Vector2 :", vec1 != vec2, "\n")
cat ("Vector1 equal to Vector2 :", vec1 == vec2, "\n")
Output:
Vector1 less than Vector2 : FALSE TRUE FALSE
Vector1 less than equal to Vector2 : FALSE TRUE TRUE
Vector1 greater than Vector2 : TRUE FALSE FALSE
Vector1 greater than equal to Vector2 : TRUE FALSE TRUE
Vector1 not equal to Vector2 : TRUE TRUE FALSE
Vector1 equal to Vector2 : FALSE FALSE TRUE
Logical Operators
Logical operators are used to combine conditional statements, based
on the specified operator between the operands it is then evaluated
to either a True or False boolean value.
Any non zero integer value is considered as a TRUE value.
It is applicable only to vectors of type logical, numeric or complex.
Operator Name Description
& Element-wise Logical AND It returns TRUE if both elements
operator are TRUE
&& Logical AND operator Returns TRUE if both statements
are TRUE
| Elementwise Logical OR It returns TRUE if one of the
operator statement is TRUE
|| Logical OR operator It returns TRUE if one of the
statement is TRUE.
! Logical NOT returns FALSE if statement is
TRUE
Input : list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1 & list2)
Output : FALSE TRUE
Input : list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1|list2)
Output : TRUE TRUE
Input : list1 <- c(0,FALSE)
Output : TRUE TRUE
Input : list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1 && list2)
Output : FALSE
Input : list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1||list2)
Output : TRUE
Example:
vec1 <- c(3,0,TRUE,2+2i)
vec2 <- c(4,1,FALSE,2+3i)
cat ("Element wise AND :", vec1 & vec2, "\n")
cat ("Element wise OR :", vec1 | vec2, "\n")
cat ("Logical AND :", vec1 && vec2, "\n")
cat ("Logical OR :", vec1 || vec2, "\n")
cat ("Negation :", !vec1)
Output:
Element wise AND : TRUE FALSE FALSE TRUE
Element wise OR : TRUE TRUE TRUE TRUE
Logical AND : TRUE
Logical OR : TRUE
Negation : FALSE TRUE FALSE FALSE
Assignment Operators
Assignment operators are used to assign values to various data
objects in R.
The objects may be integers, vectors or functions.
These values are then stores by the assigned variable names.
There are two kinds of assignment operators: Left and Right
Example:
vec1 <- c(2:5)
c(2:5) ->> vec2
vec3 <<- c(2:5)
vec4 = c(2:5)
c(2:5) -> vec5
cat ("vector 1 :", vec1, "\n")
cat("vector 2 :", vec2, "\n")
cat ("vector 3 :", vec3, "\n")
cat("vector 4 :", vec4, "\n")
cat("vector 5 :", vec5)
Output:
vector 1 : 2 3 4 5
vector 2 : 2 3 4 5
vector 3 : 2 3 4 5
vector 4 : 2 3 4 5
vector 5 : 2 3 4 5
Miscellaneous Operators
These operators are used to for specific purpose and not general
mathematical or logical computation.
These are the mixed operators that simulate the printing of sequences
and assignment of vectors, either left or right handedly.
Miscellaneous operators are used to manipulate data
Operator Description
: Colon operator. It creates the series of numbers in sequence for
a vector.
%in% This operator is used to identify if an element belongs to a vector.
%*% This operator is used to multiply a matrix with its transpose.
Example:
v <- 2:8
print(v)
Output:
[1] 2 3 4 5 6 7 8
Example:
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t)
print(v2 %in% t)
Output:
[1] TRUE
[1] FALSE
Example:
M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
t = M %*% t(M)
print(t)
Output:
[,1] [,2]
[1,] 65 82
[2,] 82 117
Conditional Statements
Conditional statements (Control statements) are expressions used to
control the execution and flow of the program based on the
conditions provided in the statements.
Decision making structures require the programmer to specify one or
more conditions to be evaluated or tested by the program, along with
a statement or statements to be executed if the condition is
determined to be true, and optionally, other statements to be
executed if the condition is determined to be false.
Following is the general form of a typical decision making structure
found in most of the programming languages –
In R programming, there are 8 types of control statements as follows:
1. if condition
2. if-else condition
3. for loop
4. nested loops
5. while loop
6. repeat and break statement
7. return statement
8. next statement
if condition
An if statement consists of a Boolean expression followed by one or
more statements.
Syntax:
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true.
Flow Diagram:
Example:
a <- 33
b <- 200
if (b > a) {
print("b is greater than a")
}
Output:
[1] "b is greater than a"
Example:
x <- 100
if(x > 10){
print(paste(x, "is greater than 10"))
}
Output:
[1] "100 is greater than 10"
Example:
x <- 30L
if(is.integer(x)) {
print("X is an Integer")
}
Output:
[1] "X is an Integer"
if-else condition
An if statement can be followed by an optional else statement which
executes when the boolean expression is false.
Syntax:
if(boolean_expression) {
// statement(s) will execute if the boolean expression is true.
} else {
// statement(s) will execute if the boolean expression is false.
Example:
a <- 33
b <- 33
if (b > a) {
print("b is greater than a")
} else if (a == b) {
print ("a and b are equal")
}
Output:
[1] "a and b are equal"
Example:
x <- 5
if(x > 10){
print(paste(x, "is greater than 10"))
}else{
print(paste(x, "is less than 10"))
}
Output:
[1] "5 is less than 10"
Example:
x <- c("what","is","truth")
if("Truth" %in% x) {
print("Truth is found")
} else {
print("Truth is not found")
}
Output:
[1] "Truth is not found"
The if...else if...else statement
Syntax:
The basic syntax for creating an if...else if...else statement in R is −
if(boolean_expression 1) {
// Executes when the boolean expression 1 is true.
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true.
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true.
} else {
// executes when none of the above condition is true.
}
Example:
x <- c("what","is","truth")
if("Truth" %in% x) {
print("Truth is found the first time")
} else if ("truth" %in% x) {
print("truth is found the second time")
} else {
print("No truth found")
}
Output:
[1] "truth is found the second time"
Nested If Statements
Example:
x <- 41
if (x > 10) {
print("Above ten")
if (x > 20) {
print("and also above 20!")
} else {
print("but not above 20")
}
} else {
print("below 10")
}
Output:
[1] "Above ten"
[1] "and also above 20!"
Switch case
Switch case statements are a substitute for long if statements that
compare a variable to several integral values.
Switch case in R is a multiway branch statement. It allows a variable
to be tested for equality against a list of values.
Switch statement follows the approach of mapping and searching
over a list of values. If there is more than one match for a specific
value, then the switch statement will return the first match found of
the value matched with the expression.
Syntax:
switch(expression, case1, case2, case3....)
An expression type with character string always matched to the listed
cases.
An expression which is not a character string then this exp is coerced to
integer.
For multiple matches, the first match element will be used.
No default argument case is available there in R switch case.
An unnamed case can be used, if there is no matched case.
Example:
x <- switch(3,"first","second","third","fourth")
print(x)
Output:
[1] "third"
Example:
a= 1
b= 2
y = switch(a+b, "C", "C++", "JAVA", "R" )
print (y)
Output:
[1] "Hello JAVA"
Example:
x= "2"
y="1"
a = switch( paste(x,y,sep=""),
"9"="Hello C",
"12"="Hello C++",
"18"="Hello JAVA",
"21"="Hello R"
)
print (a)
Output:
[1] "Hello R"
Example:
val1 = 6
val2 = 7
val3 = "a"
result = switch(val3,
"a"= cat("Addition =", val1 + val2),
"s"= cat("Subtraction =", val1 - val2),
"d"= cat("Division = ", val1 / val2),
"m"= cat("Multiplication =", val1 * val2),
"u"= cat("Modulus =", val1 %% val2),
"p"= cat("Power =", val1 ^ val2))
print(result)
Output:
Addition = 13NULL
Example:
y = "18"
a=10
b=2
x = switch(y,
"9"=cat("Addition=",a+b),
"12"=cat("Subtraction =",a-b),
"18"=cat("Division= ",a/b),
"21"=cat("multiplication =",a*b)
)
print (x)
Output:
Division= 5NULL
Looping
for – Loop
For loop in R is useful to iterate over the elements of a list, data
frame, vector, matrix or any other object.
It means, the for loop can be used to execute a group of statements
repeatedly depending upon the number of elements in the object.
It is an entry controlled loop, in this loop the test condition is tested
first, then the body of the loop is executed, the loop body would not
be executed if the test condition is false.
Syntax:
for (var in vector) {
statement(s)
}
Example:
for (x in 1:10) {
print(x)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
Example:
fruit <-c('Apple', 'Orange',"Guava", 'Pinapple', 'Banana','Grapes')
for ( i in fruit){
print(i)
}
Output:
[1] "Apple"
[1] "Orange"
[1] "Guava"
[1] "Pinapple"
[1] "Banana"
[1] "Grapes"
Example:
for (i in seq(1, 5, by=2)) {
print(i)
}
Output:
[1] 1
[1] 3
[1] 5
Example: Creating an empty list and using for statement populate the list
list <- c()
for (i in seq(1, 5, by=1)) {
list[[i]] <- i*i
}
print(list)
Output:
[1] 1 4 9 16 25
Example:
x <- c(2,5,3,9,8,11,6,44,43,47,67,95,33,65,12,45,12)
count <- 0
for (val in x) {
if(val %% 2 == 0) count = count+1
}
print(count)
Output:
[1] 6
Example:
for (i in 1:3)
{
for (j in 1:i)
{
print(i * j)
}
}
Output:
[1] 1
[1] 2
[1] 4
[1] 3
[1] 6
[1] 9
Example:
for (i in 1:2)
{
for (j in 1:3)
{
print(i * j)
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 2
[1] 4
[1] 6
Example:
adj <- list("red", "big", "tasty")
fruits <- list("apple", "banana", "cherry")
for (x in adj) {
for (y in fruits) {
print(paste(x, y))
}
}
Output:
[1] "red apple"
[1] "red banana"
[1] "red cherry"
[1] "big apple"
[1] "big banana"
[1] "big cherry"
[1] "tasty apple"
[1] "tasty banana"
[1] "tasty cherry"
Jump Statements
We use a jump statement in loops to terminate the loop at a
particular iteration or to skip a particular iteration in the loop.
The two most commonly used jump statements in loops are:
1. Break
2. Next
Break Statement: A break statement is a jump statement which is used
to terminate the loop at a particular iteration. The program then continues
with the next statement outside the loop(if any).
Example:
for (i in c(3, 6, 23, 19, 0, 21))
{
if (i == 0)
{
break
}
print(i)
}
print("Outside Loop")
Output:
[1] 3
[1] 6
[1] 23
[1] 19
[1] "Outside Loop"
Example:
fruits <- list("apple", "banana", "cherry")
for (x in fruits) {
if (x == "cherry") {
break
}
print(x)
}
Output:
[1] "apple"
[1] "banana"
Next Statement: It discontinues a particular iteration and jumps to the
next iteration. So when next is encountered, that iteration is discarded and
the condition is checked again. If true, the next iteration is executed.
Hence, a next statement is used to skip a particular iteration in the loop.
Example:
fruits <- list("apple", "banana", "cherry")
for (x in fruits) {
if (x == "banana") {
next
}
print(x)
}
Output:
[1] "apple"
[1] "cherry"
Example:
for (i in c(3, 6, 23, 19, 0, 21))
{
if (i == 0)
{
next
}
print(i)
}
print('Outside Loop')
Output:
[1] 3
[1] 6
[1] 23
[1] 19
[1] 21
[1] "Outside Loop"
Example:
v <- LETTERS[1:6]
for ( i in v) {
if (i == "D") {
next
}
print(i)
}
Output:
[1] "A"
[1] "B"
[1] "C"
[1] "E"
[1] "F"
Example:
j<-0
while(j<10){
if (j==7){
j=j+1
next
}
cat("\nnumber is =",j)
j=j+1
}
Output:
number is = 0
number is = 1
number is = 2
number is = 3
number is = 4
number is = 5
number is = 6
number is = 8
number is = 9
While-loop
A while loop is a type of control flow statements which is used to
iterate a block of code several numbers of times.
The while loop terminates when the value of the Boolean expression
will be false.
In while loop, firstly the condition will be checked and then after the
body of the statement will execute.
In this statement, the condition will be checked n+1 time, rather than
n times.
Syntax: while (test_expression) {
statement
}
Example:
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
Example:
v <- c("Hello","while loop","example")
cnt <- 2
while (cnt < 7) {
print(v)
cnt = cnt + 1
}
Output:
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
[1] "Hello" "while loop" "example"
Repeat-loop
Repeat loop in R is used to iterate over a block of code multiple
number of times.
It is a special type of loop in which there is no condition to exit from
the loop.
For exiting, we include a break statement with a user-defined condition
that executes if the condition within the loop body results to be true.
This property of the loop makes it different from the other loops.
A repeat loop constructs with the help of the repeat keyword in R. It
is very easy to construct an infinite loop in R.
Syntax:
repeat {
commands
if(condition) {
break
}
}
Example:
v <- c("Hello","loop")
cnt <- 2
repeat {
print(v)
cnt <- cnt+1
if(cnt > 5) {
break
}
}
Output:
[1] "Hello" "loop"
[1] "Hello" "loop"
[1] "Hello" "loop"
[1] "Hello" "loop"
Example:
result <- 1
i <- 1
repeat {
print(result)
i <- i + 1
result = result + 1
if(i > 5) {
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
Function
A set of statements which are organized together to perform a specific
task is known as a function.
R provides a series of in-built functions, and it allows the user to
create their own functions.
Functions are used to perform tasks in the modular approach.
Functions are used to avoid repeating the same task and to reduce
complexity.
To understand and maintain our code, we logically break it into smaller
parts using the function.
A function should be
1. Written to carry out a specified task.
2. May or may not have arguments
3. Contain a body in which our code is written.
4. May or may not return one or more output values.
Syntax:
function_name <- function(arg_1, arg_2, ...) {
Function body
Example:
my_function <- function() {
print("Hello World!")
}
Output:
"Hello World!"
Function Components
The different parts of a function are −
Function Name − This is the actual name of the function. It is stored in R
environment as an object with this name.
Arguments − An argument is a placeholder. When a function is invoked, you
pass a value to the argument. Arguments are optional; that is, a function may
contain no arguments. Also arguments can have default values.
Function Body − The function body contains a collection of statements that
defines what the function does.
Return Value − The return value of a function is the last expression in the
function body to be evaluated.
R has many in-built functions which can be directly called in the program without
defining them first. We can also create and use our own functions referred as user
defined functions.
Return value
It is the last expression in the function body which is to be evaluated.
Function Types:
Similar to the other languages, R also has two types of function,
i.e. Built-in Function and User-defined Function.
In R, there are lots of built-in functions which we can directly call in
the program without defining them.
R also allows us to create our own functions.
Built-in functions
The functions which are already created or defined in the programming
framework are known as built-in functions.
User doesn't need to create these types of functions, and these
functions are built into an application.
End-users can access these functions by simply calling it.
R have different types of built-in functions such as cat(), print(), seq(),
mean(), max(), sum(x) and paste(…) etc.
R has a rich set of functions that can be used to perform almost every
task for the user.
These built-in functions are divided into the following categories based
on their functionality.
1. Math functions
2. Character functions
3. Statistical probability
4. Other statistical function
Math Functions: R provides the various mathematical functions to perform
the mathematical calculation. These mathematical functions are very helpful
to find absolute value, square value and much more calculations. In R, there
are the following functions which are used:
S.No. Function Description Example
1. abs(x) It returns the absolute value of input x. x<- -4
print(abs(x))
Output
[1] 4
2. sqrt(x) It returns the square root of input x. x<- 4
print(sqrt(x))
Output
[1] 2
3. ceiling(x) It returns the smallest integer which is x<- 4.5
larger than or equal to x. print(ceiling(x))
Output
[1] 5
4. floor(x) It returns the largest integer, which is x<- 2.5
smaller than or equal to x. print(floor(x))
Output
[1] 2
5. trunc(x) It returns the truncate value of input x. x<-
c(1.2,2.5,8.1)
print(trunc(x))
Output
[1] 1 2 8
6. round(x, It returns round value of input x. x<- 4.23456
digits=n) print(round(x,
digits=4)
Output
4.2346
7. cos(x), It returns cos(x), sin(x) value of input x<- 4
sin(x), x. print(cos(x))
tan(x) print(sin(x))
print(tan(x))
Output
[1] -06536436
[2] -0.7568025
[3] 1.157821
8. log(x) It returns natural logarithm of input x. x<- 4
print(log(x))
Output
[1] 1.386294
9. log10(x) It returns common logarithm of input x<- 4
x. print(log10(x))
Output
[1] 0.60206
10 exp(x) It returns exponent. x<- 4
print(exp(x))
Output
[1] 54.59815
Example:
# Create a sequence of numbers from 32 to 44.
print(seq(32,44))
# Find mean of numbers from 25 to 82.
print(mean(25:82))
# Find sum of numbers frm 41 to 68.
print(sum(41:68))
Output:
[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526
String Function: R provides various string functions to perform tasks.
These string functions allow us to extract sub string from string, search
pattern etc. There are the following string functions in R:
S. Function Description Example
No.
1. substr(x, It is used to extract a <- "987654321"
start=n1,stop=n2) substrings in a character substr(a, 3, 5)
vector.
Output
[1] "765"
2. grep(pattern, x , It searches for pattern in x. st1 <-
ignore.case=FALSE, c('abcd','bdcd','abcdabcd')
fixed=FALSE) pattern<- '^abc'
print(grep(pattern, st1))
Output
[1] 1 3
3. sub(pattern, It finds pattern in x and st1<- "England is beautiful
replacement,x,ignore replaces it with replacement but not the part of EU"
case=FALSE, (new) text. sub("England', "UK", st1)
fixed=FALSE)
Output
[1] "UK is beautiful but not a
part of EU"
gsub(pattern, It finds every occurrence of st1<- "India is beautiful but
replacement,x,ignore pattern in x and replaces it India is not the part of
case=FALSE) with replacement (new) Europe"
text. sub("India”, "UK", st1)
Output
[1] " UK is beautiful but UK is
not the part of Europe "
4. paste(..., sep="") It concatenates strings after paste('one',2,'three',4,'five')
using sep string to separate
them. Output
[1] one 2 three 4 five
5. strsplit(x, split) It splits the elements of a<-"Split all the character"
character vector x at split print(strsplit(a, " "))
point.
Output
[[1]]
[1] "split" "all" "the"
"character"
6. tolower(x) It is used to convert the st1<- "shuBHAm"
string into lower case. print(tolower(st1))
Output
[1] shubham
7. toupper(x) It is used to convert the st1<- "shuBHAm"
string into upper case. print(toupper(st1))
Output
[1] SHUBHAM
User-defined Function
R allows us to create our own function in our program.
A user defines a user-define function to fulfill the requirement of user.
Once these functions are created, we can use these functions like in-
built function.
Calling a Function without an Argument:
Example:
new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}
new.function()
Output:
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
Function calling with an argument:
We can easily call a function by passing an appropriate argument in the
function.
Example:
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
new.function(6)
Output:
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
Example:
my_function <- function(fname) {
paste(fname, "Griffin")
}
my_function("Peter")
my_function("Lois")
my_function("Stewie")
Output:
[1] "Peter Griffin"
[1] "Lois Griffin"
[1] "Stewie Griffin"
Return Values:
To let a function return a result, use the return() function:
Example:
evenOdd = function(x){
if(x %% 2 == 0)
return("even")
else
return("odd")
}
print(evenOdd(4))
print(evenOdd(3))
Output:
[1] "even"
[1] "odd"
Example:
areaOfCircle = function(radius){
area = pi*radius^2
return(area)
}
print(areaOfCircle(2))
Output:
[1] 12.56637
Calling a Function with Argument Values (by position and by name):
The arguments to a function call can be supplied in the same sequence as
defined in the function or they can be supplied in a different sequence but
assigned to the names of the arguments.
Example:
new.function <- function(a,b,c) {
result <- a * b + c
print(result)
}
# Call the function by position of arguments.
new.function(5,3,11)
# Call the function by names of the arguments.
new.function(b = 10, c = 5, a = 3)
Output:
[1] 26
[1] 35
Calling a Function with Default Argument:
We can define the value of the arguments in the function definition and call
the function without supplying any argument to get the default result. But
we can also call such functions by supplying new values of the argument and
get non default result.
Example:
new.function <- function(a = 3, b = 6) {
result <- a * b
print(result)
}
new.function()
new.function(9,5)
Output:
[1] 45
Example:
my_function <- function(country = "Norway") {
paste("I am from", country)
}
my_function("Sweden")
my_function("India")
my_function() # will get the default value, which is Norway
my_function("USA")
Output:
[1] "I am from Sweden"
[1] "I am from India"
[1] "I am from Norway"
[1] "I am from USA"
Example:
Rectangle = function(length=5, width=4){
area = length * width
return(area)
}
print(Rectangle(2, 3))
print(Rectangle(width = 8, length = 4))
print(Rectangle())
Output:
[1] 6
[1] 32
[1] 20
Lazy Evaluation of Function:
Arguments to functions are evaluated lazily, which means so they are
evaluated only when needed by the function body
Example:
new.function <- function(a, b) {
print(a^2)
print(a)
print(b)
}
new.function(6)
Output:
[1] 36
[1] 6
Error in print(b) : argument "b" is missing, with no default
Inline Functions:
Sometimes creating an R script file, loading it, executing it is a lot of work
when you want to just create a very small function. So, what we can do in
this kind of situation is an inline function.
To create an inline function you have to use the function command with
the argument x and then the expression of the function.
Example:
f = function(x) x^2*4+x/3
print(f(4))
print(f(-2))
print(0)
Output:
[1] 65.33333
[1] 15.33333
[1] 0
Recursive Functions: Recursion is when the function calls itself. This
forms a loop, where every time the function is called, it calls itself again
and again and this technique is known as recursion.
Key Features of R Recursion:
The use of recursion, often, makes the code shorter and it also looks
clean.
It is a simple solution for a few cases.
Example:
rec_fac <- function(x){
if(x==0 || x==1)
{
return(1)
}
else
{
return(x*rec_fac(x-1))
}
}
rec_fac(4)
Output:
[1] 24
Global Variables:
Variables that are created outside of a function are known
as global variables.
Global variables can be used by everyone, both inside of functions and
outside.
Example:
txt <- "awesome"
my_function <- function() {
paste("R is", txt)
}
my_function()
Output:
[1] "R is awesome"
If you create a variable with the same name inside a function, this
variable will be local, and can only be used inside the function. The
global variable with the same name will remain as it was, global and
with the original value.
Example:
txt <- "global variable"
my_function <- function() {
txt = "fantastic"
paste("R is", txt)
}
my_function()
txt
Output:
[1] "R is fantastic"
[1] "global variable"
The Global Assignment Operator:Normally, when you create a variable
inside a function, that variable is local, and can only be used inside that
function.
To create a global variable inside a function, you can use the global
assignment operator <<-
Example:
my_function <- function() {
txt <<- "fantastic"
paste("R is", txt)
}
my_function()
print(txt)
Output:
[1] "R is fantastic"
[1] "fantastic"
To change the value of a global variable inside a function, refer to the
variable by using the global assignment operator <<-:
Example:
txt <- "awesome"
my_function <- function() {
txt <<- "fantastic"
paste("R is", txt)
}
my_function()
paste("R is", txt)
Output:
[1] "R is fantastic"
[1] "R is fantastic"
Nested Functions:
There are two ways to create a nested function:
Call a function within another function.
Write a function within a function.
Call a function within another function
Example:
sq.root <- function(x) {
root <- x^(1/2)
return(root)
}
sq.root(sq.root(25))
Output:
[1] 2.236068
Example:
Nested_function <- function(x, y) {
a <- x + y
return(a)
}
Nested_function(Nested_function(2,2), Nested_function(3,3))
Output:
[1] 10
Write a function within a function
You cannot directly call the Inner function because it has been defined
(nested) inside the Outer function.
We need to call Outer function first in order to call Inner function as a
second step.
Example:
Outer_func <- function(x) {
Inner_func <- function(y) {
a <- x + y
return(a)
}
}
z=Outer_func(5)
z(9)
Output:
[1] 14
Example:
num1 <- function(x) {
num2 <- function(y) {
return(x*y)
}
return(num2)
}
result <- num1(10)
print(result(9))
Output:
[1] 90