0% found this document useful (0 votes)
12 views

UNIT-I_r_programming-1

Uploaded by

deepakgowdamc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

UNIT-I_r_programming-1

Uploaded by

deepakgowdamc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

STATISTICA COMPUTING AND R PROGRAMMING

STATISTICAL COMPUTING AND R PROGRAMMING


UNIT- I
R programming is an interpreted programming language widely used to analyse
statistical information and a graphical representation. It is also a software
environment used to analyse statistical information, graphical
representation, reporting, and data modelling. R programming language was
created by Ross Ihaka and Robert Gentleman at the University of Auckland, New
Zealand.

Features of R programming
1. It is a simple and effective programming language which has been well
developed.
2. It is a well-designed, easy, and effective language which has the concepts
of user-defined, looping, conditional, and various I/O facilities.
3. For different types of calculation on arrays, lists and vectors, R contains a
suite of operators.
4. It provides effective data handling and storage facility.
5. It is an open-source, powerful, and highly extensible software.
6. It provides highly extensible graphical techniques.
7. R is an interpreted language.

R Advantages and Disadvantages


R is the most popular programming language for statistical modelling and
analysis. Like other programming languages, R also has some advantages and
disadvantages.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Pros
1) Open Source

An open-source language is a language on which we can work without any need


for a license or a fee.

2) Platform Independent

R is a platform-independent language or cross-platform programming language


which means its code can run on all operating systems.

3) Machine Learning Operations

R allows us to do various machine learning operations such as classification and


regression.

4) Quality plotting and graphing

R simplifies quality plotting and graphing. R libraries such as ggplot2 and plotly
advocates for visually appealing and aesthetic graphs which set R apart from
other programming languages.

5) Statistics

R is mainly known as the language of statistics. It is the main reason why R is


predominant than other programming languages for the development of statistical
tools.

Cons
1) Data Handling

In R, objects are stored in physical memory. It requires the entire data in one
single place which is in the memory. It is not an ideal option when we deal with
Big Data.

2) Basic Security

R lacks basic security. It is an essential part of most programming. Because of


this, there are many restrictions with R as it cannot be embedded in a web-
application.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

3) Lesser Speed

R programming language is much slower than other programming languages such


as MATLAB and Python. In comparison to other programming language, R
packages are much slower.

4) Complicated Language

R is a very complicated language, and it has a steep learning curve. The people
who don't have prior knowledge or programming experience may find it difficult
to learn R.

Data Types in R Programming


R data types are used in computer programming to specify the kind of data that
can be stored in a variable.

The operating system allocates memory based on the data type of the variable and
decides what can be stored in the reserved memory.

There are the following data types which are used in R programming:

Data Example Description


type

Logical True, False It is a special data type for data with only two possible
values which can be construed as true/false.

Numeric 12,32,112,5432 Decimal value is called numeric in R, and it is the default


computational data type.

Integer 3L, 66L, 2346L Here, L tells R to store the value as an integer,

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Complex Z=1+2i, t=7+3i A complex value in R is defined as the pure imaginary


value i.

Character 'a', '"good'", In R programming, a character is used to represent string


"TRUE", '35.4' values. We convert objects into character values with the
help of as.character() function.

Variables in R Programming
Variables are used to store the information to be manipulated and referenced in
the R program.

The R variable can store an atomic vector, a group of atomic vectors, or a


combination of many R objects.

R supports three ways of variable assignment:

Using equal operator- operators use an arrow or an equal sign to assign values to
variables.

Using the leftward operator- data is copied from right to left.

R Variables Syntax

1) Using equal to operators

variable_name = value

2) Using leftward operator

variable_name  value

Example:
var1  "hello"
print(var1)

Output: hello

The following rules need to be kept in mind while naming a R variable:

1) A valid variable name consists of a combination of alphabets, numbers,


dot(.), and underscore(_) characters. Example: var.1_ is valid

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

2) Apart from the dot and underscore operators, no other special character is
allowed. Example: var$1 or var#1 both are invalid
3) Variables can start with alphabets or dot characters. Example: .var or var
is valid
4) The variable should not start with numbers or underscore. Example: 2var
or _var is invalid.
5) If a variable starts with a dot the next thing after the dot cannot be a number.
Example: .3var is invalid
6) The variable name should not be a reserved keyword in R. Example:
TRUE, FALSE,etc.

class() function

This built-in function is used to determine the data type of the variable provided
to it. The R variable to be checked is passed to this as an argument and it prints
the data type in return.

Syntax:

class(variable)

Example:

var1 = "hello"
print(class(var1))
Output: “character”

Operators in R
An operator is a symbol which tells the compiler to perform
specific logical or mathematical manipulations. In R programming, there are
different types of operators, and each operator performs a different task.

There are the following types of operators used in R:


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Miscellaneous Operators

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Arithmetic Operators

Arithmetic operators are the symbols which are used to represent arithmetic math
operations. There are various arithmetic operators which are supported by R.

S. Operator Description Example


No

1. + This operator is a <- c(2, 3.3, 4)


used to add two b <- c(11, 5, 3)
vectors in R. print(a+b)

It will give us the following output:


[1] 13.0 8.3 5.0

