Assignment2 L6-L10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

MOOC Course - Foundations of R Software

July 2023

Assignment 2
1. Which one of the following is the correct specification to compute 𝑦 =
√𝑒𝑥𝑝( 𝑥) + 𝑥 2 + 𝑥 3/2 − 4000 and what is its value for x = 17?

a. y<-function{x}{sqrt(exp(x)+x^2)+x^3/2-4000}, y=3371.298
b. y<-f(x)[sqrt(exp(x)+x^2)+x^(3/2)-4000] , y=984.481
c. y<-function(x){sqrt(exp(x)+x^2)+x^3/2-4000}, y=3371.298
d. y<- function(x){sqrt(exp(x)+x^2)+x^(3/2)-4000}, y
=984.481

Solution: The command in R to create a function is y <-


function(x){formulation}

2. Which one of the following is the correct outcome of z(14,11) of the function spe
cified as z=function(x,y){sqrt(x^3-y^3+log(x))+log(-(5*x^-2+6*y^-
2-60))-(x^3-y^2)^(3/4)} ?
a. -4511644550
b. -324.8028
c. -2758.699
d. None of these

Solution: The command in R to evaluate a function is z(xval,yval)

3. Which one of the following is the correct command to obtain the following matrix?

21 24 27
𝑥 = (22 25 28)
23 26 29

a. x=matrix(21:29,3,3,byrow=T)
b. x=mat(21:29,3,3, byrow=T)
c. x=matrix(21:29,3,3,byrow=F)

1
d. x=mat(21:29,3,3,byrow=F)

Solution: The command in R to create a matrix is x=matrix(data, nrow, ncol,


byrow=)

4. Which one of the following is the correct command to obtain the following matrix?
15 11 17
𝑧=( )
8 19 12

a. z <- matrix(nrow=2, ncol=3, data=c(15,11,17,8,19,12),


byrow=T)
b. z <- matrix(nrow=3, ncol=2, data=c(15,8,11,19,17,12) ,
byrow=T)
c. z <- matrix(nrow=2, ncol=3, data=(15,8,11,19,17,12) ,
byrow=T)
d. z <- matrix(nrow=3, ncol=2, data=(15,8,11,19,17,12) ,
byrow=T)

Solution: The command in R to create a matrix is x=matrix(data, nrow, ncol,


byrow=)

5. Which one of the following is the correct command to obtain the third column and
first row of the following matrix?

10 20 30
𝑥 = (40 50 60)
70 80 90

a. x(3, ) and x( ,1) respectively.


b. x(3, ) and x(1, ) respectively.
c. x[, 3] and x[1, ] respectively.
d. x[ ,1] and x[3, ] respectively.

Solution: The command in R to get a row and a column of a matrix x is x[nrow, ]


and x[ , ncol] respectively.

2
6. Which one of the following is the correct outcome of the command X[3,2] for
the matrix constituted by the commands X<-matrix(61:69,3,3,byrow=F)?

a. 63
b. 66
c. 67
d. 68

Solution: The command x[nrow, ncol] provides the nrow-th row and ncol-th
column element in the matrix x.

7. Which one of the following is the correct outcome of the commands dim(x) and
dim(y) for the matrices obtained by x<-matrix(101:150,25,2,byrow=T)
and y<-matrix(201:250,25,2,byrow=F) ?

a. 25 2 and 2 25 respectively.
b. 2 25 and 25 2 respectively.
c. 2 25 and 2 25 respectively.
d. 25 2 and 25 2 respectively.

Solution: The command dim(x)provides the dimensions of the matrix x.

8. Which one of the following is the correct outcome of the command

x <- diag(5, nrow=2, ncol=2) ?

5 5
a. 𝑥 = ( )
0 5

0 5
b. 𝑥 = ( )
5 0

5 5
c. 𝑥=( )
5 5

3
5 0
d. 𝑥=( )
0 5

Solution: The command diag() creates a diagonal matrix in R.

9. Which one of the following is the correct outcome of the command t(x) for

x<-matrix(nrow=3, ncol=2, data=30:35, byrow=T) ?

a.
[,1] [,2] [,3]
[1,] 30 32 34
[2,] 31 33 35

b.
[,1] [,2]
[1,] 30 31
[2,] 32 33
[3,] 34 35

c.
[,1] [,2]
[1,] 30 33
[2,] 31 34
[3,] 32 35

d.
[,1] [,2] [,3]
[1,] 30 31 32
[2,] 33 34 35

Solution: The command t(x) creates a matrix transpose of x in R.

4
10. Which one of the following is the correct command to obtain the multiplication of
two matrices x and y of the same order?

