0% found this document useful (0 votes)
39 views57 pages

R Programming Basics and Control Structures

The document provides an overview of the R programming language, detailing its features, operators, and control structures for programming. It covers basic operations, data structures, and various types of control statements such as loops and conditionals. Additionally, it explains the types of operators available in R, including arithmetic, logical, relational, and assignment operators.

Uploaded by

sumathiv28
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)
39 views57 pages

R Programming Basics and Control Structures

The document provides an overview of the R programming language, detailing its features, operators, and control structures for programming. It covers basic operations, data structures, and various types of control statements such as loops and conditionals. Additionally, it explains the types of operators available in R, including arithmetic, logical, relational, and assignment operators.

Uploaded by

sumathiv28
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 V R LANGUAGE ​ ​

Overview, Programming structures: Control statements - Operators - Functions -


Environment and scope issues - Recursion -Replacement functions, R data structures:
Vectors - Matrices and arrays - Lists -Data frames -Classes, Input/output, String
manipulations

OVERVIEW OF R PROGRAMMING
R is a programming language and software environment for statistical analysis,
graphics representation and reporting. R was created by Ross Ihaka and Robert
Gentleman at the University of Auckland, New Zealand, and is currently developed by
the R Development Core Team.
The core of R is an interpreted computer language which allows branching and
looping as well as modular programming using functions. R allows integration with the
procedures written in the C, C++, .Net, Python or FORTRAN languages for efficiency.
R is freely available under the GNU General Public License, and pre-compiled
binary versions are provided for various operating systems like Linux, Windows and
Mac. R is free software distributed under a GNU-style copy left, and an official part of
the GNU project called GNU S.
Evolution of R
R was initially written by Ross Ihaka and Robert Gentleman at the Department
of Statistics of the University of Auckland in Auckland, New Zealand. R made its first
appearance in 1993.
●​ A large group of individuals has contributed to R by sending code and bug reports.
●​ Since mid-1997 there has been a core group (the "R Core Team") who can modify the
R source code archive.
Features of R
As stated earlier, R is a programming language and software environment for statistical
analysis, graphics representation and reporting. The following are the important features
of R
●​ R is a well-developed, simple and effective programming language which includes
conditionals, loops, user defined recursive functions and input and output facilities.
●​ R has an effective data handling and storage facility,
●​ R provides a suite of operators for calculations on arrays, lists, vectors and matrices.
●​ R provides a large, coherent and integrated collection of tools for data analysis.
●​ R provides graphical facilities for data analysis and display either directly at the
computer or printing at the papers.
Basic operations of R
1+1​ ​ [1] 2
2*3​ ​ [1] 6
1==1​ ​ [1] TRUE
3<4​ ​ [1] TRUE
2+2==5​​ [1] FALSE

Variables
R is case Sensitive

x<-42
x/2
> x<-42 x> x/2
x<-"ABC" [1] 21
>x >x
[1] "ABC" [1] 4

Control statements are expressions used to control the execution and flow of the
program based on the conditions provided in the statements. These structures are used to
make a decision after assessing the variable. In this article, we’ll discuss all the control
statements with the examples.
In R programming, there are 8 types of control statements as follows:
●​ if condition
●​ if-else condition
●​ for loop
●​ nested loops
●​ while loop
●​ repeat and break statement
●​ return statement
●​ next statement
if condition
This control structure checks if the expression provided in parenthesis is true or
not. If true, the execution of the statements in braces {} continues.
Syntax:
if(expression){
statements
....
....
}
Example:
x <- 100

if(x > 10){


print(paste(x, "is greater than 10"))
}
Output:
[1] "100 is greater than 10"
if-else condition
It is similar to if condition but when the test expression in if condition fails, then
statements in else condition are executed.
Syntax:
if(expression){
statements
....
....
}
else{
statements
....
....
}
Example:
x <- 5
# Check value is less than or greater than 10
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"
for loop
It is a type of loop or sequence of statements executed repeatedly until exit
condition is reached.
Syntax:
for(value in vector){
statements
....
....
}
Example:
x <- letters[4:10]
for(i in x){
print(i)
}
Output:
[1] "d"
[1] "e"
[1] "f"
[1] "g"
[1] "h"
[1] "i"
[1] "j"
Nested loops
Nested loops are similar to simple loops. Nested means loops inside loop.
Moreover, nested loops are used to manipulate the matrix.
Example:
# Defining matrix
m <- matrix(2:15, 2)
for (r in seq(nrow(m))) {
for (c in seq(ncol(m))) {
print(m[r, c])
}
}
Output:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10
[1] 12
[1] 14
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
[1] 13
[1] 15
while loop
while loop is another kind of loop iterated until a condition is satisfied. The testing
expression is checked first before executing the body of loop.
Syntax:
while(expression){
statement
....
....
}
Example:
x=1
# Print 1 to 5
while(x <= 5){
print(x)
x=x+1
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
repeat loop and break statement
repeat is a loop which can be iterated many number of times but there is no exit
condition to come out from the loop. So, break statement is used to exit from the loop.
break statement can be used in any type of loop to exit from the loop.
Syntax:
repeat {
statements
....
....
if(expression) {
break
}
}
Example:
x=1

# Print 1 to 5
repeat{
print(x)
x=x+1
if(x > 5){
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
return statement
return statement is used to return the result of an executed function and returns
control to the calling function.
Syntax:
return(expression)

Example:
func <- function(x){
if(x > 0){
return("Positive")
}else if(x < 0){
return("Negative")
}else{
return("Zero")
}
}
func(1)
func(0)
func(-1)
Output:
[1] "Positive"
[1] "Zero"
[1] "Negative"
next statement
next statement is used to skip the current iteration without executing the further
statements and continues the next iteration cycle without terminating the loop.
Example:
# Defining vector
x <- 1:10
# Print even numbers
for(i in x){
if(i%%2 != 0){
next #Jumps to next loop
}
print(i)
}
Output:
[1] 2
[1] 4
[1] 6
[1] 8
[1] 10

OPERATORS
R supports majorly four kinds of binary operators between a set of operands. In
this article, we will see various types of operators in R Programming language and their
usage.
Types of the operator in R language
●​ Arithmetic Operators
●​ Logical Operators
●​ Relational Operators
●​ Assignment Operators
●​ Miscellaneous Operators
Arithmetic Operators
Arithmetic Operators modulo using the specified operator between operands,
which may be either scalar values, complex numbers, or vectors. The R operators are
performed element-wise at the corresponding positions of the vectors.
1.​ Addition operator (+)
The values at the corresponding positions of both operands are added. Consider
the following R operator snippet to add two vectors:
a <- c (1, 0.1)
b <- c (2.33, 4)
print (a+b)
Output : 3.33 4.10
2.​ Subtraction Operator (-)
The second operand values are subtracted from the first. Consider the following R
operator snippet to subtract two variables:
a <- 6
b <- 8.4
print (a-b)
Output : -2.4
3.​ Multiplication Operator (*)
The multiplication of corresponding elements of vectors and Integers are
multiplied with the use of the ‘*’ operator.
B= c(4,4)
C= c(5,5)
print (B*C)
Output : 20 20
4.​ Division Operator (/)
The first operand is divided by the second operand with the use of the ‘/’ operator.
a <- 10
b <- 5
print (a/b)
Output : 2
5.​ Power Operator (^)
The first operand is raised to the power of the second operand.
a <- 4
b <- 5
print(a^b)
Output : 1024
6.​ Modulo Operator (%%)
The remainder of the first operand divided by the second operand is returned.
list1<- c(2, 22)
list2<-c(2,4)
print(list1 %% list2)
Output : 0 2
# R program to illustrate the use of Arithmetic operators
vec1 <- c(0, 2)
vec2 <- c(2, 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 ("Modulo of vectors :", vec1 %% vec2, "\n")
cat ("Power operator :", vec1 ^ vec2)

Output
Addition of vectors : 2 5
Subtraction of vectors : -2 -1
Multiplication of vectors : 0 6
Division of vectors : 0 0.6666667
Modulo of vectors : 0 2
Power operator : 0 8

Logical Operators
Logical Operators in R simulate element-wise decision operations, based on the
specified operator between the operands, which are then evaluated to either a True or
False boolean value. Any non-zero integer value is considered as a TRUE value, be it a
complex or real number.
1.​ Element-wise Logical AND operator (&)
Returns True if both the operands are True.
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1 & list2)
Output : FALSE TRUE
Any non zero integer value is considered as a TRUE value, be it complex or real
number.
2.​ Element-wise Logical OR operator (|)
Returns True if either of the operands is True.
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1|list2)
Output : TRUE TRUE
NOT operator (!)
A unary operator that negates the status of the elements of the operand.
list1 <- c(0,FALSE)
print(!list1)
Output : TRUE TRUE
3.​ Logical AND operator (&&)
Returns True if both the first elements of the operands are True.
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1[1] && list2[1])
Output : FALSE
Compares just the first elements of both the lists.
4.​ Logical OR operator (||)
Returns True if either of the first elements of the operands is True.
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1[1]||list2[1])
Output : TRUE