2. - This operator is a <- c(2, 3.3, 4)


used to divide a b <- c(11, 5, 3)
vector from another print(a-b)
one.
It will give us the following output:
[1] -9.0 -1.7 3.0

3. * This operator is a <- c(2, 3.3, 4)


used to multiply b <- c(11, 5, 3)
two vectors with print(a*b)
each other.
It will give us the following output:
[1] 22.0 16.5 4.0

4. / This operator a <- c(2, 3.3, 4)


divides the vector b <- c(11, 5, 3)
from another one. print(a/b)
It will give us the following output:
[1] 0.1818182 0.6600000
4.0000000

5. %% This operator is a <- c(2, 3.3, 4)


used to find the b <- c(11, 5, 3)
remainder of the print(a%%b)

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

first vector with the


second vector. It will give us the following output:
[1] 2.0 3.3 0

6. %/% This operator is a <- c(2, 3.3, 4)


used to find the b <- c(11, 5, 3)
division of the first print(a%/%b)
vector with the
second(quotient). It will give us the following output:
[1] 0 0 4

7. ^ This operator a <- c(2, 3.3, 4)


raised the first b <- c(11, 5, 3)
vector to the print(a^b)
exponent of the
second vector. It will give us the following output:
[1] 0248.0000 391.3539
4.0000

Relational Operators

A relational operator compares each element of the first vector with the
corresponding element of the second vector. The result of the comparison will be
a Boolean value. There are the following relational operators which are supported
by R:

S. Operator Description Example


No

1. > This operator will return a <- c(1, 3, 5)


TRUE when every element in b <- c(2, 4, 6)
the first vector is greater than print(a>b)
the corresponding element of
the second vector. It will give us the
following output:
[1] FALSE FALSE
FALSE

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

2. < This operator will return a <- c(1, 9, 5)


TRUE when every element in b <- c(2, 4, 6)
the first vector is less than the print(a<b)
corresponding element of the
second vector. It will give us the
following output:
[1] FALSE TRUE
FALSE

3. <= This operator will return a <- c(1, 3, 5)


TRUE when every element in b <- c(2, 3, 6)
the first vector is less than or print(a<=b)
equal to the corresponding
element of another vector. It will give us the
following output:
[1] TRUE TRUE TRUE

4. >= This operator will return a <- c(1, 3, 5)


TRUE when every element in b <- c(2, 3, 6)
the first vector is greater than print(a>=b)
or equal to the corresponding
element of another vector. It will give us the
following output:
[1] FALSE TRUE
FALSE

5. == This operator will return a <- c(1, 3, 5)


TRUE when every element in b <- c(2, 3, 6)
the first vector is equal to the print(a==b)
corresponding element of the
second vector. It will give us the
following output:
[1] FALSE TRUE
FALSE

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

6. != This operator will return a <- c(1, 3, 5)


TRUE when every element in b <- c(2, 3, 6)
the first vector is not equal to print(a>=b)
the corresponding element of
the second vector. It will give us the
following output:
[1] TRUE FALSE TRUE

Logical Operators

The logical operators allow a program to make a decision on the basis of multiple
conditions. In the program, each operand is considered as a condition which can
be evaluated to a false or true value. The logical operator compares each element
of the first vector with the corresponding element of the second vector.

There are the following types of operators which are supported by R:

S. Operator Description Example


No

1. & This operator is known as the a <- c(3, 0, TRUE, 2+2i)


Logical AND operator. This b <- c(2, 4, TRUE, 2+3i)
operator takes the first element print(a&b)
of both the vector and returns
TRUE if both the elements are It will give us the following
TRUE. output:
[1] TRUE FALSE TRUE
TRUE

2. | This operator is called the a <- c(3, 0, TRUE, 2+2i)


Logical OR operator. This b <- c(2, 4, TRUE, 2+3i)
operator takes the first element print(a|b)
of both the vector and returns
TRUE if one of them is TRUE. It will give us the following
output:
[1] TRUE TRUE TRUE TRUE

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

3. ! This operator is known as a <- c(3, 0, TRUE, 2+2i)


Logical NOT operator. This print(!a)
operator takes the first element
of the vector and gives the It will give us the following
opposite logical value as a result. output:
[1] FALSE TRUE FALSE
FALSE

4. && This operator takes the first a <- c(3, 0, TRUE, 2+2i)
element of both the vector and b <- c(2, 4, TRUE, 2+3i)
gives TRUE as a result, only if print(a&&b)
both are TRUE.
It will give us the following
output:
[1] TRUE

5. || This operator takes the first a <- c(3, 0, TRUE, 2+2i)


element of both the vector and b <- c(2, 4, TRUE, 2+3i)
gives the result TRUE, if one of print(a||b)
them is true.
It will give us the following
output:
[1] TRUE

Assignment Operators

An assignment operator is used to assign a new value to a variable. In R, these


operators are used to assign values to vectors. There are the following types of
assignment.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

S. Operator Description Example


No

1. <- or = or These operators are a <- c(3, 0, TRUE, 2+2i)


<<- known as left b <<- c(2, 4, TRUE, 2+3i)
assignment operators. d = c(1, 2, TRUE, 2+3i)
print(a)
print(b)
print(d)