a. x**y

b. *%*y

c. x%*%y

d. x%**%y

Solution: The command x%*%y provides the matrix multiplication of matrices x and y
in R.

11. Which one of the following is the correct command to obtain the multiplication of
4 7 12 13
two matrices 𝑥=( ) and 𝑦 = ( ) along with its correct answer?
8 2 16 11

a. x*y
and its correct answer is
[,1] [,2]
[1,] 48 128
[2,] 91 22

b. x*%*y
and its correct answer is
[,1] [,2]
[1,] 48 91
[2,] 128 22

c. x%*%y
and its correct answer is
[,1] [,2]
[1,] 160 129
[2,] 128 126

5
d. x%%*%%y
and its correct answer is
[,1] [,2]
[1,] 160 128
[2,] 129 126

Solution: The command x%*%y provides the matrix multiplication of matrices x and y
in R.

12. Let x<- matrix(nrow=3, ncol=3, data=49:41, byrow=T) then which


one of the following is the correct outcome of 3*x ?

a.
[,1] [,2] [,3]
[1,] 147 144 141
[2,] 138 135 132
[3,] 129 126 123
b.
[,1] [,2] [,3]
[1,] 147 138 129
[2,] 144 135 126
[3,] 141 132 123

c.
[,1] [,2] [,3]
[1,] 141 132 123
[2,] 144 135 126
[3,] 147 138 129

d.
[,1] [,2] [,3]

6
[1,] 129 126 123
[2,] 138 135 132
[3,] 147 144 141

Solution: The command a*x provides the scalar multiplication of matrix x with scalar
a in R.

13. Which one of the following is the correct outcome of the addition of two matrices
4 8 12 16
𝑥=( ) and 𝑦 = ( ) along with its correct answer?
7 2 13 11

a.
x + y
and its correct answer is
[,1] [,2]
[1,] 16 24
[2,] 20 13

b.
x %+% y
and its correct answer is
[,1] [,2]
[1,] 16 24
[2,] 20 13

c.
x %%+%% y
and its correct answer is
[,1] [,2]
[1,] 20 13
[2,] 16 24

d.
x %+%+ y
7
and its correct answer is
[,1] [,2]
[1,] 20 13
[2,] 16 24

Solution: The command x+y provides the matrix addition of matrices x and y in R.

14. Let x<- matrix(nrow=4, ncol=4, data=16:31, byrow=F) then which


one of the following is the correct outcome of 2+4%*%x ?

a.
[,1] [,2] [,3] [,4]
[1,] 66 80 96 112
[2,] 68 84 100 116
[3,] 72 88 104 120
[4,] 76 92 108 124

b.
[,1] [,2] [,3] [,4]
[1,] 66 82 98 114
[2,] 70 86 102 118
[3,] 74 90 106 122
[4,] 78 94 110 126

c. Error...

d. None of these

Solution: The command x%*%y provides the matrix multiplication of matrices x and y
in R. A scalar and matrix are not compatible in this multiplication.

8
15. Which one of the following is the correct outcome of X[3 ,] for the matrix
specified by

X<-matrix(nrow=3, ncol=3, data=c(10,20,30,40,50,60,70,80,90),


byrow=F) ?

a. [1] 30 60 90
b. [1] 35 75 15
c. [1] 90 60 30
d. None of these

Solution: The command x[nrow, ] provides the nrow-th row of the matrix x in R.

16. Which one of the following is the correct outcome of x[1:2,2:3] for the matrix
specified by x<- matrix(nrow=4, ncol=4, data=16:31, byrow=T)?

a.
[,1] [,2]
[1,] 21 22
[2,] 17 18

b.

[,1] [,2]

[1,] 17 18

[2,] 21 22

c.
[,1] [,2]
[1,] 17 21
[2,] 18 22

d.
[,1] [,2]

9
[1,] 17 18
[2,] 22 21

Solution: The command x[a:b, c:d] provides the submatrix of x with a to b


rows and c to d columns in R.

20 30
17. Which one of the following is the correct command to get the matrix (50 60)
80 90
from the matrix specified by

X<-matrix(nrow=4, ncol=3, data=c(10,20,30,40,50,60,70,80,90,


100,110,120), byrow=T) ?

a. X[2:3, 1:3]
b. X[1:3, 2:3]
c. X[1:1, 2:2, 3:3]
d. X[3:3, 2:2, 1:1]

Solution: The command x[a:b, c:d] provides the submatrix of x with a to b


rows and c to d columns in R.

10
13 1 12 5
4 20 14 6
18. If 𝑥 = ( ) then which one of the following is the correct
5 3 16 7
16 8 12 8

