Certificate: Alard College of Business Studies
Certificate: Alard College of Business Studies
CERTIFICATE
--------------------------- -----------------------------
Signature of Examiner Signature of Examiner
(External) (internal)
--------------------- ----------------------
(Subject Prof.) (HOD)
INDEX
NAME-
SUBJECT-
SEMESTER- YEAR-
CLASS- ROLL NO-
EXAM SEAT NO-
SUBMISSION REPORT
Pract. Date Title Page Subject Teache
No. No. Signature
01 Write an R program to find the maximum and the
minimum value of a given vector.
02 Write an R program to sort a Vector in ascending and
descending order.
03 Write an R program to compare two data frames to find
the elements in first data frame that are not present in
second data frame.
04 Write an R program to extract first 10 English letter in
lower case and last 10 letters in upper case and extract
letters between 22nd to 24th letters in upper case.
05 Write an R program to find Sum, Mean and Product of a
Vector.
06 Write an R program to create a simple bar plot of five
subject’s marks.
07 Write an R program to create a Dataframes which
contain details of 5 employees and display the details in
ascending order.
08 Write an R program to create a data frame using two
given vectors and display the duplicated elements and
unique rows of the said data frame.
09 Write an R program to change the first level of a factor
with another level of a given factor.
10 Write a script in R to create a list of cities and perform
the following
11 Write a script in R to create two vectors of different
lengths and give these vectors as input to array and print
addition and subtraction of those matrices.
12 Write an R Program to calculate Multiplication Table
13 Consider the inbuilt iris dataset
i) Create a variable “y” and attach to it the output
attribute of the “iris” dataset.
ii) Create a barplot to breakdown your output attribute.
iii) Create a density plot matrix for each attribute by
class value.
14 Write an R program to concatenate two given factor in a
single factor and display in descending order.
15 Write an R program to extract the five of the levels of
factor created from a random sample from the LETTERS
16 Consider the inbuilt mtcar dataset
17 Write an R Program to calculate Decimal into binary of a
given number.
18 Write an R program to create three vectors a,b,c with 3
integers. Combine the three vectors to become a 3×3
matrix where each column represents a vector. Print the
content of the matrix.
19 Write an R program to draw an empty plot and an empty
plot specify the axes limits of the graphic.
20 Consider Weather dataset
i) Selecting using the column number
ii) Selecting using the column name
iii) Make a scatter plot to compare Wind speed and
temperature
21 Consider the plantGrowth inbuilt dataset
i) Create a variable “y” and attach to it the output
attribute of the “plantGrowth” dataset.
ii) Create a barplot to breakdown your output attribute.
iii) Create a density plot matrix for each attribute by
class value.
22 Write an R program to print the numbers from 1 to 100
and print "SY" for multiples of 3, print "BBA" for
multiples of 5, and print "SYBBA" for multiples both.
23 Write a script in R to create two vectors of different
lengths and give these vectors as input to array and print
second row of second matrix of the array.
24 Write a script in R to create two vectors of different
lengths and give these vectors as input to array and print
Multiplication of those matrices.
25 Write an R program to create a list of elements using
vectors, matrices and a functions. Print the content of
the list.
26 Write a script in R to create an array, passing in a vector
of values and a vector of dimensions. Also provide
names for each dimension.
27 Write an R Program to calculate binary into Decimal of a
given number.
28 Write an R program to convert a given matrix to a list
and print list in ascending order
29 Write a script in R to create a list of students and
perform the following
1) Give names to the students in the list.
2) Add a student at the end of the list.
3) Remove the firstStudent.
4) Update the second last student.
30 Write an R program to sort a list of 10 strings in
ascending and descending order.
Slip no: 1
Q 1) Write an R program to find the maximum and the minimum value of a given vector.
ANS=
> nums=c(10,20,30,40,50,60)
Output-:
[1] "original vector:"
> print(nums)
[1] 10 20 30 40 50 60
Ans=
> x=c(10,20,30,25,3,6)
Output-:
> print(sort(x))
[1] 3 6 10 20 25 30
> print(sort(x))
[1] 3 6 10 20 25 30
Slip 3
Q1) Write an R program to compare two data frames to find the elements in first
data frame that are not present in second data frame.
Solution
> df_90 = data.frame(
+ "item" = c("item1","item2","item3"),
+ "jan_sale" = c(12,14,12),
+ "feb_sale" = c(11,12,15),
+ "mar_sale" = c(12,14,15)
+)
> df_91 = data.frame(
+ "item" = c("item1","item2","item3"),
+ "jan_sale" = c(12,14,12),
+ "feb_sale" = c(11,12,15),
+ "mar_sale" = c(12,15,18)
+)
> print("original datframes:")
[1] "original datframes:"
> print(df_90)
item jan_sale feb_sale mar_sale
1 item1 12 11 12
2 item2 14 12 14
3 item3 12 15 15
> print(df_91)
item jan_sale feb_sale mar_sale
1 item1 12 11 12
2 item2 14 12 15
3 item3 12 15 18
> print("row(s)in first data frame that are not present in second data frame:")
[1] "row(s)in first data frame that are not present in second data frame:"
> print(setdiff(df_90,df_91))
$mar_sale
[1] 12 14 15
Slip 4
E) Write an R program to extract first 10 English letter in lower case and
last 10 letters in upper case and extract letters between 22nd to 24th
letters in upper case.
Solution:
print("First 10 letters in lower case:")
t = head(letters, 10)
print(t)
t = tail(LETTERS, 10)
print(t)
e = tail(LETTERS[22:24])
print(e)
Output-:
> print("First 10 letters in lower case:")
> print(t)
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
> print(t)
[1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
> e = tail(LETTERS[22:24])
> print(e)
Slip 5
E) Write an R program to find Sum, Mean and Product of a Vector.
Solution:
x = c(10, 20, 30)
print("Sum:")
print(sum(x))
print("Mean:")
print(mean(x))
print("Product:")
print(prod(x))
Output-:
> x = c(10, 20, 30)
> print("Sum:")
[1] "Sum:"
> print(sum(x))
[1] 60
> print("Mean:")
[1] "Mean:"
> print(mean(x))
[1] 20
> print("Product:")
[1] "Product:"
> print(prod(x))
[1] 6000
Slip 6
E) Write an R program to create a simple bar plot of five subject’s marks .
Solution:
marks = c(70, 95, 80, 74)
barplot(marks,
xlab = "Marks",
ylab = "Subject",
col = "darkred",
horiz = TRUE)
Output-:
marks = c(70, 95, 80, 74)
> barplot(marks,
+ xlab = "Marks",
+ ylab = "Subject",
+ col = "darkred",
+ horiz = TRUE)
Slip 7
Q.3 Write an R program to create a Dataframes which contain details of 5 employees and display the
details in ascending order.
Solution
Employees = data.frame(
Gender=c("M","M","F","M","F"),
Age=c(23,22,25,26,32),
SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","679-77-576")
print(Employees[with(Employees, order(Age)),])
OUTPUT
Name Gender Age Designation SSN
Solution
a = c(10,20,10,10,40,50,20,30)
b = c(10,30,10,20,0,50,30,30)
ab = data.frame(a,b)
print(ab)
print(duplicated(ab))
print(unique(ab))
Output:-
[1] "Original data frame:"
a b
1 10 10
2 20 30
3 10 10
4 10 20
5 40 0
6 50 50
7 20 30
8 30 30
[1] "Duplicate elements of the said data frame:"
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
[1] "Unique rows of the said data frame:"
a b
1 10 10
2 20 30
4 10 20
5 40 0
6 50 50
8 30 30
Slip 9
E) Write an R program to change the first level of a factor with another level of a
given factor.
Solution:
v = c("a", "b", "a", "c", "b")
print("Original vector:")
print(v)
f = factor(v)
print(f)
levels(f)[1] = "e"
print(f)
Output-:
list_data[3] <- "updated element"
> print(list_data[3])
$Capital
> print(v)
> f = factor(v)
> print(f)
[1] a b a c b
Levels: a b c
> print(f)
[1] e b e c b
Levels: e b c
>
Slip 10
E) Write a script in R to create a list of cities and perform the following 1) Give
names to the elements in the list. 2) Add an element at the end of the list. 3)
Remove the last element. 4) Update the 3rd Element
Solution:
list_data <- list("Pune","Nasik","Mumbai")
print(list_data)
print(list_data)
list_data[4]<-"New element"
print(list_data)
print(list_data)
print(list_data[3])
Output-:
[[1]]
[1] "Pune"
[[2]]
[1] "Nasik"
[[3]]
[1] "Mumbai"
>
> print(list_data)
$`Edu City`
[1] "Pune"
$`Grape City`
[1] "Nasik"
$Capital
[1] "Mumbai"
>
> print(list_data)
$`Edu City`
[1] "Pune"
$`Grape City`
[1] "Nasik"
$Capital
[1] "Mumbai"
[[4]]
>
> print(list_data)
$`Edu City`
[1] "Pune"
$`Grape City`
[1] "Nasik"
$Capital
[1] "Mumbai"
>
> print(list_data[3])
$Capital
E) Write a script in R to create two vectors of different lengths and give these
vectors as input to array and print addition and subtraction of those matrices.
Solution:
v1<-c(10,20,30,40)
v2<-c(2,3,4)
matrix1=array(c(v1,v2),dim=(c(2,2,2)))
print(matrix1)
matrix2=array(c(v2,v1),dim=(c(2,2,2)))
print(matrix2)
matrix1+matrix2
matrix1-matrix2
Output-:
,,1
[,1] [,2]
[1,] 10 30
[2,] 20 40
,,2
[,1] [,2]
[1,] 2 4
[2,] 3 10
> matrix2=array(c(v2,v1),dim=(c(2,2,2)))
> print(matrix2)
,,1
[,1] [,2]
[1,] 2 4
[2,] 3 10
,,2
[,1] [,2]
[1,] 20 40
[2,] 30 2
> matrix1+matrix2
,,1
[,1] [,2]
[1,] 12 34
[2,] 23 50
,,2
[,1] [,2]
[1,] 22 44
[2,] 33 12
> matrix1-matrix2
,,1
[,1] [,2]
[1,] 8 26
[2,] 17 30
,,2
[,1] [,2]
[1,] -18 -36
[2,] -27 8
Slip 12
C.Write an R Program to calculate Multiplication Table
Solution:
number <- as.integer(readline(prompt = "Please Enter a Number for Table"))
for( t in 1:10)
Output-:
+}
Q3 : Write an R program to concatenate two given factor in a single factor and display in descending
order.
print(combined_factor)
Output-:
[1] H G F E D C B A
Levels: A B C D E F G H
Slip no: 15
Q 15 - E) Write an R program to extract the five of the levels of factor created from a random sample
from the LETTERS
Solution:
L = sample(LETTERS,size=20,replace=TRUE)
print("Original data:")
print(L)
f = factor(L)
print("Original factors:")
print(f)
print(table(L[1:5]))
Output-:
L = sample(LETTERS,size=20,replace=TRUE)
> print(L)
[1] "H" "C" "A" "C" "X" "Q" "V" "D" "J" "Q" "Y" "P" "Z" "A" "Z" "P" "Q" "K" "P"
[20] "N"
> f = factor(L)
> print(f)
[1] H C A C X Q V D J Q Y P Z A Z P Q K P N
Levels: A C D H J K N P Q V X Y Z
Q 16) Consider the inbuilt mtcar dataset i) Subset the vector, “mtcars[,1]“, for values greater than “15.0“.
ii) Subset “airquality” for “Ozone” greater than “28“, or “Temp” greater than “70“. Return the first five
rows. iii) Subset “airquality” for “Ozone” greater than “100“. Select the columns “Ozone“, “Temp“,
“Month” and “Day” only.
Solution:
dim(airquality)
colnames(airquality)
subset(mtcars[,1],mtcars[,1]>15.0)
head(subset(airquality, Ozone>28|Temp>70))
Solution:
dim(airquality)
colnames(airquality)
subset(mtcars[,1],mtcars[,1]>15.0)
head(subset(airquality, Ozone>28|Temp>70))
subset(airquality,Ozone>100,select=c(Ozone,Temp,Month,Day))
[1] 153 6
> colnames(airquality)
> subset(mtcars[,1],mtcars[,1]>15.0)
[1] 21.0 21.0 22.8 21.4 18.7 18.1 24.4 22.8 19.2 17.8 16.4 17.3 15.2 32.4 30.4
[16] 33.9 21.5 15.5 15.2 19.2 27.3 26.0 30.4 15.8 19.7 21.4
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
11 7 NA 6.9 74 5 11
17 34 307 12.0 66 5 17
19 30 322 11.5 68 5 19
> subset(airquality,Ozone>100,select=c(Ozone,Temp,Month,Day))
30 115 79 5 30
62 135 84 7 1
86 108 85 7 25
99 122 89 8 7
Slip 17
Slip 17 - E) Write an R Program to calculate Decimal into binary of a given number.
Solution:
binary_conversion<-function(n){
if(n>1){
binary_conversion(as.integer(n/2))
cat(n%%2)
binary_conversion(13)
Output-:
> binary_conversion<-function(n){
+ if(n>1){
+ binary_conversion(as.integer(n/2))
+ }
+ cat(n%%2)
+}
> binary_conversion(13)
1101>
Slip 18
Slip 18 - E) Write an R program to create three vectors a,b,c with 3 integers.
Combine the three vectors to become a 3×3 matrix where each column represents
a vector. Print the content of the matrix.
Solution:
a<-c(10,20,30)
b<-c(40,50,60)
c<-c(70,80,90)
m<-cbind(a,b,c)
print("Content of the matrix:")
print(m)
Output-:
> a<-c(10,20,30)
> b<-c(40,50,60)
> c<-c(70,80,90)
> m<-cbind(a,b,c)
> print("Content of the matrix:")
[1] "Content of the matrix:"
> print(m)
a b c
[1,] 10 40 70
[2,] 20 50 80
[3,] 30 60 90
Slip 19
Slip 19 - E) Write an R program to draw an empty plot and an empty plot specify the
axes limits of the graphic.
Solution:
#print("Empty plot:")
plot.new()
#print("Empty plot specify the axes limits of the graphic:")
plot(1, type="n", xlab="", ylab="", xlim=c(0, 20), ylim=c(0, 20))
Output-:
Slip 20
Slip 20 - E) Consider Weather dataset i)
Selecting using the column number ii)
Selecting using the column name iii)
Make a scatter plot to compare Wind
speed and temperature
Solution:
library(devtools)
install_github("Ram-N/weatherData")
dim(London2013)
colnames(London2013)
London2013%>% select(1:2)
London2013%>% select(Time)
dim(airquality)
with(airquality,plot(Ozone ~ Temp))
Output-:
Slip 21
E) Consider the plantGrowth inbuilt dataset i) Create a variable “y” and attach to it
the output attribute of the “plantGrowth” dataset. ii) Create a barplot to break down
your output attribute. iii) Create a density plot matrix for each attribute by class
value.
Solution:
data(PlantGrowth)
dataset<-PlantGrowth
library(caret)
x<-dataset[,1:1]
y<-dataset[ ,2]
y
library(dplyr)
library(ggplot2)
dataset %>% ggplot(aes(x=y)) + geom_bar() + labs(x = "Iris Flower Species")
scales<-list(x = list(relation = "free"), y = list(relation = "free"))
featurePlot(x = x, y = y, plot = "density", scales = scales).
Output-:
Slip22
Q.3 Write an R program to create a data frame using two given vectors and display the duplicated
elements and unique rows of the said data frame.
Solution
a = c(10,20,10,10,40,50,20,30)
b = c(10,30,10,20,0,50,30,30)
ab = data.frame(a,b)
print(ab)
print(duplicated(ab))
print(unique(ab))
Output:-
[1] "Original data frame:"
a b
1 10 10
2 20 30
3 10 10
4 10 20
5 40 0
6 50 50
7 20 30
8 30 30
[1] "Duplicate elements of the said data frame:"
[1] FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
[1] "Unique rows of the said data frame:"
a b
1 10 10
2 20 30
4 10 20
5 40 0
6 50 50
8 30 30
Slip 23
-E) Write a script in R to create two vectors of different lengths and give these vectors as input to the
array and print second row of second matrix of the array.
Solution:
a=c(1,2,3,4,5)
b=c(2,4,5,7,8,6)
print(a)
print(b)
result=array(c(a,b),dim=c(3,3,2))
print("New Array")
print(result)
print(result[2,,2])
print(result[3,3,1])
output:-
> a=c(1,2,3,4,5)
> b=c(2,4,5,7,8,6)
> print(a)
[1] 1 2 3 4 5
> print(b)
[1] 2 4 5 7 8 6
> result=array(c(a,b),dim=c(3,3,2))
,,1
[1,] 1 4 4
[2,] 2 5 5
[3,] 3 2 7
,,2
[1,] 8 2 5
[2,] 6 3 2
[3,] 1 4 4
> print(result[2,,2])
[1] 6 3 2
> print(result[3,3,1])
[1] 7
>
Slip 24
E) Write a script in R to create two vectors of different lengths and give these vectors as input to array
and print Multiplication of those matrices.
Solution:
v1<-c(10,20,30,40)
v2<-c(2,3,4)
matrix1=array(c(v1,v2),dim=(c(2,2,2)))
print(matrix1)
matrix2=array(c(v2,v1),dim=(c(2,2,2)))
print(matrix2)
matrix1*matrix2
Output-:
> v1<-c(10,20,30,40)
> v2<-c(2,3,4)
> matrix1=array(c(v1,v2),dim=(c(2,2,2)))
> print(matrix1)
,,1
[,1] [,2]
[1,] 10 30
[2,] 20 40
,,2
[,1] [,2]
[1,] 2 4
[2,] 3 10
> matrix2=array(c(v2,v1),dim=(c(2,2,2)))
> print(matrix2)
,,1
[,1] [,2]
[1,] 2 4
[2,] 3 10
,,2
[,1] [,2]
[1,] 20 40
[2,] 30 2
> matrix1*matrix2
,,1
[,1] [,2]
[1,] 20 120
[2,] 60 400
,,2
[,1] [,2]
[1,] 40 160
[2,] 90 20
Slip 25
E) Write an R program to create a list of elements using vectors, matrices and a
functions. Print the content of the list .
Solution:
l=list(c(1,2,3,4,12,7),
month.abb,
matrix(c(3,-8,1,-3),nrow=2),
asin
print(l)
Output-:
[1] "Content of the list"
> print(l)
[[1]]
[1] 1 2 3 4 12 7
[[2]]
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
[[3]]
[,1] [,2]
[1,] 3 1
[2,] -8 -3
[[4]]
>
Slip 26
E) Write a script in R to create an array, passing in a vector of values and a vector of
dimensions. Also provide names for each dimension.
Solution:
a=array(6:30,
dim=c(4,3,2),
dimnames=list(
c("Col1","Col2","Col3","Col4"),
c("Row1","Row2","Row3"),
c("Part1","Part2")))
print(a)
Output-:
, , Part1
Col1 6 10 14
Col2 7 11 15
Col3 8 12 16
Col4 9 13 17
, , Part2
Col1 18 22 26
Col2 19 23 27
Col3 20 24 28
Col4 21 25 29
Slip 27
E) Write an R Program to calculate binary into Decimal of a given number.
Solution:
b=1111
decimal=0
r=0
base=1
while(b>0){
r=b%%10
decimal=decimal+r*base
b=b/10
b=as.integer(b)
base=base*2
base=as.integer(base)
print(decimal)
Output-:
b=1111
> decimal=0
> r=0
> base=1
> while(b>0){
+ r=b%%10
+ decimal=decimal+r*base
+ b=b/10
+ b=as.integer(b)
+ base=base*2
+}
> base=as.integer(base)
> print(decimal)
[1] 15
Slip 28
E) Write an R program to convert a given matrix to a list and print list in ascending
order.
Solution:
m=matrix(1:10,nrow=2,ncol=2)
print("Original matrix")
print(m)
l=split(m,rep(1:ncol(m),each=nrow(m)))
print(l)
Output-:
> m=matrix(1:10,nrow=2,ncol=2)
Warning message:
> print(m)
[,1] [,2]
[1,] 1 3
[2,] 2 4
> l=split(m,rep(1:ncol(m),each=nrow(m)))
> print(l)
$`1`
[1] 1 2
$`2`
[1] 3 4
E) Write a script in R to create a list of students and perform the following 1) Give
names to the students in the list. 2) Add a student at the end of the list. 3) Remove
the firstStudent. 4) Update the second last student.
Solution:
list_student<-list("Javed",c("Tushar","harsh","mihir"),"anurag","anhishek")
print(list_student)
names(list_student)<-c("a","b","c","d")
print(list_student)
list_student[5]="amod"
print(list_student)
list_student[1]=NULL
print(list_student)
list_student[3]="Taha"
print(list_student)
Output-:
> #update second last student
> list_student[3]="Taha"
> print(list_student)
$b
$c
[1] "anurag"
$d
[1] "Taha"
[[4]]
[1] "amod"
Slip 30
E) Write an R program to sort a list of 10 strings in ascending and descending order.
Solution:
a<-c("tushar","harsh","mihir","anurag","abhishek","rohit","ranjeet","Taha")
list_string=list(a)
print(list_string)
sort_string=sort(a)
print(sort_string)
Output-:
[[1]]
[8] "Taha"
> sort_string=sort(a)
> print(sort_string)
[8] "tushar"
>