# R program to illustrate the use of Logical operators


vec1 <- c(0,2)
vec2 <- c(TRUE,FALSE)
# Performing operations on Operands
cat ("Element wise AND :", vec1 & vec2, "\n")
cat ("Element wise OR :", vec1 | vec2, "\n")
cat ("Logical AND :", vec1[1] && vec2[1], "\n")
cat ("Logical OR :", vec1[1] || vec2[1], "\n")
cat ("Negation :", !vec1)
Output
Element wise AND : FALSE FALSE
Element wise OR : TRUE TRUE
Logical AND : FALSE
Logical OR : TRUE
Negation : TRUE FALSE

Relational Operators
The Relational Operators in R carry out comparison operations between the
corresponding elements of the operands. Returns a boolean TRUE value if the first
operand satisfies the relation compared to the second. A TRUE value is always
considered to be greater than the FALSE.
1.​ Less than (<)
Returns TRUE if the corresponding element of the first operand is less than that of
the second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1,"apple")
list2 <- c(0,0.1,"bat")
print(list1<list2)
Output : FALSE FALSE TRUE
2.​ Less than equal to (<=)
Returns TRUE if the corresponding element of the first operand is less than or
equal to that of the second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1, "apple")
list2 <- c(TRUE, 0.1, "bat")
# Convert lists to character strings
list1_char <- [Link](list1)
list2_char <- [Link](list2)
# Compare character strings
print(list1_char <= list2_char)
Output : TRUE TRUE TRUE
3.​ Greater than (>)
Returns TRUE if the corresponding element of the first operand is greater than that
of the second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1, "apple")
list2 <- c(TRUE, 0.1, "bat")
print(list1_char > list2_char)
Output : FALSE FALSE FALSE
4.​ Greater than equal to (>=)
Returns TRUE if the corresponding element of the first operand is greater or equal
to that of the second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1, "apple")
list2 <- c(TRUE, 0.1, "bat")
print(list1_char >= list2_char)
Output : TRUE TRUE FALSE
5.​ Not equal to (!=)
Returns TRUE if the corresponding element of the first operand is not equal to the
second operand. Else returns FALSE.
list1 <- c(TRUE, 0.1,'apple')
list2 <- c(0,0.1,"bat")
print(list1!=list2)
Output : TRUE FALSE TRUE
The following R code illustrates the usage of all Relational Operators in R:
# R program to illustrate the use of Relational operators
vec1 <- c(0, 2)
vec2 <- c(2, 3)
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")
Output
Vector1 less than Vector2 : TRUE TRUE
Vector1 less than equal to Vector2 : TRUE TRUE
Vector1 greater than Vector2 : FALSE FALSE
Vector1 greater than equal to Vector2 : FALSE FALSE
Vector1 not equal to Vector2 : TRUE TRUE
Assignment Operators
Assignment Operators in R are used to assign values to various data objects in R.
The objects may be integers, vectors, or functions. These values are then stored by the
assigned variable names. There are two kinds of assignment operators: Left and Right
1.​ Left Assignment (<- or <<- or =)
Assigns a value to a vector.
vec1 = c("ab", TRUE)
print (vec1)
Output : "ab" "TRUE"
2.​ Right Assignment (-> or ->>)
Assigns value to a vector.
c("ab", TRUE) ->> vec1
print (vec1)
Output : "ab" "TRUE"
# R program to illustrate the use of Assignment operators
vec1 <- c(2:5)
c(2:5) ->> vec2
vec3 <<- c(2:5)
vec4 = c(2:5)
c(2:5) -> vec5
# Performing operations on Operands
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
Miscellaneous Operator are the mixed operators in R that simulate the printing of
sequences and assignment of vectors, either left or right-handed.
1.​ %in% Operator
Checks if an element belongs to a list and returns a boolean value TRUE if the
value is present else FALSE.
val <- 0.1
list1 <- c(TRUE, 0.1,"apple")
print (val %in% list1)
Output : TRUE
Checks for the value 0.1 in the specified list. It exists, therefore, prints TRUE.
2.​ %*% Operator
This operator is used to multiply a matrix with its transpose. Transpose of the
matrix is obtained by interchanging the rows to columns and columns to rows. The
number of columns of the first matrix must be equal to the number of rows of the second
matrix. Multiplication of the matrix A with its transpose, B, produces a square matrix. ​
Ar∗c x Bc∗r−> Pr∗r
​mat = matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
print (mat)
print( t(mat))
pro = mat %*% t(mat)
print(pro)
Output :
[,1] [,2] [,3] #original matrix of order 2x3
[1,] 1 3 5
[2,] 2 4 6
[,1] [,2] #transposed matrix of order 3x2
[1,] 1 2
[2,] 3 4
[3,] 5 6
[,1] [,2] #product matrix of order 2x2
[1,] 35 44
[2,] 44 56
The following R code illustrates the usage of all Miscellaneous Operators in R:
# R program to illustrate the use of Miscellaneous operators
mat <- matrix (1:4, nrow = 1, ncol = 4)
print("Matrix elements using : ")
print(mat)
product = mat %*% t(mat)
print("Product of matrices")
print(product,)
cat ("does 1 exist in prod matrix :", "1" %in% product)
Output
[1] "Matrix elements using : "
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[1] "Product of matrices"
[,1]
[1,] 30
does 1 exist in prod matrix : FALSE

FUNCTIONS IN R PROGRAMMING
A function accepts input arguments and produces the output by executing valid R
commands that are inside the function. Functions are useful when you want to perform a
certain task multiple times. In R Programming Language when you are creating a
function the function name and the file in which you are creating the function need not be
the same and you can have one or more functions in R.
Creating a Function in R Programming
Functions are created in R by using the command function(). The general structure of the
function file is as follows:

Parameters or Arguments in R Functions:


Parameters and arguments are the same term in functions. Parameters or
arguments are the values passed into a function. A function can have any number of
arguments, they are separated by comma in parentheses.
Example:
add_num <- function(a,b)
{
sum_result <- a+b
return(sum_result)
}
# calling add_num function
sum = add_num(35,34)
#printing result
print(sum)
Output
[1] 69
No. of Parameters:
Function should be called with right no. of parameters, neither less nor more or
else it will give error.
Default Value of Parameter:
Some functions have default values, and you can also give default value in your
user-defined functions. These values are used by functions if user doesn’t pass any
parameter value while calling a function.
Return Value:
You can use the return() function if you want your function to return the result.
Calling a Function in R
After creating a Function, you have to call the function to use it. Calling a function
in R is very easy, you can call a function by writing it’s name and passing possible
parameters value.
Passing Arguments to Functions in R Programming Language
There are several ways you can pass the arguments to the function:
●​ Case 1: Generally in R, the arguments are passed to the function in the same order as
in the function definition.
●​ Case 2: If you do not want to follow any order what you can do is you can pass the
arguments using the names of the arguments in any order.
●​ Case 3: If the arguments are not passed the default values are used to execute the
function.
# A simple R program to demonstrate passing arguments to a function
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
Types of Function in R Language
●​ Built-in Function: Built-in functions in R are pre-defined functions that are available
in R programming languages to perform common tasks or operations.
●​ User-defined Function: R language allows us to write our own function.
Built-in Function in R Programming Language
Built-in Function are the functions that are already existing in R language and you
just need to call them to use.
Here we will use built-in functions like sum(), max() and min().
# Find sum of numbers 4 to 6.
print(sum(4:6))
# Find max of numbers 4 and 6.
print(max(4:6))
# Find min of numbers 4 and 6.
print(min(4:6))
Output
[1] 15
[1] 6
[1] 4
Other Built-in Functions in R:
Functions Syntax

MATHEMATICAL FUNCTIONS

abs() calculates a number’s absolute value.

sqrt() calculates a number’s square root.

round() rounds a number to the nearest integer.

exp() calculates a number’s exponential value

log() which calculates a number’s natural logarithm.

cos(), sin(), and tan() calculates a number’s cosine, sine, and tang

STATISTICAL FUNCTIONS

mean() A vector’s arithmetic mean is determined by the


mean() function.

median() A vector’s median value is determined by the


median() function.

cor() calculates the correlation between two vectors.

var() calculates the variance of a vector and calculates the


standard deviation of a vector.
DATA MANIPULATION FUNCTIONS

unique() returns the unique values in a vector.

subset() subsets a data frame based on conditions.

aggregate() groups data according to a grouping variable.

order() uses ascending or descending order to sort a vector.

FILE INPUT / OUTPUT FUNCTIONS

[Link]() reads information from a CSV file.

[Link]() publishes information to write a CSV file.

read. table() reads information from a tabular.

[Link]() creates a tabular file with data.

User-defined Functions in R Programming Language


User-defined functions are the functions that are created by the user. User defines
the working, parameters, default parameter, etc. of that user-defined function. They can
be only used in that specific code.
Example
evenOdd = function(x)
{
if(x %% 2 == 0)
return("even")
else
return("odd")
}
print(evenOdd(4))
print(evenOdd(3))
Output
[1] "even"
[1] "odd"
Multiple Input Multiple Output
The functions in R Language take multiple input objects but returned only one
object as output, this is, however, not a limitation because you can create lists of all the
outputs which you want to create and once the list is created you can access them into the
elements of the list and get the answers which you want.
Let us consider this example to create a function “Rectangle” which takes “length”
and “width” of the rectangle and returns area and perimeter of that rectangle. Since R
Language can return only one object. Hence, create one object which is a list that
contains “area” and “perimeter” and return the list.
Rectangle = function(length, width)
{
area = length * width
perimeter = 2 * (length + width)
# create an object called result which is
# a list of area and perimeter
result = list("Area" = area, "Perimeter" = perimeter)
return(result)
}
resultList = Rectangle(2, 3)
print(resultList["Area"])
print(resultList["Perimeter"])
Output
$Area
[1] 6
$Perimeter
[1] 10
Inline Functions in R
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

ENVIRONMENT AND SCOPE ISSUES


The environment is a virtual space that is triggered when an interpreter of a
programming language is launched. Simply, the environment is a collection of all the
objects, variables, and functions. Or, Environment can be assumed as a top-level object
that contains the set of names/variables associated with some values. In this article, let us
discuss creating a new environment in R programming, listing all environments,
removing a variable from the environment, searching for a variable or function among
environments and function environments with the help of examples.
Environment Differ from the List?
●​ Every object in an environment has a name.
●​ The environment has a parent environment.
●​ Environments follow reference semantics.
Create a New Environment
An environment in R programming can be created using [Link]() function.
Further, the variables can be accessed using the $ or [[ ]] operator. But, each variable is
stored in different memory locations. There are four special environments: globalenv(),
baseenv(), emptyenv() and environment()
Syntax: [Link](hash = TRUE)
Parameters: ​
hash: indicates logical value. If TRUE, environments uses a hash table
To know about more optional parameters, use below command in console:
help(“[Link]”)
# R program to illustrate Environments in R
# Create new environment
newEnv <- [Link]()
# Assigning variables
newEnv$x <- 1
newEnv$y <- "GFG"
newEnv$z <- 1:10
# Print
print(newEnv$z)
Output:
[1] 1 2 3 4 5 6 7 8 9 10
List all Environments
Every environment has a parent environment but there is an empty environment
that does not have any parent environment. All the environments can be listed using ls()
function and search() function. ls() function also list out all the bindings of the variables
in a particular environment.
Syntax:
ls() ​
search()
Parameters:​
These functions need no argument
# R program to illustrate Environments in R

# Prints all the bindings and environments