command and its outcome for obtaining the inverse of the submatrix of the matrix x
with first three rows and last three columns?

a.
solve(X[1:3,2:4])=
[,1] [,2] [,3]
[1,] -0.02777778 0.05555556 -0.02777778
[2,] 1.69444444 0.11111111 -1.30555556
[3,] -3.86111111 -0.27777778 3.13888889

b.
inv(X[1:3,2:4])=
[,1] [,2] [,3]
[1,] -0.02777778 0.05555556 -0.02777778
[2,] 1.69444444 0.11111111 -1.30555556
[3,] -3.86111111 -0.27777778 3.13888889

c.
inv(X[1:3,2:4])=
[,1] [,2] [,3]
[1,] -0.02777778 1.6944444 -3.8611111
[2,] 0.05555556 0.1111111 -0.2777778
[3,] -0.02777778 -1.3055556 3.1388889

d.
solve((X[1:3,2:4]))=

11
[,1] [,2] [,3]
[1,] -0.02777778 1.6944444 -3.8611111
[2,] 0.05555556 0.1111111 -0.2777778
[3,] -0.02777778 -1.3055556 3.1388889

Solution: The command x[a:b, c:d] provides the submatrix of x with a to b


rows and c to d columns in R. And the command solve(x) provides inverse of
x.

19. Suppose x is any vector as x=c(10, 80, 90, 101:200, NA) then
which one of the following is the correct outcome of the command mean(x)?
a. 147.8641

b. 96.2

c. Error...

d. NA

Solution: The command mean(x)returns NA value if at least one of the entries in x


is NA.

20. Which one of the following is the correct outcome of the command

(x < 15) || (x > 12) & (x < 15) && (x > 12)||(x == 17) when
x = 13 and when x = -13 ?

a. FALSE and FALSE respectively.

b. TRUE and TRUE respectively.

c. FALSE and TRUE respectively.

d. TRUE and FALSE respectively.

12
Solution: Using the Boolean algebra, (x < 15) || (x > 12) and (x < 15)
returns TRUE in both cases when x=13, and -13. (x > 12)||(x == 17) returns
TRUE when x=13, and FALSE when x=-13. Because of the OR argument after (x <
15) it returns TRUE no matter what other arguments are.

21. Suppose x = 13:16 then which one of the following is the correct outcome of
the command

(x > 13)&(x < 15) ?

a. FALSE TRUE FALSE FALS

b. TRUE FALSE TRUE TRUE

c. TRUE TRUE TRUE FALSE

d. FALSE FALSE FALSE TRUE

Solution: It follows using the Boolean algebra.

22. Suppose x = 3:18 then which one of the following correctly specifies the
outcome of the following statement: x[(x > 14) & (x < 17)] and x[(x < 5)
| (x < 8)] ?

a. are different for both as 13 14 15 16 and 5 6 7 8 respectively.

b. are different for both as 15 16 and 3 4 5 6 7 respectively.

c. are different for both as 5 6 7 8 and 13 14 15 16 respectively.

d. are the same for both as 5 6 7 8

Solution: It follows using basic inequality that first expression gives numbers
between 14 and 17. The second expression gives less than 8.

13
23. Suppose x = 10:50 then which one of the following is the correct command to
know that which of the values in x are more than 30 and less than 40?

a. x[(x > 30) & (x < 40)]

b. (x > 40) & (x < 50)

c. x[(x >= 40) & (x >= 40)]

d. x[(x <= 40) & (x <= 40)]

Solution: It follows using basic inequality.

24. Suppose x = 45 then which one of the following is the correct outcome

of (x > 80) & (x < 70) and (x > 80) | (x < 70) ?

a. TRUE and FALSE respectively.

b. FALSE and FALSE respectively.

c. TRUE and TRUE respectively.

d. FALSE and TRUE respectively.

Solution: It follows using the Boolean algebra that first expression results in FALSE
& TRUE. Second expression results in FALSE | TRUE

25. Suppose x = 13:17 then which one of the following is the correct outcome of

x[(x > 12) | (x < 15)] ?

a. TRUE

b. FALSE

c. 13 14 15 16 17

d. 17 16 15 14 13

14
Solution: It follows using the fact that elements in x that are either greater than 12 or
less than 15 contains every element of x.

MOOC Course - Introduction to R Software

Answers of Assignment 2

1. d

2. b
3. c

4. a

5. c

6. b

7. d

8. d

9. a

10. c

11. c

12. a

13. a

14. c

15. a

16. b

17. b

18. a

19. d

20. b

21. a

15
22. b

23. a

24. d

25. c

16

You might also like