It will give us the following output:


[1] 3+0i 0+0i 1+0i 2+2i
[1] 2+0i 4+0i 1+0i 2+3i
[1] 1+0i 2+0i 1+0i 2+3i

2. -> or ->> These operators are c(3, 0, TRUE, 2+2i) -> a


known as right c(2, 4, TRUE, 2+3i) ->> b
assignment operators. print(a)
print(b)

It will give us the following output:


[1] 3+0i 0+0i 1+0i 2+2i
[1] 2+0i 4+0i 1+0i 2+3i

Miscellaneous Operators

Miscellaneous operators are used for a special and specific purpose. These
operators are not used for general mathematical or logical computation. There are
the following miscellaneous operators which are supported in R

S. Operator Description Example


No

1. : The colon operator is v <- 1:8


used to create the series print(v)
of numbers in sequence
for a vector. It will give us the following output:
[1] 1 2 3 4 5 6 7 8

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

2. %in% This is used when we a1 <- 8


want to identify if an a2 <- 12
element belongs to a d <- 1:10
vector. print(a1%in%t)
print(a2%in%t)

It will give us the following output:


[1] FALSE
[1] FALSE

3. %*% It is used to multiply a M=matrix(c(1,2,3,4,5,6),nrow=2,


matrix with its ncol=3, byrow=TRUE)
transpose. T=m%*%T(m)
print(T)

It will give us the following output:


14 32
32 77

Data Structures in R Programming language:


1) Vectors:
In R, a sequence of elements which share the same data type is known as
vector. Vector is classified into two parts:
i) Atomic Vectors
ii) List

Note: There is only one difference between atomic vectors and lists. In an
atomic vector, all the elements are of the same type, but in the list, the elements
are of different data types.

How to create a vector in R?

1) Using the c() function


In R, we use c() function to create a vector. This function returns a one-
dimensional array or simply vector.
Eample:
Myvec <- c(1,3,1,4,2)

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Print(Myvec)

Output: 1 3 1 4 2

2) Using the colon(:) operator

We can create a vector with the help of the colon operator. There is the following
syntax to use colon operator:

Z  x:y
Example: b 1:10
Print (b)
Output: 1 2 3 4 5 6 7 8 9 10

3)Using the seq() function

A sequence function creates a sequence of elements as a vector. The seq() function


is used by setting step size with ‘by' parameter.

Example: seq_vec<-seq(1,4,by=0.5)
Print (seq_vec)
Output: 1.0 1.5 2.0 2.5 3.0 3.5 4.0

Atomic vectors in R

In R, there are four types of atomic vectors.

Numeric vector

A vector which contains numeric elements is known as a numeric vector. If we


assign a decimal value to any variable, then that variable will become a numeric
type.

Example: num_vec<-c(10.1, 10.2, 33.2)


Print(num_vec )
class(num_vec)
Output: 10.1 10.2 33.2
"numeric"

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Integer vector
A non-fraction numeric value is known as integer data. An integer value can be
assigned to variable by appending L to the value.
Example: int_vec1<-c(1L,2L,3L,4L,5L)
class(int_vec1)
Output: “integer”

Character vector
A vector which contains character elements is known as an integer vector. In R
character data type value can be created using double quotes("") or single
quotes('').
Example: char_vec1<-c("shubham","arpita","nishka","vaishali")
Print(char_vec)
class(char_vec1)
Output: "shubham" "arpita" "nishka" "vaishali"
"character"

Logical vector

The logical data types have only two values i.e., True or False. These values are
based on which condition is satisfied. A vector which contains Boolean values is
known as the logical vector.

Example: d<- 5
e<- 6
f<- 7
log_vec<-c(d<e, d<f, e<d,e<f,f<d,f<e)
print(log_vec)
class(log_vec)
Output: TRUE TRUE FALSE TRUE FALSE FALSE
"logical"

Accessing elements of vectors

We can access the elements of a vector with the help of vector indexing. Indexing
denotes the position where the value in a vector is stored.

In R, the indexing starts from 1. We can perform indexing by specifying an integer


value in square braces [] next to our vector.

Example: seq_vec<-seq(1,4,length.out=6)
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

Print(seq_vec)
Print(seq_vec[2])
Output: 1.0 1.6 2.2 2.8 3.4 4.0
1.6

Vector Operation
1) Combining vectors
By combining one or more vectors, it forms a new vector which contains all the
elements of each vector.
Example: p<-c(1,2,4,5,7,8)
q<-c("shubham","arpita","nishka","gunjan","vaishali","sumit")
r<-c(p,q)
print (r)
Output: "1" "2" "4" "5" "7" "8"
"shubham" "arpita" "nishka" "gunjan" "vaishali" "sumit"

2) Arithmetic operations

We can perform all the arithmetic operation on vectors. The arithmetic operations
are performed member-by-member on vectors.

Example:
a<-c(1,3,5,7)
b<-c(2,4,6,8)
print (a+b)
print (a-b)
Output: 3 7 11 15
-1 -1 -1 -1

R Lists
In R, lists are the second type of vector. A list is a data structure which has
components of mixed data types. Lists are the objects of R which contain
elements of different types such as number, vectors, string and another list inside
it.

