R Programming Basics and Control Structures
R Programming Basics and Control Structures
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
# 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
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:
MATHEMATICAL FUNCTIONS
cos(), sin(), and tan() calculates a number’s cosine, sine, and tang
STATISTICAL FUNCTIONS
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()
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
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
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
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"