# attached to Global Environment
ls()
# Prints bindings of newEnv
ls(newEnv)
# Lists all the environments of the parent environment
search()
Output:
[1] "al" "e" "e1" "f" "newEnv" "pts" "x" "y"
[9] "z"
[1] "x" "y" "z"
[1] ".GlobalEnv" "package:stats" "package:graphics"
[4] "package:grDevices" "package:utils" "package:datasets"
[7] "package:methods" "Autoloads" "package:base"
Removing a Variable From an Environment
A variable in an environment is deleted using rm() function. It is different from
deleting entries from lists as entries in lists are set as NULL to be deleted. But, using rm()
function, bindings are removed from the environment.
Syntax: rm(…)
Parameters: ​
…: indicates list of objects
Example:
# R program to illustrate Environments in R
# Remove newEnv
rm(newEnv)
# List
ls()
Output:
[1] "al" "e" "e1" "f" "pts" "x" "y" "z"
Search a Variable or Function Among Environments
A variable or a function can be searched in R programming by using where()
function among all the environments and packages present. where() function is present in
pryr package. This function takes only two arguments, the name of the object to search
for and the environment from where to start the search.
Syntax: where(name)
Parameters: ​
name: indicates object to look for
Example:
# R program to illustrate Environments in R
# Install pryr package
[Link]("pryr")
# Load the package
library(pryr)
# Search
where("x")
where("mode")
Output:
<environment: R_GlobalEnv>
<environment: base>
Scope of Variable in R
Variables:
In R, variables are the containers for storing data values. They are reference, or pointers,
to an object in memory which means that whenever a variable is assigned to an instance,
it gets mapped to that instance. A variable in R can store a vector, a group of vectors or a
combination of many R objects.
Example:
# R program to demonstrate variable assignment
# Assignment using equal operator
var1 = c(0, 1, 2, 3)
print(var1)
# Assignment using leftward operator
var2 <- c("Python", "R")
print(var2)
# A Vector Assignment
a = c(1, 2, 3, 4)
print(a)
b = c("Debi", "Sandeep", "Subham", "Shiba")
print(b)
# A group of vectors Assignment using list
c = list(a, b)
print(c)
Output:
[1] 0 1 2 3
[1] "Python" "R"
[1] 1 2 3 4
[1] "Debi" "Sandeep" "Subham" "Shiba"
[[1]]
[1] 1 2 3 4
[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"
Scope of a variable
The location where we can find a variable and also access it if required is called the scope
of a variable. There are mainly two types of variable scopes:
●​ Global Variables: Global variables are those variables that exist throughout the
execution of a program. It can be changed and accessed from any part of the program.
●​ Local Variables: Local variables are those variables that exist only within a certain
part of a program like a function and are released when the function call ends.

Global Variable
As the name suggests, Global Variables can be accessed from any part of the
program. They are available throughout the lifetime of a program. They are declared
anywhere in the program outside all of the functions or blocks. Declaring global
variables: Global variables are usually declared outside of all of the functions and blocks.
They can be accessed from any portion of the program.
# R program to illustrate usage of global variables
# global variable
global = 5
# global variable accessed from within a function
display = function()
{
print(global)
}
display()

# changing value of global variable


global = 10
display()
Output:
[1] 5
[1] 10
Local Variable
Variables defined within a function or block are said to be local to those functions.
Local variables do not exist outside the block in which they are declared, i.e. they can not
be accessed or used outside that block. Local variables are declared inside a block.
Example:
# R program to illustrate usage of local variables
func = function()
{
# this variable is local to the function func() and cannot be accessed outside this
function
age = 18​
}
print(age)
Output:
Error in print(age) : object 'age' not found
Accessing Global Variables
Global Variables can be accessed from anywhere in the code unlike local variables
that have a scope restricted to the block of code in which they are created.
Example:
f = function() {
# a is a local variable here
a <-1
}
f()
# Can't access outside the function
print(a) # This'll give error
Output:
Error in print(a) : object 'a' not found

RECURSION
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. Since the loops increase the memory we use the recursion. The recursive
function uses the concept of recursion to perform iterative tasks they call themselves,
again and again, which acts as a loop. These kinds of functions need a stopping condition
so that they can stop looping continuously. Recursive functions call themselves. They
break down the problem into smaller components. The function() calls itself within the
original function() on each of the smaller components. After this, the results will be put
together to solve the original problem.
Example 1: Factorial using Recursion in R
rec_fac <- function(x)
{
if(x==0 || x==1)
{
return(1)
}
else
{
return(x*rec_fac(x-1))
}
}
rec_fac(5)
Output:
[1] 120

Example 2: Sum of Series Using Recursion


Recursion in R is most useful for finding the sum of self-repeating series. In this
example, we will find the sum of squares of a given series of numbers. Sum =
12+22+…+N2
sum_series <- function(vec)
{
if(length(vec)<=1)
{
return(vec^2)
}
else
{
return(vec[1]^2+sum_series(vec[-1]))
}
}
series <- c(1:10)
sum_series(series)
Output:
[1] 385
Example 3: Sum of n numbers Using Recursion
sum_n <- function(n)
{
if (n == 1) {
return(1)
} else {
return(n + sum_n(n-1))
}
}
# Test the sum_n function
sum_n(5)
Output:
[1] 15
In this example, the sum_n function recursively increases n until it reaches 1,
which is the base case of the recursion, by adding the current value of n to the sum of the
first n-1 values.
Example 4: Finding the exponentiation Using Recursion
exp_n <- function(base, n)
{
if (n == 0) {
return(1)
} else {
return(base * exp_n(base, n-1))
}
}
# Test the exp_n function
exp_n(4, 5)
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.
●​ It expresses itself in a function that calls itself.
Applications of Recursion in R
Recursive functions are used in many efficient programming techniques like
dynamic programming language(DSL) or divide-and-conquer algorithms. In dynamic
programming, for both top-down as well as bottom-up approaches, recursion is vital for
performance. In divide-and-conquer algorithms, we divide a problem into smaller
subproblems that are easier to solve. The output is then built back up to the top.
Recursion has a similar process, which is why it is used to implement such algorithms. In
its essence, recursion is the process of breaking down a problem into many smaller
problems, these smaller problems are further broken down until the problem left is trivial.
The solution is then built back up piece by piece.
Types of Recursion in R
●​ Direct Recursion: The recursion that is direct involves a function calling itself
directly. This kind of recursion is the easiest to understand.
●​ Indirect Recursion: An indirect recursion is a series of function calls in which one
function calls another, which in turn calls the original function.
●​ Mutual Recursion: Multiple functions that call each other repeatedly make up
mutual recursion. To complete a task, each function depends on the others.
●​ Nested Recursion: Nested recursion happens when one recursive function calls
another recursively while passing the output of the first call as an argument. The
arguments of one recursion are nested inside of this one.
●​ Structural Recursion: Recursion that is based on the structure of the data is known
as structural recursion. It entails segmenting a complicated data structure into smaller
pieces and processing each piece separately.

REPLACEMENT FUNCTIONS
Replacement functions modify their arguments in place(modifying an R object
usually creates a copy). The name of replacement functions are always succeeded by <.
They must have arguments named x and value, and return the modified object. In case of
a replacement, a function needs additional arguments, the additional arguments should be
placed between x and value, and must be called with additional arguments on the left.
The name of the function has to be quoted as it is a syntactically valid but non-standard
name and the parser would interpret <- as the operator not as part of the function name if
it weren’t quoted.
Syntax:
“function_name<-” <- function(x, additional arguments, value) ​
{​
function body ​
}​
Example:
# R program to illustrate Replacement function
"replace<-" <- function(x, value)
{
x[1] = value
x
}
x = [Link](5, 7)
replace(x) = 0L
print(x)
Output:
[1] 0 5 5 5 5 5 5

R DATA STRUCTURES: VECTORS


The fundamental data type in R is the vector. R Vectors are the same as the arrays
in R language which are used to hold multiple data values of the same type. One major
key point is that in R Programming Language the indexing of the vector will start from
‘1’ and not from ‘0’. We can create numeric vectors and character vectors as well.

Creating a vector
A vector is a basic data structure that represents a one-dimensional array. To create an
array we use the “c” function which is the most common method used in R Programming
Language.
X<- c(61, 4, 21, 67, 89, 2)
cat('using c function', X, '\n')
# seq() function for creating
# a sequence of continuous values.
# [Link] defines the length of the vector.
Y<- seq(1, 10, [Link] = 5)
cat('using seq() function', Y, '\n')
# use':' to create a vector
# of continuous values.
Z<- 2:7
cat('using colon', Z)
Output:
using c function 61 4 21 67 89 2
using seq() function 1 3.25 5.5 7.75 10
using colon 2 3 4 5 6 7
Adding and Deleting Vector Elements
Vectors are stored like arrays in C, contiguously, and thus you cannot insert or delete
elements The size of a vector is determined at its creation, so if you wish to add or delete
elements, we need to reassign the vector.
> x <- c(88,5,12,13)
> x <- c(x[1:3],168,x[4])
# insert 168 before the 13
>x
[1] 88 5 12 168 13
Obtaining the Length of a Vector
We obtain the length of a vector by using the length() function:
> x <- c(1,2,4)
> length(x)
[1] 3
first1 <- function(x) {
for (i in 1:length(x)) {
if (x[i] == 1) break # break out of loop
}
return(i)
}
x <- c(88,1,12,13)
print(first1(x))
Output:
[1] 2
Matrices and Arrays as Vectors
Arrays and matrices are actually vectors. They merely have extra class attributes.
For example, matrices have the number of rows and columns.
>m
[,1] [,2]
[1,] 1 2
[2,] 3 4
> m + 10:13
[,1] [,2]
[1,] 11 14
[2,] 14 17
The 2-by-2 matrix m is stored as a four-element vector, column-wise, as (1,3,2,4).
We then added (10,11,12,13) to it, yielding (11,14,14,17), but R remembered that we
were working with matrices and thus gave the 2-by-2 result
Declarations
As with most scripting languages (such as Python and Perl), we do not declare variables
in R. For instance, consider this code:
z <- 3
This code, with no previous reference to z, is perfectly legal (and common-place).
However, if you reference specific elements of a vector, you must warn R. For instance,
say we wish y to be a two-component vector with values 5 and 12.
> y <- vector(length=2)
> y[1] <- 5
> y[2] <- 12
The following will also work:
> y <- c(5,12)
This approach is all right because on the right-hand side we are creating a new vector, to
which we then bind y. The reason we cannot suddenly spring an expression like y[2] on R
stems from R’s functional language nature. The reading and writing of individual vector
elements are actually handled by functions. If R doesn’t already know that y is a vector,
these functions have nothing on which to act. Speaking of binding, just as variables are
not declared, they are not constrained in terms of mode. The following sequence of
events is perfectly valid:
> x <- c(1,5)
>x
[1] 1 5
> x <- "abc"
First, x is associated with a numeric vector, then with a string.
Recycling
When applying an operation to two vectors that requires them to be the same
length, R automatically recycles, or repeats, the shorter one, until it is long enough to
match the longer one. Here is an example:
> c(1,2,4) + c(6,0,9,20,22)
[1] 7 2 13 21 24
longer object length is not a multiple of shorter object length in: c(1, 2, 4) + c(6,0, 9, 20,
22). The shorter vector was recycled, so the operation was taken to be as follows:
> c(1,2,4,1,2) + c(6,0,9,20,22)
[1] 7 2 13 21 24
>x
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
> x+c(1,2)
[,1] [,2]
[1,] 2 6
[2,] 4 6
[3,] 4 8
Again, keep in mind that matrices are actually long vectors. Here, x, as a 3-by-2 matrix,
is also a six-element vector, which in R is stored column by column. In other words, in
terms of storage, x is the same as c(1,2,3,4,5,6). We added a two-element vector to this
six-element one, so our added vector needed to be repeated twice to make six elements.
In other words, we were essentially doing this: x + c(1,2,1,2,1,2)
Common Vector Operations
Vector Arithmetic and Logical Operations
R is a functional language. Every operator, including + in the following example, is
actually a function.
> x <- c(1,2,4)
> x + c(5,0,-1)
[1] 6 2 3
When we multiply two vectors.
> x * c(5,0,-1)
[1] 5 0 -4

DATA FRAMES
Data Frames in R Language are generic data objects of R that are used to store
tabular data. Data frames can also be interpreted as matrices where each column of a
matrix can be of different data types. R DataFrame is made up of three principal
components, the data, rows, and columns.
R Data Frames Structure
As you can see in the image below, this is how a data frame is structured. The data
is presented in tabular form, which makes it easier to operate and understand.

Create Dataframe
To create an R data frame use [Link]() function and then pass each of the
vectors we have created as arguments to the function.
[Link] <- [Link](
friend_id = c(1:5),
friend_name = c("Sachin", "Sourav",
"Dravid", "Sehwag",
"Dhoni"),
stringsAsFactors = FALSE
)
# print the data frame
print([Link])
Output:
friend_id friend_name
1 1 Sachin
2 2 Sourav
3 3 Dravid
4 4 Sehwag
5 5 Dhoni

If the named argument stringsAsFactors is not specified, then by default,


stringsAsFactors will be TRUE. (You can also use options() to arrange the opposite
default.) This means that if we create a data frame from a character vector—in this case,
friend_name. R will convert that vector to a factor. Because our work with character data
will typically be with vectors rather than factors, we’ll set stringsAsFactors to FALSE.
Accessing Data Frames
Since d is a list, we can access it as such via component index values or component
names:
> kids <- c("Jack","Jill")
> ages <- c(12,10)
> d <- [Link](kids,ages,stringsAsFactors=FALSE)
> d # matrix-like viewpoint
kids ages
1 Jack 12
2 Jill 10
> d[[1]]
[1] "Jack" "Jill"
> d$kids
[1] "Jack" "Jill"
But we can treat it in a matrix-like fashion as well. For example, we can
view column 1:
> d[,1]
[1] "Jack" "Jill"
This matrix-like quality is also seen when we take d apart using str():
> str(d)
'[Link]': 2 obs. of 2 variables:
$ kids: chr "Jack" "Jill"
$ ages: num 12 10
Add Rows in R Data Frame
To add rows in a Data Frame, you can use a built-in function rbind().
Products <- [Link](
Product_ID = c(101, 102, 103),
Product_Name = c("T-Shirt", "Jeans", "Shoes"),
Price = c(15.99, 29.99, 49.99),
Stock = c(50, 30, 25)
)

# Print the existing dataframe


cat("Existing dataframe (Products):\n")
print(Products)
# Adding a new row for a new product
New_Product <- c(104, "Sunglasses", 39.99, 40)
Products <- rbind(Products, New_Product)
# Print the updated dataframe after adding the new product
cat("\nUpdated data frame after adding a new product:\n")
print(Products)
Output:
Existing dataframe (Products):
Product_ID Product_Name Price Stock
1 101 T-Shirt 15.99 50
2 102 Jeans 29.99 30
3 103 Shoes 49.99 25
Updated dataframe after adding a new product:
Product_ID Product_Name Price Stock
1 101 T-Shirt 15.99 50
2 102 Jeans 29.99 30
3 103 Shoes 49.99 25
4 104 Sunglasses 39.99 40
Add Columns in R Data Frame
To add columns in a Data Frame, you can use a built-in function cbind().
Products <- [Link](
Product_ID = c(101, 102, 103),
Product_Name = c("T-Shirt", "Jeans", "Shoes"),
Price = c(15.99, 29.99, 49.99),
Stock = c(50, 30, 25)
)
# Print the existing dataframe
cat("Existing dataframe (Products):\n")
print(Products)
# Adding a new column for 'Discount' to the dataframe
Discount <- c(5, 10, 8) # New column values for discount
Products <- cbind(Products, Discount)
# Rename the added column
colnames(Products)[ncol(Products)] <- "Offer" # Renaming the last column
# Print the updated dataframe after adding the new column
cat("\nUpdated dataframe after adding a new column 'Discount':\n")
print(Products)
Output:
Existing dataframe (Products):

Product_ID Product_Name Price Stock


1 101 T-Shirt 15.99 50
2 102 Jeans 29.99 30
3 103 Shoes 49.99 25
Updated dataframe after adding a new column 'Discount':
Product_ID Product_Name Price Stock Discount
1 101 T-Shirt 15.99 50 5
2 102 Jeans 29.99 30 10
3 103 Shoes 49.99 25 8
Renaming the last column
Product_ID Product_Name Price Stock Offer
1 101 T-Shirt 15.99 50 5
2 102 Jeans 29.99 30 10
3 103 Shoes 49.99 25 8

MATRICES
A matrix is a vector with two additional attributes: the number of rows and the
number of columns. Since matrices are vectors, they also have modes, such as numeric
and character.
Creating Matrices
To create a matrix in R you need to use the function called matrix(). The
arguments to this matrix() are the set of elements in the vector. You have to pass how
many numbers of rows and how many numbers of columns you want to have in your
matrix.
Note: By default, matrices are in column-wise order.
Syntax to Create R-Matrix
matrix(data, nrow, ncol, byrow, dimnames)
Parameters:
data – values you want to enter
nrow – no. of rows
ncol – no. of columns
byrow – logical clue, if ‘true’ value will be assigned by rows
dimnames – names of rows and columns
Example:
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
rownames(A) = c("a", "b", "c")
colnames(A) = c("c", "d", "e")
cat("The 3x3 matrix:\n")
print(A)
Output
The 3x3 matrix:
cde
a123
b456
c789
Creating Special Matrices in R
R allows the creation of various different types of matrices with the use of arguments
passed to the matrix() function.
1. Matrix where all rows and columns are filled by a single constant ‘k’:
To create such a R matrix the syntax is given below:
Syntax: matrix(k, m, n)
Parameters: ​
k: the constant ​
m: no of rows ​
n: no of columns
Example:
print(matrix(5, 3, 3))

Output
[,1] [,2] [,3]
[1,] 5 5 5
[2,] 5 5 5
[3,] 5 5 5
2. Diagonal matrix:
A diagonal matrix is a matrix in which the entries outside the main diagonal are all zero.
To create such a R matrix the syntax is given below:
Syntax: diag(k, m, n)​
Parameters: ​
k: the constants/array ​
m: no of rows ​
n: no of columns
Example:
print(diag(c(5, 3, 3), 3, 3))
Output
[,1] [,2] [,3]
[1,] 5 0 0
[2,] 0 3 0
[3,] 0 0 3
3. Identity matrix:
An identity matrix in which all the elements of the principal diagonal are ones and all
other elements are zeros. To create such a R matrix the syntax is given below:
Syntax: diag(k, m, n)​
Parameters: ​
k: 1 m: no of rows n: no of columns
Example:
print(diag(1, 3, 3))
Output
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
Matrix Metrics
Matrix metrics tell you about the Matrix you created. You might want to know the
number of rows, number of columns, dimensions of a Matrix. It describes about How you
can know the dimension of the matrix? How can you know how many rows are there in
the matrix? How many columns are in the matrix? How many elements are there in the
matrix?
Example:
A = matrix( c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
cat("Dimension of the matrix:\n")
print(dim(A))
cat("Number of rows:\n")
print(nrow(A))
cat("Number of columns:\n")
print(ncol(A))
cat("Number of elements:\n")
print(length(A))
print(prod(dim(A)))
Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Dimension of the matrix:
[1] 3 3
Number of rows:
[1] 3
Number of columns:
[1] 3
Number of elements:
[1] 9
Accessing Elements of a R-Matrix
We can access elements in the R matrices using the same convention that is
followed in data frames. So, you will have a matrix and followed by a square bracket
with a comma in between the arrays. Value before the comma is used to access rows and
value that is after the comma is used to access columns. Let’s illustrate this by taking a
simple R code.
Accessing rows:
A = matrix(6 c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
cat("Accessing first and second row\n")
print(A[1:2, ])
Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing first and second row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Accessing columns:
A = matrix( c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
cat("Accessing first and second column\n")
print(A[, 1:2])
Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing first and second column
[,1] [,2]
[1,] 1 2
[2,] 4 5
[3,] 7 8
Adding Rows and Columns in a R-Matrix
To add a row in R-matrix you can use rbind() function and to add a column to
R-matrix you can use cbind() function.
Adding Row
Let’s see below example on how to add row in R-matrix?
Example:
number <- matrix( c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("Before inserting a new row:\n")
print(number)
new_row <- c(10, 11, 12)
A <- rbind(number[1, ], new_row, number[-1, ])
cat("\nAfter inserting a new row:\n")
print(number)
Output
Before inserting a new row:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
After inserting a new row:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,]...
Adding Column
Let’s see below example on how to add column in R-matrix?
number <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("Before adding a new column:\n")
print(number)
new_column <- c(10, 11, 12) # Define the new column
number <- cbind(number, new_column)
cat("\nAfter adding a new column:\n")
print(number)
Output
Before adding a new column:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
After adding a new column:
new_column
[1,] 1 2 3 10
[2,] 4 5 6 1...
Deleting Rows and Columns of a R-Matrix
To delete a row or a column, first of all, you need to access that row or column and
then insert a negative sign before that row or column. It indicates that you had to delete
that row or column.
Row deletion:
A = matrix( c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("Before deleting the 2nd row\n")
print(A)
A = A[-2, ]
cat("After deleted the 2nd row\n")
print(A)
Output
Before deleting the 2nd row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
After deleted the 2nd row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 7 8 9
Column deletion:
A = matrix( c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("Before deleting the 2nd column\n")
print(A)
A = A[, -2]
cat("After deleted the 2nd column\n")
print(A)
Output
Before deleting the 2nd column
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
After deleted the 2nd column
[,1] [,2]
[1,] 1 3
[2,] 4 6
[3,] 7 9

ARRAYS
Arrays are essential data storage structures defined by a fixed number of
dimensions. Arrays are used for the allocation of space at contiguous memory locations.
In R Programming Language Uni-dimensional arrays are called vectors with the
length being their only dimension. Two-dimensional arrays are called matrices, consisting
of fixed numbers of rows and columns. R Arrays consist of all elements of the same data
type. Vectors are supplied as input to the function and then create an array based on the
number of dimensions.
Creating an Array
An R array can be created with the use of array() the function. A list of elements is
passed to the array() functions along with the dimensions as required.
Syntax:
array(data, dim = (nrow, ncol, nmat), dimnames=names)
where
nrow: Number of rows
ncol : Number of columns
nmat: Number of matrices of dimensions nrow * ncol
dimnames : Default value = NULL.
Otherwise, a list has to be specified which has a name for each component of the
dimension. Each component is either a null or a vector of length equal to the dim value of
that corresponding dimension.
Uni-Dimensional Array
A vector is a uni-dimensional array, which is specified by a single dimension,
length. A Vector can be created using ‘c()‘ function. A list of values is passed to the c()
function to create a vector.
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
print (vec1)
cat ("Length of vector : ", length(vec1))
Output:
[1] 1 2 3 4 5 6 7 8 9
Length of vector : 9
Multi-Dimensional Array
A two-dimensional matrix is an array specified by a fixed number of rows and
columns, each containing the same data type. A matrix is created by using array()
function to which the values and the dimensions are passed.
arr = array(2:13, dim = c(2, 3, 2))
print(arr)
Output:
,,1
[,1] [,2] [,3]
[1,] 2 4 6
[2,] 3 5 7
,,2
[,1] [,2] [,3]
[1,] 8 10 12
[2,] 9 11 13
Vectors of different lengths can also be fed as input into the array() function.
However, the total number of elements in all the vectors combined should be equal to the
number of elements in the matrices. The elements are arranged in the order in which they
are specified in the function.
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vec2 <- c(10, 11, 12)
arr = array(c(vec1, vec2), dim = c(2, 3, 2))
print (arr)
Output:
,,1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
,,2
[,1] [,2] [,3]
[1,] 7 9 11
[2,] 8 10 12
Dimension of the Array
We will use dim function to find out the dimension of the R array.
arr = array(2:13, dim = c(2, 3, 2))
dim(arr)
Output:
[1] 2 3 2
This specifies the dimensions of the R array. In this case, we are creating a 3D
array with dimensions 2x3x2. The first dimension has size 2, the second dimension has
size 3, and the third dimension has size 2.
Naming of Arrays
The row names, column names and matrices names are specified as a vector of the
number of rows, number of columns and number of matrices respectively. By default, the
rows, columns and matrices are named by their index values.
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
arr = array(2:14, dim = c(2, 3, 2), dimnames = list(row_names, col_names, mat_names))
print (arr)
Output:
, , Mat1
col1 col2 col3
row1 2 4 6
row2 3 5 7
, , Mat2
col1 col2 col3
row1 8 10 12
row2 9 11 13
Accessing arrays
The R arrays can be accessed by using indices for different dimensions separated
by commas. Different components can be specified by any combination of elements’
names or positions.
Accessing Uni-Dimensional Array
The elements can be accessed by using indexes of the corresponding elements.
vec <- c(1:10)
cat ("Vector is : ", vec)
cat ("Third element of vector is : ", vec[3])
Output:
Vector is : 1 2 3 4 5 6 7 8 9 10
Third element of vector is : 3
Accessing entire matrices
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vec2 <- c(10, 11, 12)
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
arr = array(c(vec1, vec2), dim = c(2, 3, 2), dimnames = list(row_names, col_names,
mat_names))
arr
print ("Matrix 1")
print (arr[,,1])
print ("Matrix 2")
print(arr[,,"Mat2"])
Output:
, , Mat1
col1 col2 col3
row1 1 3 5
row2 2 4 6
, , Mat2
col1 col2 col3
row1 7 9 11
row2 8 10 12
accessing matrix 1 by index value
[1] "Matrix 1"
col1 col2 col3
row1 1 3 5
row2 2 4 6
accessing matrix 2 by its name
[1] "Matrix 2"
col1 col2 col3
row1 7 9 11
row2 8 10 12
Accessing specific rows and columns of matrices
Rows and columns can also be accessed by both names as well as indices.
vec1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vec2 <- c(10, 11, 12)
row_names <- c("row1", "row2")
col_names <- c("col1", "col2", "col3")
mat_names <- c("Mat1", "Mat2")
arr = array(c(vec1, vec2), dim = c(2, 3, 2), dimnames = list(row_names, col_names,
mat_names))
arr
print ("1st column of matrix 1")
print (arr[, 1, 1])
print ("2nd row of matrix 2")
print(arr["row2",,"Mat2"])
Output:
, , Mat1
col1 col2 col3
row1 1 3 5
row2 2 4 6
, , Mat2
col1 col2 col3
row1 7 9 11
row2 8 10 12
accessing matrix 1 by index value
[1] "1st column of matrix 1"
row1 row2
1 2
accessing matrix 2 by its name
[1] "2nd row of matrix 2"
col1 col2 col3
8 10 12
Adding elements to array
Elements can be appended at the different positions in the array. The sequence of
elements is retained in order of their addition to the array. The time complexity required
to add new elements is O(n) where n is the length of the array. The length of the array
increases by the number of element additions. There are various in-built functions
available in R to add new values:
●​ c(vector, values): c() function allows us to append values to the end of the array.
Multiple values can also be added together.
●​ append(vector, values): This method allows the values to be appended at any
position in the vector. By default, this function adds the element at end.
●​ append(vector, values, after=length(vector)) adds new values after specified
length of the array specified in the last argument of the function.
●​ Using the length function of the array: Elements can be added at length+x
indices where x>0.
Example:
x <- c(1, 2, 3, 4, 5)
x <- c(x, 6)
print ("Array after 1st modification ")
print (x)
x <- append(x, 7)
print ("Array after 2nd modification ")
print (x)
len <- length(x)
x[len + 1] <- 8
print ("Array after 3rd modification ")
print (x)
x[len + 3]<-9
print ("Array after 4th modification ")
print (x)
print ("Array after 5th modification")
x <- append(x, c(10, 11, 12), after = length(x)+3)
print (x)
print ("Array after 6th modification")
x <- append(x, c(-1, -1), after = 3)
print (x)
Output:
[1] "Array after 1st modification "
[1] 1 2 3 4 5 6
[1] "Array after 2nd modification "
[1] 1 2 3 4 5 6 7
[1] "Array after 3rd modification "
[1] 1 2 3 4 5 6 7 8
[1] "Array after 4th modification "
[1] 1 2 3 4 5 6 7 8 NA 9
[1] "Array after 5th modification"
[1] 1 2 3 4 5 6 7 8 NA 9 10 11 12
[1] "Array after 6th modification"
[1] 1 2 3 -1 -1 4 5 6 7 8 NA 9 10 11 12
Removing Elements from Array
Elements can be removed from arrays in R, either one at a time or multiple
together. These elements are specified as indexes to the array, wherein the array values
satisfying the conditions are retained and rest removed. The comparison for removal is
based on array values. Multiple conditions can also be combined together to remove a
range of elements. Another way to remove elements is by using %in% operator wherein
the set of element values belonging to the TRUE values of the operator are displayed as
result and the rest are removed.
Example:
m <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
print("Original Array")
print(m)
m <- m[m != 3]
print("After 1st modification")
print(m)
m <- m[m > 2 & m <= 8]
print("After 2nd modification")
print(m)
remove <- c(4, 6, 8)
print(m %in% remove)
print("After 3rd modification")
print(m[!m %in% remove])
Output:
[1] "Original Array"
[1] 1 2 3 4 5 6 7 8 9
[1] "After 1st modification"
[1] 1 2 4 5 6 7 8 9
[1] "After 2nd modification"
[1] 4 5 6 7 8
[1] TRUE FALSE TRUE FALSE TRUE
[1] "After 3rd modification"
[1] 5 7
Updating Existing Elements of Array
The elements of the array can be updated with new values by assignment of the
desired index of the array with the modified value. The changes are retained in the
original array. If the index value to be updated is within the length of the array, then the
value is changed, otherwise, the new element is added at the specified index. Multiple
elements can also be updated at once, either with the same element value or multiple
values in case the new values are specified as a vector.
Example:
m <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
print ("Original Array")
print (m)
m[1] <- 0
print ("After 1st modification")
print (m)
m[7:9] <- -1
print ("After 2nd modification")
print (m)
m[c(2, 5)] <- c(-1, -2)
print ("After 3rd modification")
print (m)
m[10] <- 10
print ("After 4th modification")
print (m)
Output:
[1] "Original Array"
[1] 1 2 3 4 5 6 7 8 9
[1] "After 1st modification"
[1] 0 2 3 4 5 6 7 8 9
[1] "After 2nd modification"
[1] 0 2 3 4 5 6 -1 -1 -1
[1] "After 3rd modification"
[1] 0 -1 3 4 -2 6 -1 -1 -1
[1] "After 4th modification"
[1] 0 -1 3 4 -2 6 -1 -1 -1 10

CLASSES IN R PROGRAMMING
Classes and Objects are basic concepts of Object-Oriented Programming that
revolve around real-life entities. Everything in R is an object. An object is simply a data
structure that has some methods and attributes. A class is just a blueprint or a sketch of
these objects. It represents the set of properties or methods that are common to all objects
of one type.
Unlike most other programming languages, R has a three-class system. These are S3, S4,
and Reference Classes.
S3 Class
S3 is the simplest yet the most popular OOP system and it lacks formal definition and
structure. An object of this type can be created by just adding an attribute to it. Following
is an example to make things more clear:
Example:
movieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")
class(movieList) <- "movie"
movieList
Output:
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"
In S3 systems, methods don’t belong to the class. They belong to generic functions. It
means that we can’t create our own methods here, as we do in other programming
languages like C++ or Java. But we can define what a generic method (for example print)
does when applied to our objects.
print(movieList)
Output:
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"

INPUT IN R
Developers often have a need to interact with users, either to get data or to provide
some sort of result. Most programs today use a dialog box as a way of asking the user to
provide some type of input. Like other programming languages in R it’s also possible to
take input from the user. For doing so, there are two methods in R.
●​ Using readline() method
●​ Using scan() method
Using readline() method
In R language readline() method takes input in string format. If one inputs an
integer then it is inputted as a string, lets say, one wants to input 255, then it will input as
“255”, like a string. So one needs to convert that inputted value to the format that he
needs. In this case, string “255” is converted to integer 255. To convert the inputted value
to the desired data type, there are some functions in R, ​
[Link](n); —> convert to integer
[Link](n); —> convert to numeric type (float, double etc)
[Link](n); —> convert to complex number (i.e 3+2i)
[Link](n) —> convert to date …, etc
var = readline();
# convert the inputted value to integer
var = [Link](var);
# print the value
print(var)
Output: ​
255
[1] 255
Taking multiple inputs in R
Taking multiple inputs in R language is same as taking single input, just need to
define multiple readline() for inputs. One can use braces for define multiple readline()
inside it.​
Syntax: ​
var1 = readline(“Enter 1st number : “); ​
var2 = readline(“Enter 2nd number : “); ​
var3 = readline(“Enter 3rd number : “); ​
var4 = readline(“Enter 4th number : “);​
or,​
{​
var1 = readline(“Enter 1st number : “); ​
var2 = readline(“Enter 2nd number : “); ​
var3 = readline(“Enter 3rd number : “); ​
var4 = readline(“Enter 4th number : “); ​
}
Example:
# using braces
{
var1 = readline("Enter 1st number : ");
var2 = readline("Enter 2nd number : ");
var3 = readline("Enter 3rd number : ");
var4 = readline("Enter 4th number : ");
}
# converting each value
var1 = [Link](var1);
var2 = [Link](var2);
var3 = [Link](var3);
var4 = [Link](var4);
# print the sum of the 4 number
print(var1 + var2 + var3 + var4)
Output: ​
Enter 1st number : 12
Enter 2nd number : 13
Enter 3rd number : 14
Enter 4th number : 15
[1] 54
Using scan() method
Another way to take user input in R language is using a method, called scan()
method. This method takes input from the console. This method is a very handy method
while inputs are needed to taken quickly for any mathematical calculation or for any
dataset. This method reads data in the form of a vector or list. This method also uses to
reads input from a file also. ​
Syntax: ​
x = scan()​
scan() method is taking input continuously, to terminate the input process, need to press
Enter key 2 times on the console.
Example: ​
​ This is simple method to take input using scan() method, where some integer
number is taking as input and print those values in the next line on the console.
x = scan()
print(x)
Output: ​
1: 1 2 3 4 5 6
7: 7 8 9 4 5 6
13:
Read 12 items
[1] 1 2 3 4 5 6 7 8 9 4 5 6
In R there are various methods to print the output. Most common method to print
output in R program, there is a function called print() is used. Also if the program of R is
written over the console line by line then the output is printed normally, no need to use
any function for print that output. To do this just select the output variable and press run
button.
OUTPUT IN R
Print output using print() function
Using print() function to print output is the most common method in R.
Implementation of this method is very simple.
Syntax: print(“any string”) or, print(variable)
print("GFG")
x <- "GeeksforGeeks"
print(x)
Output:
[1] "GFG"
[1] "GeeksforGeeks"
Print output using paste() function inside print() function
R provides a method paste() to print output with string and variable together. This
method defined inside the print() function. paste() converts its arguments to character
strings.
x <- "GeeksforGeeks"
print(paste(x, "is best (paste inside print())"))
print(paste0(x, "is best (paste0 inside print())"))
Output:
[1] "GeeksforGeeks is best (paste inside print())"
[1] "GeeksforGeeksis best (paste0 inside print())"
Print output using sprintf() function
sprintf() is basically a C library function. This function is use to print string as C
language. This is working as a wrapper function to print values and strings together like
C language. This function returns a character vector containing a formatted combination
of string and variable to be printed.
x = "GeeksforGeeks" # string
x1 = 255 ​ # integer
x2 = 23.14 ​ # float
sprintf("%s is best", x)
sprintf("%d is integer", x1)
sprintf("%f is float", x2)
Output:
> sprintf("%s is best", x)
[1] "GeeksforGeeks is best"
> sprintf("%d is integer", x1)
[1] "255 is integer"
> sprintf("%f is float", x2)
[1] "23.140000 is float"
Print output using cat() function
Another way to print output in R is using of cat() function. It’s same as print()
function. cat() converts its arguments to character strings. This is useful for printing
output in user defined functions.
Syntax: cat(“any string”) or, cat(“any string”, variable)
Example:
x = "GeeksforGeeks"
cat(x, "is best\n")
cat("This is R language")
Output:
GeeksforGeeks is best
This is R language
Print output using message() function
Another way to print something in R by using message() function. This is not used
for print output but its use for showing simple diagnostic messages which are no
warnings or errors in the program. But it can be used for normal uses for printing output.
Syntax: message(“any string”) or, message(“any string”, variable)
Example:
x = "GeeksforGeeks"
message(x, "is best")
message("This is R language")
Output:
GeeksforGeeks is best
This is R language

STRING MANIPULATION IN R
String manipulation basically refers to the process of handling and analyzing
strings. It involves various operations concerned with modification and parsing of strings
to use and change its data. R offers a series of in-built functions to manipulate the
contents of a string. In this article, we will study different functions concerned with the
manipulation of strings in R.
Concatenation of Strings
String Concatenation is the technique of combining two strings. String
Concatenation can be done using many ways:
1. paste() function Any number of strings can be concatenated together using the paste()
function to form a larger string. This function takes a separator as an argument which is
used between the individual string elements and another argument ‘collapse’ which
reflects if we wish to print the strings together as a single larger string. By default, the
value of collapse is NULL.
Syntax:
paste(..., sep=" ", collapse = NULL)
Example:
str <- paste("Learn", "Code")
print (str)
Output:
"Learn Code"
In case no separator is specified the default separator ” ” is inserted between individual
strings.
Example:
str <- paste(c(1:3), "4", sep = ":")
print (str)
Output:
"1:4" "2:4" "3:4"
2. cat() function Different types of strings can be concatenated together using the cat())
function in R, where sep specifies the separator to give between the strings and file name,
in case we wish to write the contents onto a file.
Syntax:
cat(..., sep=" ", file)
Example:
str <- cat("learn", "code", "tech", sep = ":")
print (str)
Output:
learn:code:techNULL
The output string is printed without any quotes and the default separator is ‘:’.NULL
value is appended at the end.
Example:
cat(c(1:5), file ='[Link]')
Output:
12345
Calculating Length of strings
1. length() function The length() function determines the number of strings specified in
the function.
Example:
print (length(c("Learn to", "Code")))
Output:
2
There are two strings specified in the function.
nchar() function nchar() counts the number of characters in each of the strings specified
as arguments to the function individually.
Example:
print (nchar(c("Learn", "Code")))
Output:
5 4
Case Conversion of strings
1. Conversion to uppercase All the characters of the strings specified are converted to
upper case.
Example:
print (toupper(c("Learn Code", "hI")))
2. Conversion to lowercase All the characters of the strings specified are converted to
lowercase.
Example:
print (tolower(c("Learn Code", "hI")))
Output :
"learn code" "hi"
3. casefold() function All the characters of the strings specified are converted to
lowercase or uppercase according to the arguments in casefold(…, upper=TRUE).
Examples:
print (casefold(c("Learn Code", "hI"), upper = TRUE)))
Output :
"LEARN CODE" "HI"

You might also like