You are on page 1of 21

INDEX

LIST OF
S.NO DATE GRADE REMARKS SIGNATURE
EXPRIEMENTS
Download and install
R-Programming
1 environment and install
basic packages using
install.packages()
command in R.
. Learn all the basics of
R-Programming (Data
2 types, Variables,
Operators etc.)

Implement R-Loops
3 with different
examples.

Learn the basics of


functions in R and
4 implement with
examples.

Implement different
data structures in R
5 (Vectors, Lists, Data
Frames)

Implementation of
6 vector data objects
operations

Implementation of
matrix, array and
7 factors and perform in
R

Implementation and
8 use of data frames in R

Create Sample Data in


9 R and perform data
manipulation with R
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
Study and
implementation of
10 various control
structures in R .

11 Create pie charts and


bar charts using R.

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
EXPERIMENT NUMBER- 1
OBJECTIVE: Download and install R-Programming environment and install
basic packages using install.packages() command in R.

Run Time Environment:-


Runtime environment takes its meaning in the context of runtime - the lifecycle.
Therefore, runtime environment is the environment in which a program or
application is executed. It's the hardware and software infrastructure that supports
the running of a particular codebase in real time. A runtime environment loads
applications and has them run on a platform - hardware and software architecture
that acts as a foundation upon which other applications, processes, or technologies
are developed. All the resources necessary for running a program independently of
the operating system (OS) are available on the platform. Practically speaking,
a runtime environment is a piece of software that is designed to run other software.

Need of Run Time Environment:-


We use a variety of computer programs every day, for tasks like photo editing, word
processing, and calculation. It's expected that these programs run as fast and
smoothly as possible under a variety of conditions. Since operating systems can
differ significantly from each other, and even the same OS has many different
versions, it's necessary for developers to adapt programs to each OS by using runtime
environment.
Therefore, runtime environment provides the following advantages:
Cross-platform functionality: Enables cross-platform functionality for applications, which
simplifies the development process since the program does not need to be adapted to
different OS.
Identical user interfaces: Allows programs to have identical user interfaces regardless of
whether they're run on Windows, macOS, or Linux.
Conservation of resources: Allows similar applications to use the same runtime
environments and share common components.

How Run Time Environment Works:-


Your code is just code. Whatever you write, in whatever language you choose, needs
to eventually execute on a computer. An application that's currently running
interacts with the runtime environment via a runtime system. The runtime
environment, therefore, acts as the middleman between the application and the
operating system.
As soon as a program is executed, the runtime environment sends instructions to the
computer's processor and RAM, and accesses system resources. A runtime
environment provides various basic functions for memory, networks, and hardware.
These functions are carried out by the runtime environment instead of the application

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
and work independently of the OS. They include reading and writing files, managing
input and output devices, searching and sorting files, and transporting data via networks.

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
EXPERIMENT NUMBER- 2.
Objective: . Learn all the basics of R-Programming (Data types, Variables, Operators etc.)
.

A data type is an attribute associated with a piece of data that tells a


computer system how to interpret its value. Understanding data types ensures
that data is collected in the preferred format and the value of each property is
as expected.

For example, knowing the data type for “Ross, Bob” will help a computer
know:

 whether the data is referring to someone’s full name (“Bob Ross”)

 or a list of two names (“Bob” and “Ross”)

Variable:-

Variables are used to store information to be referenced and manipulated in a


computer program. They also provide a way of labeling data with a descriptive
name, so our programs can be understood more clearly by the reader and
ourselves. It is helpful to think of variables as containers that hold information.
Their sole purpose is to label and store data in memory. This data can then be
used throughout your program.

Naming variables is known as one of the most difficult tasks in computer


programming. When you are naming variables, think hard about the names. Try
your best to make sure that the name you assign your variable is accurately
descriptive and understandable to another reader. Sometimes that other reader
is yourself when you revisit a program that you wrote months or even years
earlier.

Operators:-

In mathematics and computer programming, an operator is a character that