Lists creation

The function which is used to create a list in R is list( ).

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Example:

1) list_1<-list(1,2,3)
list_2<-list("Shubham","Arpita","Vaishali")
Output:
1
2
3

"Shubham"
"Arpita"
"Vaishali"

2) list_datalist("Shubham","Arpita",c(1,2,3,4,5),TRUE,FALSE,22.5,12L)
print(list_data)
Output:
"Shubham"
"Arpita"
12345
TRUE
FALSE
22.5
12

Giving a name to list elements

There are only three steps to print the list data corresponding to the name:

1. Creating a list.
2. Assign a name to the list elements with the help of names() function.
3. Print the list data.

Example

# Creating a list containing a vector, a matrix and a list.


list_data <- list(c("Shubham","Nishka","Gunjan"), matrix(c(40,80,60,70,90,80),
nrow = 2),
list("BCA","MCA","B.tech"))

# Giving names to the elements in the list.


names(list_data) <- c("Students", "Marks", "Course")
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

# Show the list.


print(list_data)

Output:

$Students
[1] "Shubham" "Nishka" "Gunjan"

$Marks
[,1] [,2] [,3]
[1,] 40 60 90
[2,] 80 70 80

$Course
$Course[[1]]
[1] "BCA"

$Course[[2]]
[1] "MCA"

$Course[[3]]
[1] "B. tech."

Accessing List Elements

R provides two ways through which we can access the elements of a list.

1) First one is the indexing method performed in the same way as a vector.
2) In the second one, we can access the elements of a list with the help of
names.

Example 1: Accessing elements using index

# Creating a list containing a vector, a matrix and a list.


list_data <- list(c("Shubham","Arpita","Nishka"), matrix(c(40,80,60,70,90,80),
nrow = 2),list("BCA","MCA","B.tech"))

# Accessing the first element of the list.


print(list_data[1])

Output:
"Shubham" "Arpita" "Nishka"

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Example 2: Accessing elements using names

# Creating a list containing a vector, a matrix and a list.


list_data <- list(c("Shubham","Arpita","Nishka"), matrix(c(40,80,60,70,90,80),
nrow = 2),list("BCA","MCA","B.tech"))

# Giving names to the elements in the list.


names(list_data) <- c("Student", "Marks", "Course")
# Accessing the first element of the list.
print(list_data["Student"])

Output:
$Student
"Shubham" "Arpita" "Nishka"

Manipulation of list elements

R allows us to add, delete, or update elements in the list.

Example

# Creating a list containing a vector, a matrix and a list.


list_data <- list(c("Shubham","Arpita","Nishka"), matrix(c(40,80,60,70,90,80),
nrow = 2),list("BCA","MCA","B.tech"))

# Giving names to the elements in the list.


names(list_data) <- c("Student", "Marks", "Course")

#Adding element at the end of the list.


list_data[4] <- "Moradabad"
print(list_data[4])

#Removing the last element.


list_data[4] <- NULL

# Printing the 4th Element.


print(list_data[4])

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

# Updating the 3rd Element.


list_data[3] <- "Masters of computer applications"
print(list_data[3])

Output:
"Moradabad"

$<NA>
NULL

$Course
"Masters of computer applications"

Converting list to vector

There is a drawback with the list, i.e., we cannot perform all the arithmetic
operations on list elements.

This drawback can be overcome with the function unlist( ), this function converts
the list into vectors.
Example:
# Creating lists.
list1 <- list(1:5)
print(list1)
list2 <-list(10:14)
print(list2)

# Converting the lists to vectors.


v1 <- unlist(list1)
v2 <- unlist(list2)

adding the vectors


result <- v1+v2
print(result)

Output: 1 2 3 4 5
10 11 12 13 14
11 13 15 17 19

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Merging Lists

R allows us to merge one or more lists into one list.

To merge the lists, we have to pass all the lists into list function as a parameter,
and it returns a list which contains all the elements which are present in the lists.

Example

# Creating two lists.


Even_list <- list(2,4,6,8)
Odd_list <- list(3,5,7,9)

# Merging the two lists.


merged.list <- list(Even_list,Odd_list)

# Printing the merged list.


print(merged.list)
Output:2
4
6
8
3
5
7
9

R Matrix
In R, a two-dimensional rectangular data set is known as a matrix.

A matrix is created with the help of the vector input to the matrix function.

In R, we use matrix( ) to create matrix.

Syntax: matrix(data,nrow,ncol,byrow,im_names)

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

data:- It is the input vector which is the data elements of the matrix.

nrow:- It is the number of rows which we want to create in the matrix.

ncol: It is the number of columns which we want to create in the matrix.

byrow:- The byrow parameter is a logical clue. If its value is true, then the input
vector elements are arranged by row.

dim_name:- It is the name assigned to the rows and columns.

Example: p <- matrix(c(5:16), nrow=4, ncol=3, byrow=TRUE)

Print(p)

Output: 5 6 7
8 9 10
11 12 13
14 15 16

Assigning names to the matrix:

# Defining the column and row names.


row_names = c("row1", "row2", "row3", "row4")
ccol_names = c("col1", "col2", "col3")

