Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
56 views6 pages

MOOC Course - Introduction To R Software July 2017 Solution To Assignment 2

This document provides solutions to 25 multiple choice questions from an assignment for an Introduction to R Software MOOC course in July 2017. Each question includes the question prompt, the correct answer choice, and a brief explanation of the solution using R syntax and functions such as defining functions, matrices, matrix operations, logical operators, and extracting elements from matrices.

Uploaded by

xlntyogesh
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)
56 views6 pages

MOOC Course - Introduction To R Software July 2017 Solution To Assignment 2

This document provides solutions to 25 multiple choice questions from an assignment for an Introduction to R Software MOOC course in July 2017. Each question includes the question prompt, the correct answer choice, and a brief explanation of the solution using R syntax and functions such as defining functions, matrices, matrix operations, logical operators, and extracting elements from matrices.

Uploaded by

xlntyogesh
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/ 6

MOOC Course - Introduction to R Software

July 2017

Solution to Assignment 2

1. To define a function, we use the syntax function(vector of inputs)


{expression}.

Hence option d is correct.

2. We first need to type the following function on R console:

z=function(x,y)
{
sqrt(x^2+y^2)+exp(-(x^-2+y^-2))-(x^2+y^2)^(2/4)
}
and then type z(12,14) over the R console which assigns x = 12 and y = 14 to
the function. This gives an outcome 0.9880258.

Hence option b is correct.

3. The syntax for defining a matrix is matrix(vector, number of rows,


number of columns) and the extra command in the argument "byrow=F" (or
"byrow=T") enter the values in column (or row) first. Therefore
x=matrix(1:9,3,3,byrow=F) gives the required matrix.

Hence option c is correct.

1
4. The syntax for defining a matrix is matrix(number of rows, number of
columns, data=vector). We have to arrange the data c(5,6,7,8,9,10) in
rows first. So an extra option "byrow=T" inside the argument of matrix() function
is needed to enter the data in rows first. The default arrangement in matrix() is to
enter the data in columns first.

Hence option a is correct.

5. Command for extracting rth row (or rth column) of a vector x is x[r,] (or
x[,r]).

Hence option c is correct.

6. The given data is a sequence 1,2,3,4,5,6,7,8,9. The option byrow=F will not
arrange the data by row as the given option to do this has been set to False (F).
Thus the syntax x<-matrix(1:9,3,3,byrow=F) returns a matrix

and x[3,2] extracts its element having the position of third row and second
column.

Hence option b is correct.

7. Each of the matrices x and y have the number of rows and the number of
columns as 50 and 2, respectively. Hence the output for the command dim(x) and
dim(y) to get the dimension of x and y, respectively, is as shown in option d.

Hence option d is correct.

2
8. The syntax diag(3, nrow=2, ncol=2) is a command to get a diagonal matrix
with each diagonal entry as 3 and having 2 rows and 2 columns.

Hence option d is correct.

9. The command t(x) is used to find a transpose of a matrix x. Assigning the


given matrix to x and executing the command t(x) gives the required result

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


[1,] 6 8 10
[2,] 7 9 11

Hence option a is correct.

10. For matrix multiplication between two matrices, the symbol %*% is used.

Hence option c is correct.

11. Options a, b and d do not provide correct command for the multiplication of two
matrices. The correct command to multiply is x%*%y.

Hence option c is correct.

12. The command 2*x is used to multiply each element of the matrix x by 2. the
given command gives

[,1] [,2]

[1,] 1 2

[2,] 3 4

as the output.

Hence option a is correct.

3
13. To add two matrices, the + sign is used between two matrices. So other options
are not correct except option a.

Hence option a is correct.

14. For multiplying a constant to a matrix, only * sign is used between the constant
and the matrix. Thus 2%*%x is the wrong command and it returns error.

Hence option c is correct.

15. The command X<-matrix(nrow=3, ncol=3, data = c(10,20,30,40,


50,60,70,80,90), byrow=F) returns the matrix

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


[1,] 10 40 70
[2,] 20 50 80
[3,] 30 60 90
when executed over the R console and X[ ,2] provides the second column of the
matrix.

Hence option a is correct.

16. The command X[2:3,2:3] extracts the sub matrix with row number from 2 to
3 and column number from 2 to 3 of the initial matrix X. The command

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


80,90), byrow=F) returns the matrix

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


[1,] 10 40 70
[2,] 20 50 80
[3,] 30 60 90
Hence option b is correct.

4
17. The command X<-matrix(nrow=3, ncol=3, data=c(10,20,30,40,50,
60,70,80,90), byrow=F) returns the matrix

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


[1,] 10 40 70
[2,] 20 50 80
[3,] 30 60 90
 40 70 
as an output and the sub matrix   which is basically the common part
 50 80 
obtained from its first and second rows; and second and third columns. Hence
X[1:2,2:3] provides the required sub matrix.

Hence option b is correct.

18. The function solve() is used to obtain the inverse of a matrix. So assigning
the given matrix as x and executing the command solve(x) over the R console
gives the inverse of x.

Hence option a is correct option.

19. The vector x includes one of its entry as NA and result of any calculation which
includes NA always gives NA as an output. Therefore mean(x) is also returns NA.

Hence option d is correct.

20. First assign the value x = 3. The statement in question is based on the logical
operators and truth table. It is executed as

(3 < 5)&& (3 >2) & (3 < 5)|| (3 > 2)||(3 == 7)

which results into logical TRUE. Similarly assigning the value x = -3 and executing
it gives the result logical FALSE.

Hence option b is correct.

5
21. The logical operator & works as AND operator between two conditions and it
checks every element of the vector. Consider x=3, then the condition (x > 3) is
false and the condition (x < 5) is true. Hence (x > 3)&(x < 5) becomes false
for this value of x. In the similar way, it checks for other values of x also.

Hence option a is correct.

22. First assign the values x = 33:53 then execute the command x[(x > 28) &
(x < 59)]. It counts which of the numbers in x = 33:53 satisfy the condition x
> 28) || (x < 59)? For example, if x = 33 then (33 > 28) || (33 < 59)
is executed. The answer is TRUE. So answer x = 33 is added in the answer and
all other numbers are similarly verified. Similarly the condition x > 28) || (x <
59) is checked.

Hence option b is correct.

23. The command x[(x > 20) & (x < 50)] shows only those elements of x
which are more than 20 and less than 50. The explanation is the same as in question
22.

Hence option a is correct.

24. Assign x = 53:97 then the conditions (x > 92) && (x < 85) and (x >
92) || (x < 85) are checked. This results in FALSE and TRUE respectively.

Hence option d is correct.

25. After assigning x = 3:7, the conditions (x > 2) || (x < 5) is checked


and finally, x[(x > 2) || (x < 5)] returns those values of x which satisfies the
condition. This results 3 4 5 6 7 as the output because all these values satisfy the
condition.

Hence option c is correct.

You might also like