represents a specific mathematical or logical action or process. For
instance, "x" is an arithmetic operator that indicates multiplication, while
"&&" is a logical operator representing the logical AND function in
programming.

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

Depending on its type, an operator manipulates an arithmetic or logical


value, or operand, in a specific way to generate a specific result. From
handling simple arithmetic functions to facilitating the execution of complex
algorithms, like security encryption, operators play an important role in the
programming world.

Mathematical and logical operators should not be confused with a system


operator, or sysop, which refers to a person operating a server or the
hardware and software in a computing system or network.

EXPERIMENT NUMBER- 3.

OBJECTIVE: Implement R-Loops with different examples.


Loop:-
In computer Programming, a Loop is used to execute a group of instructions or a
block of code multiple times, without writing it repeatedly. The block of code is
executed based on a certain condition. Loops are the control structures of a program.
Using Loops in computer programs simplifies rather optimizes the process of coding.
Kids may come across the question ‘what is Loop’ when they begin to learn computer
Programming languages like C or Java. Well, to understand ‘what is Loop’ they will
have to learn about the structure of various Loops in detail.

The structure of a Loop can be virtually divided into two parts, namely the control
statement, and the body. The control statement of a Loop comprises the conditions
that have to be met for the execution of the body of the Loop. For every iteration of
the Loop, the conditions in the control statement have to be true. The body of a Loop
comprises the block of code or the sequence of logical statements that are to be
executed multiple times. There are two types of Loops in Python, namely, For Loop,
and While Loop. When a Loop is written within another Loop, the control structure is
termed as a nested Loop.
In R programming, we require a control structure to run a block of code
multiple times. Loops come in the class of the most fundamental and strong
programming concepts. A loop is a control statement that allows multiple
executions of a statement or a set of statements. The word ‘looping’ means
cycling or iterating.
A loop asks a query, in the loop structure. If the answer to that query
requires an action, it will be executed. The same query is asked again and
again until further action is taken. Any time the query is asked in the loop, it
is known as an iteration of the loop. There are two components of a loop,

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
the control statement, and the loop body. The control statement controls the
execution of statements depending on the condition and the loop body
consists of the set of statements to be executed.
In order to execute the identical lines of code numerous times in a program,
a programmer can simply use a loop.
There are three types of loop in R programming:
 For Loop
 While Loop
 Repeat Loop
For Loop in R
It is a type of control statement that enables one to easily construct a loop
that has to run statements or a set of statements multiple times. For loop is
commonly used to iterate over items of a sequence. It is an entry controlled
loop, in this loop the test condition is tested first, then the body of the loop is
executed, the loop body would not be executed if the test condition is false.

R – For loop Syntax:

for (value in sequence)


{
statement
}

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
For Loop Flow Diagram:

Example 1: Program to display numbers from 1 to 5 using for loop in R.


 R

# R program to demonstrate the use of for loop

# using for loop

for (val in 1: 5)

# statement

print(val)

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

While Loop in R
It is a type of control statement which will run a statement or a set of
statements repeatedly unless the given condition becomes false. It is also an
entry controlled loop, in this loop the test condition is tested first, then the
body of the loop is executed, the loop body would not be executed if the test
condition is false.

R – While loop Syntax:

while ( condition )
{
statement
}
While loop Flow Diagram:

Below are some programs to illustrate the use of the while loop in R
programming.
Example 1: Program to display numbers from 1 to 5 using while loop in R.
 R

# R program to demonstrate the use of while loop

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

val = 1

# using while loop

while (val <= 5)

# statements

print(val)

val = val + 1

Repeat Loop in R
It is a simple loop that will run the same statement or a group of statements
repeatedly until the stop condition has been encountered. Repeat loop does
not have any condition to terminate the loop, a programmer must specifically
place a condition within the loop’s body and use the declaration of a break
statement to terminate this loop. If no condition is present in the body of the
repeat loop then it will iterate infinitely.

R – Repeat loop Syntax:

repeat
{
statement

if( condition )
{
break
}
}

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
Repeat loop Flow Diagram:

To terminate the repeat loop, we use a jump statement that is


the break keyword. Below are some programs to illustrate the use of repeat
loops in R programming.
Example 1: Program to display numbers from 1 to 5 using repeat loop in R.
 R

# R program to demonstrate the use of repeat loop

val = 1

# using repeat loop

repeat

# statements

print(val)

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

val = val + 1

# checking stop condition

if(val > 5)

# using break statement

# to terminate the loop

break

EXPERIMENT NUMBER- 4

OBJECTIVE: Learn the basics of functions in R and implement with examples

Functions are useful when you want to perform a certain task multiple times.
A function accepts input arguments and produces the output by executing
valid R commands that are inside the function. 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 function definitions in a single R file.
Types of function in R Language
 Built-in Function: Built function R is sq(), mean(), max(), these
function are directly call in the program by users.
 User-defined Function: R language allow us to write our own
function.

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
Functions in R Language

Functions are created in R by using the command function(). The general


structure of the function file is as follows:

Note: In the above syntax f is the function name, this means that you are
creating a function with name f which takes certain arguments and executes
the following statements.
Built-in Function in R Programming Language
Here we will use built-in function like sum(), max() and min().

 R

# 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))

User-defined Functions in R Programming Language


R provides built-in functions like print(), cat(), etc. but we can also create
our own functions. These functions are called user-defined functions.
Example:
 R

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

# A simple R function to check

# whether x is even or odd

evenOdd = function(x){

if(x %% 2 == 0)

return("even")

else

return("odd")

print(evenOdd(4))

print(evenOdd(3))

EXPERIMENT NUMBER- 5

OBJECTIVE: Implement different data structures in R (Vectors, Lists, Data Frames)


A data structure is a particular way of organizing data in a computer so that it
can be used effectively. The idea is to reduce the space and time
complexities of different tasks. Data structures in R programming are tools
for holding multiple values.
R’s base data structures are often organized by their dimensionality (1D, 2D,
or nD) and whether they’re homogeneous (all elements must be of the
identical type) or heterogeneous (the elements are often of various types).
This gives rise to the six data types which are most frequently utilized in data
analysis.
The most essential data structures used in R include:

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
 Vectors
 Lists
 Dataframes
 Matrices
 Arrays
 Factors

EXPERIMENT NUMBER- 6

OBJECTIVE: Implementation of vector data objects operations

Vectors

A vector is an ordered collection of basic data types of a given length. The


only key thing here is all the elements of a vector must be of the identical
data type e.g homogeneous data structures. Vectors are one-dimensional
data structures.
Example:

# R program to illustrate Vector

# Vectors(ordered collection of same data type)

X = c(1, 3, 5, 7, 8)

# Printing those elements in console

print(X)

EXPERIMENT NUMBER- 7

OBJECTIVE Implementation of matrix, array and factors and perform in R

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
Matrices in R

Matrix in R is a table-like structure consisting of elements arranged in a fixed


number of rows and columns. All the elements belong to a single data type.
R contains an in-built function matrix() to create a matrix. Elements of a
matrix can be accessed by providing indexes of rows and columns. The
arithmetic operation, addition, subtraction, and multiplication can be
performed on matrices with the same dimensions. Matrices can be easily
converted to data frames CSVs.

Syntax:
matrix(data, nrow, ncol, byrow)
Parameters:
data: contain a vector of similar data type elements.
nrow: number of rows.
ncol: number of columns.
byrow: By default matrices are in column-wise order. So this parameter
decides how to arrange the matrix
Example:-

A = matrix(
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8, 9),

# No of rows and columns


nrow = 3, ncol = 3,

# By default matrices are


# in column-wise order
# So this parameter decides
# how to arrange the matrix
byrow = TRUE
)

print(A)

EXPERIMENT NUMBER- 8

OBJECTIVE :Implementation and use of data frames in R