R <- matrix(c(3:14), nrow = 4, byrow = TRUE, dimnames = list(row_names, col_


names)) print(R)
Output:
col1 col2 col3
row1 3 4 5
row2 6 7 8
row3 9 10 11
row4 12 13 14

Accessing matrix elements in R

There are three ways to access the elements from the matrix.

1. We can access the element which presents on nth row and mth column.
2. We can access all the elements of the matrix which are present on the nth
row.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

3. We can also access all the elements of the matrix which are present on the
mth column.

Example: For the above created R matrix, accessing the elements as follow
#Accessing element present on 3rd row and 2nd column
print(R[3,2])

#Accessing element present in 3rd row


print(R[3,])

#Accessing element present in 2nd column


print(R[,2])
Output: 12

col1 col2 col3


11 12 13

row1 row2 row3 row4


6 9 12 15

Modification of the matrix

Assign a single element

In matrix modification, the first method is to assign a single element to the matrix
at a particular position. By assigning a new value to that position, the old value
will get replaced with the new one.

Syntax: matrix[n, m]<-y

Here, n and m are the rows and columns of the element, respectively. And, y is
the value which we assign to modify our matrix.

Example:
R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, co
l_names))

#Assigning value 20 to the element at 3d roe and 2nd column


R[3,2]<-20
print(R)
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

Output:
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 20 13
row4 14 15 16

Use of Relational Operator


R[R==12]<-0
print(R)
output: col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 0 13
row4 14 15 16

Addition of Rows and Columns


The cbind() and rbind() function are used to add a column and a row
respectively.
Example:
R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names, co
l_names))
#Adding row
rbind(R,c(17,18,19))
print(R)
Output:
col1 col2 col3
row1 5 6 7
row2 8 9 10
row3 11 12 13
row4 14 15 16
17 18 19

#Adding column
cbind(R,c(17,18,19,20))
print(R)
Output:
col1 col2 col3
row1 5 6 7 17
row2 8 9 10 18

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

row3 11 12 13 19
row4 14 15 16 20

Matrix operations
In R, we can perform the mathematical operations on a matrix such as addition,
subtraction, multiplication, etc.

Example:
R <- matrix(c(5:16), nrow = 4,ncol=3)
S <- matrix(c(1:12), nrow = 4,ncol=3)

#Addition
sum<-R+S
print(sum)

#Subtraction
sub<-R-S
print(sub)

#Multiplication
mul<-R*S
print(mul)

#Division
div<-R/S
print(div)

Output:
[,1] [,2] [,3]
[1,] 6 14 22
[2,] 8 16 24
[3,] 10 18 26
[4,] 12 20 28

[,1] [,2] [,3]


[1,] 4 4 4
[2,] 4 4 4
[3,] 4 4 4
[4,] 4 4 4

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

[,1] [,2] [,3]


[1,] 5 45 117
[2,] 12 60 140
[3,] 21 77 165
[4,] 32 96 192

[,1] [,2] [,3]


[1,] 5.000000 1.800000 1.444444
[2,] 3.000000 1.666667 1.400000
[3,] 2.333333 1.571429 1.363636
[4,] 2.000000 1.500000 1.333333

Applications of matrix

1. Matrix is the representation method which helps in plotting common


survey things.
2. In robotics and automation, Matrices have the topmost elements for the
robot movements.
3. In computer-based application, matrices play a crucial role in the creation
of realistic seeming motion.

Arrays

In R, arrays are the data objects which allow us to store data in more than two
dimensions. In R, an array is created with the help of the array() function.

This array() function takes a vector as an input and to create an array it uses
vectors values in the dim parameter.

For example- if we will create an array of dimension (2, 3, 4) then it will create
4 rectangular matrices of 2 row and 3 columns.
Syntax:
array_name <- array(data, dim= (row_size, column_size, matrices, dim_na
mes))

data: It is an input vector which is given to the array.

Matrices: In R, the array consists of multi-dimensional matrices.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

row_size: This parameter defines the number of row elements which an array can
store.

column_size: This parameter defines the number of columns elements which an


array can store.

dim_names: This parameter is used to change the default names of rows and
columns.

Creation of an Array:

There are only two steps to create a matrix which are as follows

1. In the first step, we will create two vectors of different lengths.


2. Once our vectors are created, we take these vectors as inputs to the array.

Example:

#Creating two vectors of different lengths


vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)

#Taking these vectors as input to the array


res <- array(c(vec1,vec2),dim=c(3,3,2))
print(res)

Output:
,,1
[,1] [,2] [,3]
[1,] 1 10 13
[2,] 3 11 14
[3,] 5 12 15

,,2
[,1] [,2] [,3]
[1,] 1 10 13
[2,] 3 11 14
[3,] 5 12 15

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Naming rows and columns

In R, we can give the names to the rows, columns, and matrices of the array. This
is done with the help of the dim name parameter of the array() function.

Example:
#Creating two vectors of different lengths
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)

#Initializing names for rows, columns and matrices


col_names <- c("Col1","Col2","Col3")
row_names <- c("Row1","Row2","Row3")
matrix_names <- c("Matrix1","Matrix2")

#Taking the vectors as input to the array


