[go: up one dir, main page]

0% found this document useful (0 votes)
16 views55 pages

Certificate: Alard College of Business Studies

The document is a certificate template from Alard College of Business Studies for students completing practical work in R programming. It includes a detailed index of various R programming tasks, such as finding maximum values, sorting vectors, and creating data frames. Additionally, it provides example solutions for each task, demonstrating the use of R for data manipulation and visualization.

Uploaded by

Vaibhav Dukare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views55 pages

Certificate: Alard College of Business Studies

The document is a certificate template from Alard College of Business Studies for students completing practical work in R programming. It includes a detailed index of various R programming tasks, such as finding maximum values, sorting vectors, and creating data frames. Additionally, it provides example solutions for each task, demonstrating the use of R for data manipulation and visualization.

Uploaded by

Vaibhav Dukare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

ALARD COLLEGE OF BUSINESS STUDIES

CERTIFICATE

This is to certify that Mr/Ms . _________________________________________________of

Class ____________________has completed his/her practical work for subject

___________________as a part of curriculum by Alard College of Business Studies ,Savitribai

Phule Pune University, Pune during the Academic year [ ]

--------------------------- -----------------------------
Signature of Examiner Signature of Examiner
(External) (internal)

------------------------ Date of Examination


(Principal) ---------------------------

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

> print('original vector:')

Output-:
[1] "original vector:"

> print(nums)

[1] 10 20 30 40 50 60

> print(paste("maximum value of the said vector:",max(nums)))

[1] "maximum value of the said vector: 60"

> print (paste("minimum value of the said vector:",min(nums)))

[1] "minimum value of the said vector: 10"


Slip No 2

Q1) Write an R program to sort a Vector in ascending and descending order.

Ans=

> x=c(10,20,30,25,3,6)

> print("original vector")

Output-:

[1] "original vector"

> print("sort in ascending order")

[1] "sort in ascending order"

> print(sort(x))

[1] 3 6 10 20 25 30

> print("sort in descending order")

[1] "sort in descending order"

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

print("Last 10 letters in upper case:")

t = tail(LETTERS, 10)

print(t)

print("Letters between 22nd to 24th letters in upper case:")

e = tail(LETTERS[22:24])

print(e)

Output-:
> print("First 10 letters in lower case:")

[1] "First 10 letters in lower case:"

> t = head(letters, 10)

> print(t)

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

> print("Last 10 letters in upper case:")

[1] "Last 10 letters in upper case:"

> t = tail(LETTERS, 10)

> print(t)

[1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"

> print("Letters between 22nd to 24th letters in upper case:")


[1] "Letters between 22nd to 24th letters in upper case:"

> 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,

main = "Comparing marks of 5 subjects",

xlab = "Marks",

ylab = "Subject",

names.arg = c("English", "Science", "Math.", "Hist."),

col = "darkred",

horiz = TRUE)

Output-:
marks = c(70, 95, 80, 74)

> barplot(marks,

+ main = "Comparing marks of 5 subjects",

+ xlab = "Marks",

+ ylab = "Subject",

+ names.arg = c("English", "Science", "Math.", "Hist."),

+ 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(

Name=c("Anant", "Divas", "Kartiki", "Jivan", "Leela"),

Gender=c("M","M","F","M","F"),

Age=c(23,22,25,26,32),

Designation=c("Clerk", "Manager", "Executive", "CEO", "ASSISTANT"),

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

2 Divas M 22 Manager 123-44-779

1 Anant M 23 Clerk 123-34-2346

3 Kartiki F 25 Executive 556-24-433

4 Jivan M 26 CEO 123-98-987

5 Leela F 32 ASSISTANT 679-77-576


Slip 8
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)

print("Original data frame:")

ab = data.frame(a,b)

print(ab)

print("Duplicate elements of the said data frame:")

print(duplicated(ab))

print("Unique rows of the said data frame:")

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("Factor of the said vector:")

print(f)

levels(f)[1] = "e"

print(f)
Output-:
list_data[3] <- "updated element"

> print(list_data[3])

$Capital

[1] "updated element"

> v = c("a", "b", "a", "c", "b")

> print("Original vector:")

[1] "Original vector:"

> print(v)

[1] "a" "b" "a" "c" "b"

> f = factor(v)

> print("Factor of the said vector:")

[1] "Factor of the said vector:"

> print(f)

[1] a b a c b

Levels: a b c

> levels(f)[1] = "e"

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

names(list_data)<-c("Edu City","Grape City","Capital")

print(list_data)

list_data[4]<-"New element"

print(list_data)

list_data[4] <- NULL

print(list_data)

list_data[3] <- "updated element"

print(list_data[3])

Output-:
[[1]]

[1] "Pune"

[[2]]
[1] "Nasik"

[[3]]

[1] "Mumbai"

>

> names(list_data)<-c("Edu City","Grape City","Capital")

> print(list_data)

$`Edu City`

[1] "Pune"

$`Grape City`

[1] "Nasik"

$Capital

[1] "Mumbai"

>

> list_data[4]<-"New element"

> print(list_data)

$`Edu City`

[1] "Pune"

$`Grape City`

[1] "Nasik"
$Capital

[1] "Mumbai"

[[4]]

[1] "New element"

>

> list_data[4] <- NULL

> print(list_data)

$`Edu City`

[1] "Pune"

$`Grape City`

[1] "Nasik"

$Capital

[1] "Mumbai"

>

> list_data[3] <- "updated element"

> print(list_data[3])

$Capital

[1] "updated element"


Slip 11

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)