Data Frames
Data Frames are data displayed in a format as a table.

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
Data Frames can have different types of data inside it. While the first column
can be character, the second and third can be numeric or logical. However,
each column should have the same type of data.

Use the data.frame() function to create a data frame:

# Create a data frame


Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)

# Print the data frame


Data_Frame

EXPERIMENT NUMBER- 9

OBJECTIVE :Create Sample Data in R and perform data manipulation with R

Data Manipulation
Data manipulation involves modifying data to make it easier to read and to be more
organized. We manipulate data for analysis and visualization. It is also used with the
term ‘data exploration’ which involves organizing data using available sets of
variables.
At times, the data collection process done by machines involves a lot of errors and
inaccuracies in reading. Data manipulation is also used to remove these
inaccuracies and make data more accurate and precise.

Enroll yourself in R Programming Training and give a head-start to your career


in R!

For example:
We will use the default iris table in R, as follows:
#To load datasets package
library("datasets")
#To load iris dataset
data(iris)
summary(iris)

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

Output:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa: 50
1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 versicolor:0.300 versicolor:50
Median: 5.800 Median: 3.000 Median: 4.350 Median: 1.300 Virginica: 50
Mean: 5.843 Mean: 3.057 Mean: 3.758 Mean: 1.199
3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500

EXPERIMENT NUMBER- 10

OBJECTIVE: Study and implementation of various control structures in R .


Control structures in R allow you to control the flow of execution of a series of R
expressions. Basically, control structures allow you to put some “logic” into your R
code, rather than just always executing the same R code every time. Control
structures allow you to respond to inputs or to features of the data and execute
different R expressions accordingly.

Commonly used control structures are

 if and else: testing a condition and acting on it


 for: execute a loop a fixed number of times
 while: execute a loop while a condition is true
 repeat: execute an infinite loop (must break out of it to stop)
 break: break the execution of a loop
 next: skip an interation of a loop
Most control structures are not used in interactive sessions, but rather when
writing functions or longer expresisons. However, these constructs do not have to
be used in functions and it’s a good idea to become familiar with them before we
delve into functions.

Control structures like if, while, and for allow you to control the flow of an R
program

Infinite loops should generally be avoided, even if (you believe) they are
theoretically correct.

Control structures mentioned here are primarily useful for writing programs; for
command-line interactive work, the “apply” functions are more useful.

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT
EXPERIMENT NUMBER- 11
OBJECTIVE: Create pie charts and bar charts using R.

A pie chart is a circular statistical graphic, which is divided into slices to


illustrate numerical proportions. It depicts a special chart that uses “pie
slices”, where each sector shows the relative sizes of data. A circular chart
cuts in a form of radii into segments describing relative frequencies or
magnitude also known as a circle graph.
R – Pie Charts
R Programming Language uses the function pie() to create pie charts. It
takes positive numbers as a vector input.
Syntax: pie(x, labels, radius, main, col, clockwise)
 x: This parameter is a vector that contains the numeric values which
are used in the pie chart.
 labels: This parameter gives the description to the slices in pie chart.
 radius: This parameter is used to indicate the radius of the circle of
the pie chart.(value between -1 and +1).
 main: This parameter is represents title of the pie chart.
 clockwise: This parameter contains the logical value which indicates
whether the slices are drawn clockwise or in anti clockwise direction.
 col: This parameter give colors to the pie in the graph.

Creating a simple pie chart

To create a simple pie chart:


 By using the above parameters, we can draw a pie chart.
 It can be described by giving simple labels.
Example:
 R

# Create data for the graph.

geeks<- c(23, 56, 20, 63)

labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

# Plot the chart.

pie(geeks, labels)

Output:

Pie chart including the title and colors

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20
SAGAR INSTITUTE OF RESEARCH & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING / IT

KailodKartal,Rau Bypass Road,Indore–452020, M.P.(INDIA)


Student Name: Sem: Subject Name: Sub Code: 2019-20

You might also like