res <- array(c(vec1,vec2),dim=c(3,3,2),dimnames=list(row_names,col_names,m
atrix_names))
print(res)

Output:
, , Matrix1

Col1 Col2 Col3


Row1 1 10 13
Row2 3 11 14
Row3 5 12 15

, , Matrix2

Col1 Col2 Col3


Row1 1 10 13
Row2 3 11 14
Row3 5 12 15

Accessing array elements

The elements are accessed with the help of the index.


Example: For the above created array

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Print(res[3, ,2]) #To print third row of second matrix


Output: 5 12 15

Print(res[3,2,2]) #To print third row second column element of 2nd matrix
Output: 12

Print(res[ ,2,1]) #To print second column element of 1nd matrix


Output: 10 11 12

Manipulation of elements

The array is made up matrices in multiple dimensions so that the operations on


elements of an array are carried out by accessing elements of the matrices.
Example:
#Creating two vectors of different lengths
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)

#Taking the vectors as input to the array1


res1 <- array(c(vec1,vec2),dim=c(3,3,1))
print(res1)

#Creating two vectors of different lengths


vec1 <-c(8,4,7)
vec2 <-c(16,73,48,46,36,73)

#Taking the vectors as input to the array2


res2 <- array(c(vec1,vec2),dim=c(3,3,1))
print(res2)

#Creating matrices from these arrays


res3 <- mat1+mat2
print(res3)

Output:
,,1
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

[,1] [,2] [,3]


[1,] 1 10 13
[2,] 3 11 14
[3,] 5 12 15

,,1
[,1] [,2] [,3]
[1,] 8 16 46
[2,] 4 73 36
[3,] 7 48 73

[,1] [,2] [,3]


[1,] 9 26 59
[2,] 7 84 50
[3,] 12 60 88

Data Frame
A data frame is a two-dimensional array-like structure or a table in which a
column contains values of one variable, and rows contains one set of values from
each column.
A data frame is a special case of the list in which each component has equal
length.
A matrix can contain one type of data, but a data frame can contain different data
types such as numeric, character, factor, etc.

There are following characteristics of a data frame.

o The columns name should be non-empty.


o The rows name should be unique.
o The data which is stored in a data frame can be a factor, numeric, or
character type.
o Each column contains the same number of data items.

How to create Data Frame

In R, the data frames are created with the help of frame() function of data. This
function contains the vectors of any type such as numeric, character, or integer.

Example: we create a data frame that contains employee id (integer vector),


employee name(character vector), salary(numeric vector), and starting date(Date
vector).
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

# Creating the data frame.


emp.data<- data.frame(
employee_id = c (1:5),
employee_name = c("Shubham","Arpita","Nishka","Gunjan","Sumit"),
sal = c(623.3,915.2,611.0,729.0,843.25),
starting_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-
05-11",
"2015-03-27")),
stringsAsFactors = FALSE
)

# Printing the data frame.


print(emp.data)

Output:
employee_id employee_name sal starting_date
1 1 Shubham 623.30 2012-01-01
2 2 Arpita 915.20 2013-09-23
3 3 Nishka 611.00 2014-11-15
4 4 Gunjan 729.00 2014-05-11
5 5 Sumit 843.25 2015-03-27

To print the structure of data frame


# Printing the structure of data frame.
str(emp.data)
Output:
'data.frame' : 5 obs. of 4 variables:
$ employee_id : int 1 2 3 4 5
$ employee_name: chr "Shubham" "Arpita" "Nishka" "Gunjan" ...
$ sal : num 623 515 611 729 843
$ starting_date: Date, format: "2012-01-01" "2013-09-23" ...

Extracting data from Data Frame

We can extract the data in three ways which are as follows:

1. We can extract the specific columns from a data frame using the column
name.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

2. We can extract the specific rows also from a data frame.


3. We can extract the specific rows corresponding to specific columns.

Extracting the specific columns from a data frame

Example: For the above created data frame

# Extracting specific columns from a data frame


final <- data.frame(emp.data$employee_id,emp.data$sal)
print(final)

Output:

emp.data.employee_id emp.data.sal
1 623.30
2 515.20
3 611.00
4 729.00
5 843.25
Extracting the specific rows from a data frame

Example

# Extracting first row from a data frame


final <- emp.data[1,]
print(final)

Output:

employee_id employee_name sal starting_date


1 Shubham 623.3 2012-01-01
Extracting specific rows corresponding to specific columns

Example:

# Extracting 2nd and 3rd row corresponding to the 1st and 4th column
final <- emp.data[c(2,3),c(1,4)]
print(final)
Output:
employee_id starting_date
2 2013-09-23
3 2014-11-15

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Modification in Data Frame

It is possible to add and delete rows and columns to the data frame.

Example: Adding rows and columns

#Adding row in the data frame


x <- list(6,"Vaishali",547,"2015-09-01")
rbind(emp.data,x)

Output: employee_id employee_name sal starting_date


1 Shubham 623.30 2012-01-01
2 Arpita 515.20 2013-09-23
3 Nishka 611.00 2014-11-15
4 Gunjan 729.00 2014-05-11
5 Sumit 843.25 2015-03-27
6 Vaishali 547.00 2015-09-01

