Introduction To Data Science With R Programming Language Lab Record
Introduction To Data Science With R Programming Language Lab Record
INDEX
Sno Date Program Page Number
1 24/08/202 Basic programs of R and Sample Programs on
1 Arithmetic Operations on vectors-I
1. Addition
2. Subtraction 1
3. Multiplication
4. Division
5. Modulous
2 31/08/202 Basic programs of R and Sample Programs on
1 Arithmetic Operations on vectors-II
1. Floor Division 2
2. Exponent
[1] 6 9
[1] 2 1
[1] 8 20
[1] 0 1
1|Page
Program 2:
Basic programs of R and Sample Programs on Arithmetic Operations on vectors-II
1. Floor Division
2. Exponent
Program:
print("Floor Division of two vectors")
print(var1%/%var2)
print("Exponent")
print(var1^var2)
Output
[1] 2 1
[1] "Exponent"
[1] 16 625
2|Page
Program 3
Operation on Matrix: Addition
Program:
A=matrix(c(3,5,4,6,7,8,9,3,5),nrow=3, ncol=3, byrow=TRUE)
print("First 3x3 matrix")
print(A)
B=matrix(c(5,6,7,2,4,3,6,9,7),nrow=3, ncol=3, byrow=TRUE)
print("Second 3x3 matrix")
print(B)
print("result of addition of two matrices")
print(A+B)
Output:
[1,] 3 5 4
[2,] 6 7 8
[3,] 9 3 5
[1,] 5 6 7
[2,] 2 4 3
[3,] 6 9 7
[1,] 8 11 11
[2,] 8 11 11
[3,] 15 12 12
3|Page
Subtraction of Two matrices
print("result of subtraction of two matrices")
print(A-B)
Output:
[1,] 3 5 4
[2,] 6 7 8
[3,] 9 3 5
[1,] 5 6 7
[2,] 2 4 3
[3,] 6 9 7
[1,] -2 -1 -3
[2,] 4 3 5
[3,] 3 -6 -2
[1,] 3 5 4
[2,] 6 7 8
4|Page
[3,] 9 3 5
[1,] 5 6 7
[2,] 2 4 3
[3,] 6 9 7
[1,] 15 30 28
[2,] 12 28 24
[3,] 54 27 35
5|Page
Program 4:
Row Concatenation of two matrices
print("result of row concatenation of two matrices")
print(rbind(A,B))
Output:
[1,] 3 5 4
[2,] 6 7 8
[3,] 9 3 5
[1,] 5 6 7
[2,] 2 4 3
[3,] 6 9 7
[1,] 3 5 4
[2,] 6 7 8
[3,] 9 3 5
[4,] 5 6 7
[5,] 2 4 3
[6,] 6 9 7
6|Page
Deleting Second Column
A=matrix(c(3,5,4,6,7,8,9,3,5),nrow=3, ncol=3, byrow=TRUE)
print("Before deleting Second column")
print(A)
A=A[,-2]
print("After deleting Second column")
print(A)
Output:
[1,] 3 5 4
[2,] 6 7 8
[3,] 9 3 5
[,1] [,2]
[1,] 3 4
[2,] 6 8
[3,] 9 5
7|Page
[1,] 3 5 4
[2,] 6 7 8
[3,] 9 3 5
[1,] 3 5 4
[2,] 9 3 5
[1,] 3 5 4
[2,] 6 7 8
[3,] 9 3 5
[1,] 3 5 4
[2,] 11 12 15
[3,] 9 3 5
8|Page
Program 5:
Line Graph
A line chart is a graph that connects a series of points by drawing line segments between
them. These points are ordered in one of their coordinate (usually the x-coordinate) value.
Line charts are usually used in identifying the trends in data.
Syntax
The basic syntax to create a line chart in R is –
plot(v,type,col,xlab,ylab)
Program2:
marks=c(15,22,35,55,45,65)
9|Page
plot(marks,type = "o", col = "red", xlab = "Roll Number", ylab = "Marks in Statistics",
main = "Marks Obtained by Data Science Students")
Output
10 | P a g e
Bell Curves
Output:
11 | P a g e
Program 6:
Bar Chart
Program:
marks=c(92,50,45,73)
barplot(marks, main="Comparing marks of 5 subjects", xlab="marks", ylab="subjects",
names.arg = c("eng","comp","math"," r program"), col="blue",horiz=FALSE)
Output:
12 | P a g e
Pie Chart
Syntax:
The basic syntax for creating a pie-chart using the R is −
pie(x, labels, radius, main, col, clockwise)
Program
vtr=c(43,31,64,40,29)
names=c("london","paris","los angeles", "mexico","new york")
pie(vtr,labels=names,main="Favourable Places", col= rainbow(length(vtr)))
Output:
13 | P a g e
Program 7:
There are three types of loop in R programming:
1. for
2. while
3. repeat
1. for loop
Syntax:
for (value in sequence)
{
statement
}
Flow Chart:
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
14 | P a g e
for (day in week)
{
print(day)
}
Output:
[1] "Sunday"
[1] "Monday"
[1] "Tuesday"
[1] "Wednesday"
[1] "Thursday"
[1] "Friday"
[1] "Saturday"
While loop
Syntax:
while( condition)
{
statement
}
Flow chart:
Output:
[1] 120
15 | P a g e
Repeat Loop
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.
Syntax:
repeat
{
statement
if( condition )
{
break
}
}
Flow chart:
Program:
Program to display numbers from 1 to 5 using repeat loop in R.
val = 1
repeat
{
print(val)
val = val + 1
if(val > 5)
{
break
}
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
16 | P a g e
Program 8: Functions in R
Function Components
The different parts of a function are −
1. Function Name − This is the actual name of the function. It is stored in R environment
as an object with this name.
3. Function Body − The function body contains a collection of statements that defines
what the function does.
4. Return Value − The return value of a function is the last expression in the function
body to be evaluated.
R has many in-built functions which can be directly called in the program without defining
them first. We can also create and use our own functions referred as user defined functions.
Built-in Function
Simple examples of in-built functions are seq(), mean(), max(), sum(x) and paste(...) etc.
They are directly called by user written programs. You can refer most widely used R
functions.
17 | P a g e
18 | P a g e
19 | P a g e