0% found this document useful (0 votes)
29 views36 pages

DA Practical File

Data analytics

Uploaded by

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

DA Practical File

Data analytics

Uploaded by

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

Practical File

Of
Data Analytics Using R
(PCC-CSEAI204-P)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

GURU JAMBHESHWAR UNIVERSITY OF SCIENCE


AND TECHNOLOGY
Hisar – Haryana (India)
Submitted To: Submitted By: Sarita
Dr. Anupma Sangwan Roll Number: 220010150027
Department of CSE Class: B.Tech (CSE-AI & ML)
INDEX

Page Teacher’s
Sr. Program
No. Signature
No
1 Install R and then install RStudio. Get yourself acquainted 5-8
with the GUI of various working windows of RStudio.
2 Create the following objects in R and then check their 9-10
class:
(a). A vector of strings
(b). A vector consisting of factor type data. For instance,
vector consisting of hair color of a few individuals.
(c). A list data type consisting of vectors of names of five
students and a matrix of the marks of students in four
courses.
(d). A data frame consisting of names of students, their
age, total marks, and grades awarded.
3 Apply str and summary commands to the objects created in 11-12
assignment 2. Interpret the output.
4 Check and justify the outcome of the following 13
expressions:
(a). sqrt(3) ^2 ==3
(b). near(sqrt(3)^2,3)
5 Install, load package ‘stringdist’ and run the following code: 14
my_strings = c(“Viraj”, “Viraj”, “Viraj”, “Vikraj”, “Viraji”,
“Viroj”, “Vroj”, “Siroji”)
name =“Viraj”
matched = (stringdist(my_string, name) == 0) matched =
(stringdist(my_string, name) == 1) matched =
(stringdist(my_string, name) == 2)
Interpret the output. [Hint: ‘stringdist’ is a package to find the
distance between strings in term of replacement, insertion and
declaration of letters.
6 Apply summary command to iris dataset of the ‘datasets’ 15
package and interprets the output.
7 Use plot(iris) function and interpret the output. Write 16-17
down your findings about the dataset.

1
8 Install and load the MASS package and access the Boston 18
dataset. Study the dataset from the resources available on the
internet and write what you can find relevant to the
dataset
9 Write a script file to compute the following of the numeric 19
variable in Boston dataset.
(a). Sum
(b). Range
(c). Mean
(d). Standard deviation
10. Create a vector x of all those values from 1:100 that are 20-21
divisible by 5 and do the following operations on the vector:
(a). Find the length of vector x.
(b). Print the values stored at the fifth, tenth, and fifteenth
location of vector x.
(c). Find the sum mean range median and standard deviation
of vector x.
(d). Replace the fifth and tenth values with NA and NaN
values, respectively and find the mean of modified vector.
(e). Check if x contains any NA values and print the indices
of NA values in vector x.
(f). Remove NA values from vector x and use summary
command on it.
(g). Print the values of first and third quartile of vector x from the
output of the summary command.
11. Assume the given vectors and do the following 22-23
operations:
x = 1:12; y = 13:24; z = 1:6; a = 1:12; c = (5,10, 15, NA, 25,
NaN); b = (13, 15, 17, 19, 20, 21, 23, 25, 27, 29, 31, 24);
v = (26, 21, 87, 56, 72, 60); k = (0, 2, 4, 6, 8, 16, 32)
(a). Find x × y and x × y × z and interpret the output.
(b). Do an element-wise comparison between x and a, and y
and b.
(c). Find all the elements that are greater than 6 of vector x
and store these elements into another vector p.
(d). Check for NA and NaN values in vectors b and c.
(e). Check if overall vector x is equal to vector a and vector b.
(f). Why does identical(x, z) evaluate to FALSE?
(g). What is the difference between all() and all.equal()
functions? Illustrate with the help of an example. (h). Run
any(x, z) function and interpret the out.

2
(h). Create a new vector of the non-NA values of vector c
using a single line code.
Sort vector v in descending order and output the original
indices in order of the sorted elements. Find log to the base 2
of vector k.

12. Assuming the character vector cv = c(“sunita”, “bimla”, 24


“kavita”, “geeta”, “anu”, “dikshita”, “susmita”, “seema”): (a).
Find the character count in each name.
(b). Find the geeta exist in vector cv
13. Output the indices of the names that contain substring ee in 25
vector cv of assignment 12
14. Find out how many strings end with the letters ta in vector cv 25
of in assignment 12.
15. Create a vector to store the grades of 20 students for the first 26
minor exam. Grades are given at four levels (A, B, C, D).
Compute the modal grade. Further, store the grades of the
same students for the second minor exam. Compare the
grades for the two exams. Count the number of students who
have got a higher grade in the second minor.
16. Create a matrix m of five rows in a row-major order of 27-29
numbers from 1 to 100 incremented by a step of 5 units:
(a) Find row and column-wise mean of matrix m.
(b) Find the minimum value for each row and column.
(c) Find the transpose and sort the values in each column in
decreasing order.
(d) Assign the row names as R1 to R5 and column names as
C1 to C4.
(e) Display all the elements of the second and fourth column
without using indices.
(f) Display all the elements of the first and third row without
using indices.
(g) Create a new matrix by deleting the second and fourth
column of matrix m using indices and column names.
(h) Replace elements at indices (2, 3), (2, 4), (3, 3), and (3,
4) with NA values.
(i) Replace element at index (1, 3) with NaN.
(j) Check if matrix m contains any NA or NaN values and
interpret the output.
(k) Create two new matrices rm and cm by concatenating
matrix m row-wise and column-wise with itself.

3
17. Interpret the output of the following commands: 30-31
(a) n = matrix(rep(m, 2), nrow= ncol(m), byrow= FALSE)
(b) n = matrix(rep(m, 2), nrow= nrow(m), byrow= FALSE)
(c) n = matrix(rep(m, 2), nrow= ncol(m), byrow = TRUE)
(d) m1 = do.call(rbind, replicate(2, m, simplify = FALSE))
(e) m2 = do.call(cbind, replicate (2, m, simplify FALSE)) (f)
Rename row and column names as per the requirements of
matrix m1 and m2.
18. Create a 4 x 3 matrix A of normally distributed random 32
numbers with mean 100 and standard deviation 10. Create
another 3 x 4 matrix B with normally distributed random
numbers with mean 10 and standard deviation 1. Perform
matrix multiplication of the two matrices and store the result
in a third matrix C rounded up to two decimal places.
19. Create a 4 x 3 matrix A of uniformly distributed random 33
integer numbers between 1 to 100. Create another 3 x 4
matrix B with uniformly distributed random integer numbers
between 1 to 10. Perform matrix multiplication of the two
matrices and store the result in a third matrix C.
20. Replicate the resulting matrix C obtained in Exercise 19 34
twice vertically.
21. Create A and B, two 4 x 3 matrices of normally distributed 35
random numbers, with mean 0 and standard deviation 1.
Find the indices of all those numbers in matrix A which are
less than the respective numbers in matrix B and print these
numbers.
kkuihji

4
1. Install R and then install RStudio. Get yourself acquainted with
the GUI of various working windows of RStudio.

1. To install R, go to cran.r-project.org

2. Depending on your operating system, click Download R for (your operating system).

3. Click on install R for the first time.

4. Click Download R for Windows. Open the downloaded file.

5
5. Select the language you would like to use during the installation. Then click OK.

6. Click Next.

7. Select where you would like R to be installed. It will default to your Program Files on
your C Drive. Click Next.

8. You can then choose which installation you would like.

6
9. Then specify if you want to customized your startup or just use the defaults. Then
click Next.

11. Then you can choose the folder that you want R to be saved within or the default if
the R folder that was created. Once you have finished, click Next.

12. You can then select additional shortcuts if you would like. Click Next.

13. Click Finish.

14. Next, download RStudio. Go to www.rstudio.com

15. Click Download RStudio.

16. Click Download under RStudio Desktop- Open Source License.

7
17. Click on the operating system that you are working with.

18. The RStudio installation wizard will pop-up. Click Next and go through the
installation steps.

19. Congratulations! You have now installed R and RStudio.

8
2. Create the following objects in R and then check their class
(a). A vector of strings
(b). A vector consisting of factor type data. For instance, a vector
consisting of hair color of a few individuals.
(c). A list data type consisting of vectors of names of five students
and a matrix of the marks of students in four courses.
(d). A data frame consisting of names of students, their age, total
marks, and grades awarded.

#a
q<-c("Shubham","Arpita","Nisha","Gunjan","Vaishali","Sumit") #String type
vector
class(q) #Checking class of the object

#b
hc <- c("Black", "Brown", "Brown", "Black", "Grey") #Creating vector for storing hair colour
hcf <- factor(hc) #Converting into factor class
hcf #Printing the object
class(hc)
class(hcf)

#c
slst = list(c("Harshita", "Kiran", "Sonia", "Neha", "Anuradha"),
matrix(c(30,27,36,35,32,37,35,36,34,29,31,38,38,28,29,34,35,36,36,37), nrow = 4, dimnames =
list(c("Maths", "English", "Science", "CS"),c("Harshita", "Kiran", "Sonia", "Neha",
"Anuradha"))))
slst #Creating list for storing names of students and marks in 4 subjects
class(slst)

#d
sname = c("Harshita", "Kiran", "Sonia", "Neha", "Anuradha")
sage = c(18, 18, 19, 19, 18)
tmarks = c( 447, 456, 444, 449, 452)
grade = c("A","A+","A","A","A+")
sdf = data.frame(sname, sage, tmarks, grade) #Creating data frame to store students record
sdf
class(sdf)

9
Output:

10
3.Apply str and summary commands to the objects created in assignment 2.
Interpret the output.

str(q)
summary(q)
str(hcf)
summary(hcf)
str(slst)
summary(slst)
str(sdf)
summary(sdf)

Output:

11
Interpretation :-
The `str()` and `summary()` commands provide information about the structure and summary
statistics of the objects created in Assignment 2.

1. For the object `q` (a character vector):


- `str(q)` shows that it's a character vector with 6 elements containing names.
- `summary(q)` provides information about its length (6) and indicates it's a character class.

2. For the object `hcf` (a factor):


- `str(hcf)` reveals it's a factor with 3 levels: Black, Brown, and Grey.
- `summary(hcf)` shows the count of each level: Black (2), Brown (2), and Grey (1).

3. For the object `slst` (a list of two elements):


- `str(slst)` indicates that it's a list containing a character vector and a numeric matrix.
- `summary(slst)` provides the length of each element in the list.

4. For the object `sdf` (a data frame with 4 variables):


- `str(sdf)` describes it as a data frame with 5 observations and 4 variables.
- `summary(sdf)` offers summary statistics for each variable: `sname` (character), `sage`
(numeric), `tmarks` (numeric), and `grade` (character).

In summary, the `str()` function provides the structure of the objects, indicating their data types
and content. The `summary()` function gives summary statistics specific to the data type, such
as levels and counts for factors and basic statistics for numeric variables.

12
4. Check and justify the outcome of the following expressions:
(a). sqrt(3) ^2 ==3
(b). near(sqrt(3)^2,3)

library(dplyr)
sqrt(3)^2==3 #`sqrt(3)^2 == 3` compares the square of the square root of 3 to 3.
It returns `FALSE` because of floating-point precision issues.

near(sqrt(3)^2,3) #'near(sqrt(3)^2, 3)' uses the 'near()' function to account for floating-point
errors, returning `TRUE` as the values are considered "near" to each other.

Output:

13
5. Install, load package ‘stringdist’ and run the following code:
my_strings = c(“Viraj”, “Viraj”, “Viraj”, “Vikraj”, “Viraji”, “Viroj”,
“Vroj”, “Siroji”)
name =“Viraj”
matched = (stringdist(my_string, name) == 0)
matched = (stringdist(my_string, name) == 1)
matched = (stringdist(my_string, name) == 2)
Interpret the output. [Hint: ‘stringdist’ is a package to find the
distance between strings in term of replacement, insertion and
declaration of letters.

Output:

Interpretation :-
In the code, the `stringdist` library is used to calculate string distances between the elements in
the `mystring` vector and the target string `name`, which is "Viraj."

- `stringdist(mystring, name) == 0` checks for exact matches, resulting in `TRUE` for the
first three elements that are identical to "Viraj."

- `stringdist(mystring, name) == 1` checks for strings with a distance of 1 from "Viraj,"


yielding `TRUE` for "Vikraj," "Viraji," and "Viroj."

- `stringdist(mystring, name) == 2` checks for strings with a distance of 2 from "Viraj,"


yielding `TRUE` for "Vroj"

14
6. Apply summary command to iris dataset of the ‘datasets’ package and
interprets the output.

summary(iris)

Output:

Interpretation :-

The `summary()` command applied to the Iris dataset from the 'datasets' package provides a
sum mary of the dataset's five numerical variables: Sepal.Length, Sepal.Width, Petal.Length,
Petal.Width, and the categorical variable Species.

For the numerical variables:


- It presents statistics such as minimum, 1st quartile (25th percentile), median (50th percentile),
mean, 3rd quartile (75th percentile), and maximum values.
- These statistics help understand the distribution and central tendency of each variable.

For the categorical variable Species:


- It shows that 50 is the count of each species category: setosa, versicolor, and virginica.
- This information provides an overview of the dataset's class distribution.

15
7. Use plot(iris) function and interpret the output. Write down your finding
about the dataset.

plot(iris)

Output:

16
Interpretation :-

Here are some findings from the scatterplots:

1. Sepal.Length vs. Sepal.Width:


- Setosa species typically has shorter sepal length and wider sepal width compared to
the other species.
- Versicolor and Virginica have a broader overlap in sepal length and width.

2. Sepal.Length vs. Petal.Length:


- Sepal length and petal length are positively correlated.
- Setosa species generally has shorter sepal and petal lengths.

3. Sepal.Length vs. Petal.Width:


- There is a positive correlation between sepal length and petal width.
- Setosa species has the smallest petal width.

4. Sepal.Width vs. Petal.Length:


- Sepal width and petal length show some variation, but the correlation is not as strong
as in other pairs.

5. Sepal.Width vs. Petal.Width:


- Sepal width and petal width have some variation, but there is no strong correlation.

6. Petal.Length vs. Petal.Width:


- Petal length and petal width are positively correlated.
- Setosa species has the smallest petals, while Virginica has the largest.

17
8. Install and load the MASS package and access the Boston dataset. Study
the dataset from the resources available on the internet and write what you
can find relevant to the dataset.

library("MASS")
Boston

Output:

Interpretation :-
The Boston dataset, available in the MASS package, contains housing-related information for
various suburbs in Boston. It is widely used in statistical modeling and machine learning. Key
variables in this dataset include factors influencing housing prices, such as crime rates, property
tax, proximity to employment centers, and more.

Relevant information about the dataset:


- It consists of 506 observations and 14 variables.
- Target variable: Median value of owner-occupied homes (MEDV).
- Factors like crime rate, room count, and accessibility to highways impact housing prices.
- The dataset is often used for regression tasks to predict housing prices.
- It serves as a benchmark in evaluating regression algorithms and housing market analysis.

Researchers and analysts use the Boston dataset to study relationships between these factors and
gain insights into real estate market dynamics.

18
9. Write a script file to compute the following of the numeric variable in
Boston dataset.
• Sum
• Range
• Mean
• Standard deviation.

colSums(Boston)
colMeans(Boston)
sapply(Boston, range)
sapply(Boston, sd)

Output:

19
10. Create a vector x of all those values from 1:100 that are divisible by 5
and do the following operations on the vector:
a. Find the length of vector x.
b. Print the values stored at the fifth, tenth, and fifteenth location
of vector x.
c. Find the sum, mean, range, median and standard deviation of
vector x.
d. Replace the fifth and tenth values with NA and NaN values,
respectively and find the mean of modified vector.
e. Check if x contains any NA values and print the indices of NA values
in vector x.
f. Remove NA values from vector x and use summary command on it.
g. Print the values of first and third quartile of vector x from the output
of the summary command.

vec = 5*1:20 #Creating vector from 1:100 that are divisible by 5


vec
length(vec) #Finding the length of vector
vec[c(5, 10, 15)] #Printing the values stored at 5, 10, 15 position
sum(vec) #Calculating the sum of the vector
mean(vec) #Calculating the mean of the vector
range(vec) #Calculating the range of the vector
median(vec) #Calculating the median of the vector
sd(vec) #Calculating the standard deviation of the vector
vec[c(5, 10)] = c(NA, NaN) #Replace the 5th and 10th values with NA and NaN values
mean(vec)
mean(vec, na.rm = TRUE) #Finding the mean of modified vector.
which(is.na(vec)) #Printing the indices of NA values
vec = vec[!is.na(vec)]
summary(vec)
summary(vec)[c("1st Qu.", "3rd Qu.")] #Printing the values of 1st and 3rd quartile

20
Output:

21
11. Assume the given vectors and do the following operations:
x = 1:12; y = 13:24; z = 1:6; a = 1:12; c = (5,10, 15, NA, 25, NaN); b = (13, 15,
17, 19, 20, 21, 23, 25, 27, 29, 31, 24);
v = (26, 21, 87, 56, 72, 60); k = (0, 2, 4, 6, 8, 16, 32)
(a). Find x × y and x × y × z and interpret the output.
(b). Do an element-wise comparison between x and a, and y and b.
(c). Find all the elements that are greater than 6 of vector x and store these
elements into another vector p.
(d). Check for NA and NaN values in vectors b and c.
(e). Check if overall vector x is equal to vector a and vector b .
(f). Why does identical(x, z) evaluate to FALSE?
(g). What is the difference between all() and all.equal() functions? Illustrate
with the help of an example.
(h). Run any(x, z) function and interpret the out.
(i). Create a new vector of the non-NA values of vector c using a single
line code.
(j). Sort vector v in descending order and output the original indices in
order of the sorted elements.
(k). Find log to the base 2 of vector k.

x = 1:12; y = 13:24; z = 1:6; a = 1:12; k = c(0, 2, 4, 6, 8, 16, 32); v = c(26, 21, 87, 56, 72, 60);
b = c(13, 15, 17, 19, 20, 21, 23, 25, 27, 29, 31, 24); c = c(5, 10, 15, NA, 25, NaN)
x*y #performs element-wise multiplication between the vectors x and y
x*y*z #multiplies the elements of all three vectors (x, y, and z) element-wise
x==a #element-wise comparison
y==b
p = x[x>6] #storing all the elements that are greater than 6 of vector x in p
p
is.na(b) #Checking for NA and NaN values
is.na(c)
all(x==a)
all(x==b)
identical(x, z) #Since x and z have different lengths and potentially different attributes,
identical(x, z) returns FALSE.
all(x, y) #`all()` checks if all elements of a logical vector are TRUE
all.equal(x, y) #`all.equal()` compares numeric values allowing tolerance for differences.
any(x, z) #The any(x, z) function checks if there's at least one TRUE value in the
logical vector resulting from comparing x and z element-wise.
f = c[!is.na(c)] #Creating a new vector of the non-NA values
f
order(v, decreasing = TRUE)
log2(k)

22
Output:

23
12. Assuming the character vector cv = c(“sunita”, “bimla”, “kavita”,
“geeta”, “anu”, “dikshita”, “susmita”, “seema”):
a. Find the character count in each name.
b. Find the geeta exist in vector cv

cv = c("sunita", "bimla", "kavita", "geeta", "anu", "dikshita", "susmita", "seema")


cv
nchar(cv) #Counting no. of character in cv
"geeta"%in%cv #Checking if geeta is present in cv

Output:

24
13.Output the indices of the names that contain substring ee in vector cv of
assignment 12.
for (i in 1:length(cv)){ if (grepl("ee", cv[i])==TRUE) {print(i)}}
#using grepl to check substring in a vector

Output:

14. Find out how many strings end with the letters ta in vector cv of in
assignment 12.

cnt=0 #initializing counter


for (i in 1:length(cv)){ if ((endsWith(cv[i], "ta"))==TRUE) {cnt=cnt+1}}
cnt #using endsWith built-in function

Output:

25
15. Create a vector to store the grades of 20 students for the first minor
exam. Grades are given at four levels (A, B, C, D). Compute the modal
grade. Further, store the grades of the same students for the second minor
exam. Compare the grades for the two exams. Count the number of
students who have got a higher grade in the second minor.

fmg <- sample(c("A", "B", "C", "D"), 20, replace = TRUE) #using sample function to create
a vector with random values
smg <- sample(c("A", "B", "C", "D"), 20, replace = TRUE)
mfmg <- names(sort(table(fmg), decreasing = TRUE))[1] #converting the value in table
class in sorting in decreasing order and catching the first value
cat("Modal Grade for First Minor Exam:", mfmg, "\n")
shgsm <- sum(smg > fmg) #adding the no. of students got a higher grade in the 2nd minor
cat("Number of Students with Higher Grade in Second Minor Exam:", shgsm, "\n")

Output:

26
16. Create a matrix m of five rows in a row-major order of numbers from 1
to 100 incremented by a step of 5 units:
(a) Find row and column-wise mean of matrix m.
(b) Find the minimum value for each row and column.
(c) Find the transpose and sort the values in each column in decreasing
order.
(d) Assign the row names as R1 to R5 and column names as C1 to C4.
(e) Display all the elements of the second and fourth column without using
indices.
(f) Display all the elements of the first and third row without using indices.
(g) Create a new matrix by deleting the second and fourth column of matrix
m using indices and column names.
(h) Replace elements at indices (2, 3), (2, 4), (3, 3), and (3, 4) with NA values.
(i) Replace element at index (1, 3) with NaN.
(j) Check if matrix m contains any NA or NaN values and interpret the
output.
(k) Create two new matrices rm and cm by concatenating matrix m row-wise
and column-wise with itself.
m = matrix(data = seq(1, 100, 5), byrow = T, nrow = 5)
rowMeans(m) #Row wise mean
colMeans(m) #Coloumn wise mean
apply(m, 1,min) #min value for each row
apply(m, 2,min) #min value for each col
t(m) #tranpose of the matrix
apply(t(m), 2, function(a) sort(a, decreasing = T)) #sorting values in each col in dec order
row.names(m) = c("R1", "R2", "R3", "R4", "R5") #Assigning row names
colnames(m) = c("C1", "C2", "C3", "C4") #Assigning col names
m[ , c("C2", "C4")] #Printing elements of 2nd & 4rt col
m[c("R1", "R3"), ] #Printing elements of 1st & 3rd row
m2 = m[, c("C1", "C3")]
m2
m[c(2, 3), c(3, 4)] = NA #Replacing values with na
m[1, 3] = NaN #Replacing the value with nan
m
is.na(m)
is.nan(m)
rm = do.call(cbind, replicate(2, m, simplify = F)) #row repeat
rm
cm = do.call(rbind, replicate(2, m, simplify = F)) #col repeat
cm

27
Output:

28
29
17.Interpret the output of the following commands:
a. n = matrix(rep(m, 2), nrow= ncol(m), byrow= FALSE)
b. n = matrix(rep(m, 2), nrow= nrow(m), byrow= FALSE)
c. n = matrix(rep(m, 2), nrow= ncol(m), byrow = TRUE)
d. m1 = do.call(rbind, replicate(2, m, simplify = FALSE))
e. m2 = do.call(cbind, replicate (2, m, simplify = FALSE))
f. Rename row and column names as per the requirements of matrix
m1 and m2.

n = matrix(rep(m, 2), nrow= ncol(m), byrow= FALSE)


n
n = matrix(rep(m, 2), nrow= nrow(m), byrow= FALSE)
n
n = matrix(rep(m, 2), nrow= ncol(m), byrow = TRUE)
n
m1 = do.call(rbind, replicate(2, m, simplify = FALSE))
m2 = do.call(cbind, replicate (2, m, simplify = FALSE))
row.names(m1) = c("R1", "R2", "R3", "R4", "R5","R6", "R7", "R8", "R9", "R10")
#Assigning row names
colnames(m1) = c("C1", "C2", "C3", "C4") #Assigning col names
m1
row.names(m2) = c("R1", "R2", "R3", "R4", "R5")
colnames(m2) = c("C1", "C2", "C3", "C4","C5","C6","C7", "C8")
m2

Interpretation :-

Here is the interpretation of the output :-


• n is a column major matrix created by repeating elements of m twice in column-wise order,
resulting in a matrix with no. of rows same as no. of cols in m.
• n is a column major matrix created by repeating elements of m twice in column-wise order,
resulting in a matrix with no. of rows same as no. of rows in m.
• n is a row major matrix created by repeating elements of m twice in column-wise order,
resulting in a matrix with no. of rows same as no. of cols in m.
• m1 replicates m vertically, stacking two copies of m on top of each other using rbind.
• m2 replicates m horizontally, creating a matrix with the same number of rows as m but
twice as many columns using cbind.

30
Output:

31
18. Create a 4 x 3 matrix A of normally distributed random numbers with
mean 100 and standard deviation 10. Create another 3 x 4 matrix B with
normally distributed random numbers with mean 10 and standard
deviation 1. Perform matrix multiplication of the two matrices and store
the result in a third matrix C rounded up to two decimal places.

#Using runif function to generate normally distributed random numbers


A = matrix((runif(12,1,100)), nrow = 4)
A
B = matrix((runif(12,1,10)), nrow = 3)
B
C = round(A%*%B, 2) #Rounding up to 2 decimal place
C

Output:

32
19. Create a 4 x 3 matrix A of uniformly distributed random integer
numbers between 1 to 100. Create another 3 x 4 matrix B with uniformly
distributed random integer numbers between 1 to 10. Perform matrix
multiplication of the two matrices and store the result in a third matrix C.

#Using runif function to generate normally distributed random numbers & as.integer to convert it
into integer
A = matrix(as.integer(runif(12,1,100)), nrow = 4)
A
B = matrix(as.integer(runif(12,1,10)), nrow = 3)
B
C = A%*%B #Multipling the matrices
C

Output:

33
20. Replicate the resulting matrix C obtained in Exercise 19 twice verti-
cally.

do.call(rbind, replicate(2, C, simplify = F))

Output:

34
21. Create A and B, two 4 x 3 matrices of normally distributed random
numbers, with mean 0 and standard deviation 1. Find the indices of all
those numbers in matrix A which are less than the respective numbers in
matrix B and print these numbers.

A = matrix(rnorm(12),4,3) #using rnorm function to create normally distributed matrix


A
B = matrix(rnorm(12),4,3)
B
which(A>B) #which function is used to get index values

Output:

35

You might also like