print ( paste ( number, '*', t, '=', number * t))

Output-:
+}

[1] "NA * 1 = NA"

[1] "NA * 2 = NA"

[1] "NA * 3 = NA"

[1] "NA * 4 = NA"

[1] "NA * 5 = NA"

[1] "NA * 6 = NA"

[1] "NA * 7 = NA"

[1] "NA * 8 = NA"

[1] "NA * 9 = NA"

[1] "NA * 10 = NA"


Slip 13
E) 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 break down your output
attribute. iii) Create a density plot matrix for each attribute by class value .
Solution:
data(iris)
dataset<-iris
library(caret)
x<-dataset[,1:4]
y<-dataset[ ,5]
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-:
Slip no: 14

Q3 : Write an R program to concatenate two given factor in a single factor and display in descending
order.

# Creating two factors

factor1 <- factor(c("A", "B", "C", "D"))

factor2 <- factor(c("E", "F", "G", "H"))

# Concatenating two factors into a single factor

combined_factor <- factor(c(as.character(factor1), as.character(factor2)))

# Displaying the combined factor in descending order

combined_factor <- combined_factor[order(combined_factor, decreasing = TRUE)]

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("Only five of the levels")

print(table(L[1:5]))

Output-:
L = sample(LETTERS,size=20,replace=TRUE)

> print("Original data:")

[1] "Original data:"

> 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("Original factors:")

[1] "Original factors:"

> 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

> print("Only five of the levels")

[1] "Only five of the levels"

> print(table(L[1:5])) ACHX 1211


Slip no: 16

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

subset(airquality,Ozone>100,select=c(Ozone,Temp,Month,Day))Slip 16 - E) 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))

subset(airquality,Ozone>100,select=c(Ozone,Temp,Month,Day))

show me the output with this solution


Output
dim(airquality)

[1] 153 6

> colnames(airquality)

[1] "Ozone" "Solar.R" "Wind" "Temp" "Month" "Day"

> 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

> head(subset(airquality, Ozone>28|Temp>70))

Ozone Solar.R Wind Temp Month Day

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

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)

print("Original data frame:")

ab = data.frame(a,b)

print(ab)

print("Duplicate elements of the said data frame:")

print(duplicated(ab))

print("Unique rows of the said data frame:")

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("The second row of the second matrix of the array")

print(result[2,,2])

print("The third row of the third matrix of the array")

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

> print("New Array")

[1] "New Array"


> print(result)

,,1

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

[1,] 1 4 4

[2,] 2 5 5

[3,] 3 2 7

,,2

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

[1,] 8 2 5

[2,] 6 3 2

[3,] 1 4 4

> print("The second row of the second matrix of the array")

[1] "The second row of the second matrix of the array"

> print(result[2,,2])

[1] 6 3 2

> print("The third row of the third matrix of the array")

[1] "The third row of the third matrix of the array"

> 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("Content of the list")

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]]

function (x) .Primitive("asin")

>
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

Row1 Row2 Row3

Col1 6 10 14

Col2 7 11 15

Col3 8 12 16

Col4 9 13 17

, , Part2

Row1 Row2 Row3

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("list of matrix is")

print(l)

Output-:
> m=matrix(1:10,nrow=2,ncol=2)

Warning message:

In matrix(1:10, nrow = 2, ncol = 2) :

data length differs from size of matrix: [10 != 2 x 2]

> print("Original matrix")

[1] "Original matrix"

> print(m)

[,1] [,2]

[1,] 1 3

[2,] 2 4

> l=split(m,rep(1:ncol(m),each=nrow(m)))

> print("list of matrix is")

[1] "list of matrix is"

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

#ADDING ELEMENT at END

list_student[5]="amod"

print(list_student)

#remove 1st student

list_student[1]=NULL

print(list_student)

#update second last student

list_student[3]="Taha"

print(list_student)

Output-:
> #update second last student

> list_student[3]="Taha"

> print(list_student)

$b

[1] "Tushar" "harsh" "mihir"

$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("sorted list/vector is")

print(sort_string)

Output-:
[[1]]

[1] "tushar" "harsh" "mihir" "anurag" "abhishek" "rohit" "ranjeet"

[8] "Taha"

> sort_string=sort(a)

> print("sorted list/vector is")

[1] "sorted list/vector is"

> print(sort_string)

[1] "abhishek" "anurag" "harsh" "mihir" "ranjeet" "rohit" "Taha"

[8] "tushar"

>

You might also like