#Adding column in the data frame


y <- c("Moradabad","Lucknow","Etah","Sambhal","Khurja")
cbind(emp.data,Address=y)

Output:

employee_id employee_name sal starting_date Address 1


Shubham 623.30 2012-01-01 Moradabad
2 Arpita 515.20 2013-09-23 Lucknow
3 Nishka 611.00 2014-11-15 Etah
4 Gunjan 729.00 2014-05-11 Sambhal
5 Sumit 843.25 2015-03-27 Khurja

Non-Numeric Values
Logical-values
Introduction to Logical-values
• Logical-values can only take on two values: TRUE or FALSE.
• Logical-values represent binary states like
- >yes/no
->one/zero
• Logical-values are used to indicate whether a condition has been met or not.
TRUE and FALSE Notation
• Logical-values are represented as TRUE and FALSE.
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

• TRUE and FALSE are abbreviated as T and F, respectively.

Assigning Logical-values
• Example:
b1 <- TRUE
b2 <- FALSE

Creating Vectors
• Vectors can be filled with logical-values using T or F.
• Example:
myvec <- c(T,T,F,F,F)

Vector Length
• You can determine the length of a vector using the `length` function.
• Example:

`length(myvec)` returns 5

A Logical Outcome: Relational Operators

Using Relational Operators


• Relational operators are used to find the relationship between two operands.
• The output of relational expression is either TRUE or FALSE.
• The 2 operands may be constants, variables or expressions.
• There are 6 relational operators:
Operator Meaning of Operator Example
> Greater than 4 > 5 returns FALSE
< Less than 4 < 5 returns TRUE
>= Greater than or equal to 4 >= 5 returns FALSE
<= Less than or equal to 4 <= 5 returns TRUE
== Equal to 4 == 5 returns FALSE
!= Not equal to 4 != 5 returns TRUE

any & all Functions


• any() checks whether at least one element in a vector meets a specific condition.
• It returns TRUE if any element satisfies the condition; otherwise, it returns
FALSE.
Example:
# Creating a vector
vector1 <- c(1, 2, 3, 4, 5)

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

# Checking if any element is greater than 3


result <- any(vector1 > 3)
Output:
TRUE

• all() checks whether all elements in a vector meet a specific condition.


• It returns TRUE if all elements satisfy the condition; otherwise, it returns
FALSE
Example:

# Creating a vector
vector2 <- c(1, 2, 3, 4, 5)
# Checking if all elements are greater than 0
result <- all(vector2 > 0)
Output:
TRUE

Multiple Comparisons: Logical Operators

Using Logical Operators for Multiple Conditions


• These operators are used to perform logical operations like negation,
conjunction
and disjunction.

• The output of logical expression is either TRUE or FALSE.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Short and Long Versions


• There are versions of logical operators: i) Short versions ii) Long versions
i) Short versions are for element-wise comparisons.
Short versions return multiple logical-values.
Eg: `&`, `|`
• Element-wise comparisons are performed when comparing two vectors of equal
length.
• Element-wise comparisons return a vector of logical-values.

• Example:

`b1 <- c(T, F, F)`


`b2 <- c(F, T, F)`
`b1 & b2` returns `[F, F, F]`.
`b1 | b2` returns `[T, T, F]`.

ii) Long versions are for comparing individual values.


Long versions return a single logical-value.
Eg: `&&`, `||`
• Using long versions of logical operators evaluates only the first pair of logicals
in
two vectors.
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

• Example:
`b1 <- c(T, F, F)`
`b2 <- c(F, T, F)`
`b1 && b2` returns `F`.
`b1 || b2` returns `T`.

Logical Subsetting and Extraction


• Logical subsetting and extraction involve using logical conditions to select
elements
that satisfy a particular criterion.
• You create a logical vector with TRUE and FALSE values based on the
condition.
•Then, you use this vector to subset or extract elements.

Example:
# Creating a numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)

# Logical subsetting to extract even numbers


even_numbers <- numeric_vector[numeric_vector %% 2 == 0]
Output:
24
Explanation of above program:
• We have a numeric vector called numeric_vector.
• We use the logical condition numeric_vector %% 2 == 0 to create a logical
vector
with TRUE for even numbers and FALSE for odd numbers.
• We use this logical vector for subsetting, resulting in even_numbers containing
only
the even elements from numeric_vector.

String
• A string is a data type.
• It is used to represent text or character data.
• Strings can consist of almost any combination of characters, including numbers.
• Strings are commonly used for storing and manipulating textual information.
For e.g.: names, sentences, and text-data extracted from files or databases.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Creating a String
• You can create strings using single or double quotation marks.
• Example:
single_quoted <- 'This is a single-quoted string.'
double_quoted <- "This is a double-quoted string."

Finding String Length with `nchar() `


• nchar() can be used to determine the number of characters in a given string.
• It calculates and returns the length of a string in terms of the number of
characters

Example:
# Define a string
my_string <- "Hello, World!"

# Use nchar() to determine the length of the string


string_length <- nchar(my_string)

# Print the result


cat("The length of the string is:", string_length)
Output:
The length of the string is: 13

Explanation of above program:


• We define a string my_string containing the text "Hello, World!".
• We use the nchar() function to calculate the length of the string, including spaces
and special characters.
• The result is stored in the string_length variable.
• We then use cat() to display the length of the string.
Concatenation
Concatenation Functions
• Two main functions are used for concatenating strings: `cat` and `paste`.

1) Using the cat() Function


• cat() can be used for concatenating and printing strings with optional separators.
• Example:
# Concatenate and print two strings with a space separator
cat("Hello", "World")
Output:

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

"Hello World"

2) Using the paste() Function


• paste() can be used to concatenate multiple strings into one, with optional
separator and other arguments.
Example:
# Concatenate two strings with a space separator
concatenated <- paste("Hello", "World")
Output:
"Hello World"

cat() vs. paste()


• cat() is used for printing (displaying) text to the console.
It concatenates and prints its arguments with optional separators.
• paste() is used for concatenating strings into a single character vector.
The concatenated string can be assigned to a variable for further use.
It doesn't print directly to the console.

Separator (sep) Argument


• An optional argument `sep` is used as a separator between concatenated strings.
• Example:
# Concatenate two strings with a custom separator
concatenated <- paste("Hello", "World", sep = ", ")
#Output:
"Hello, World"

Escape Sequences
Backslash (\) Usage
• The backslash (\) is used to invoke an escape sequence.
• Escape sequences allow you to enter characters that control the format and
spacing of the string.
Common Escape Sequences
`\n` starts a newline.
`\t` represents a horizontal tab.
`\b` invokes a backspace.
`\\` is used to include a single backslash.
`\"` includes a double quote.

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Substrings and Matching


Pattern Matching
• Pattern matching allows you to inspect a given string to identify smaller strings
within it.
• substr() can be used to extract substrings within a given string
Example:
# Extracting a substring from a string
original_string <- "Hello, World!"
substring <- substr(original_string, start = 1, stop = 5)
Output:
"Hello"

sub() can be used for replacing the first occurrence of a pattern within a string
Example:
# Replacing the first occurrence of "apple" with "banana"
text <- "I like apples, but apples are red."
new_text <- sub("apple", "banana", text)
Output:
I like bananas, but apples are red."

gsub() can be used for replacing all occurrences of a pattern within a string.
Example:
# Replacing all occurrences of "apple" with "banana"
text <- "I like apples, but apples are red."
new_text <- gsub("apple", "banana", text)
Output:
I like bananas, but bananas are red."

Special Values
When a data set has missing observations or when a practically infinite number
is calculated the software has some unique terms reserved for these situations.
They are;
1) NA (Not Available):- If the value is not define, data value is out of range,
in such cases NA values be printed as output.
Example:
X< - c(1,2,3)
X[4]
Output: NA
BGS FIRST GRADE COLLEGE MYSURU SAHANA K
STATISTICA COMPUTING AND R PROGRAMMING

2) INF and -INF: When a number is to large for R to represent, the value is
given as Infinite.
Example:
r> 1 / 0
Output: INF

3) Nan (Not a Number): In some situations, it is impossible to express the


result of calculation using number, in such cases Nan is given as the output.
Example: factorial(-66)
s

4) NULL: This value is used to explicitly define an empty entity.


Example: f < - NULL
Print(f)

Output: NULL

Coercion
In R programming, converting from one object or data type to another object or
data type is referred as coercion.
Ther are two types of coercion. They are:
Implicit coercion: This type of coercion occurs automatically.
Example: The logical value True will be treated as 1 and False will be treated as
0.
Explicit coercion: This type of coercion can be done with the help of as.integer,
as.logical etc., functons.
Example:
X <- c(0,1,0,3)
Class (x)
Output: “numeric”
as.integer(x)
Class(x)
Output: “integer”

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

as.complex(x)
Class(x)
Output: “complex”

Basic plotting in R:

The plot() function is used to draw points (markers) in a diagram.

The function takes parameters for specifying points in the diagram.

Parameter 1 specifies points on the x-axis.

Parameter 2 specifies points on the y-axis.

Example

Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):

1) plot(c(1, 8), c(3, 10))

2) x<-c(1, 2, 3, 4, 5)
y<-c(3, 7, 8, 9, 12)

plot(x, y)

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Draw a Line

The plot() function also takes a type parameter with the value l to draw a line to
connect all the points in the diagram:

plot(1:10, type="l")

Plot Labels

The plot() function also accept other parameters, such as main, xlab and ylab if
you want to customize the graph with a main title and different labels for the x
and y-axis:

Example: plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Colors

Use col="color" to add a color to the points:

Example:

plot(1:10, col="red")

Size

Use cex=number to change the size of the points

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

Example: plot(1:10, cex=2)

Point Shape

Use pch with a value from 0 to 25 to change the point shape format:

Example: plot(1:10, pch=25, cex=2)

To change the colour of line


Example: plot(1:10, type=”l”, colour=”blue”)

BGS FIRST GRADE COLLEGE MYSURU SAHANA K


STATISTICA COMPUTING AND R PROGRAMMING

To change the width of the line:


Example: plot(1:10, type=”l”, lwd=2)

BGS FIRST GRADE COLLEGE MYSURU SAHANA K

You might also like