250+ TOP MCQs on Subsetting and Answers

R Programming Language Multiple Choice Questions on “Subsetting”.

1. Which of the following extracts first element from the following R vector?

 > x <- c("a", "b", "c", "c", "d", "a")

a) x[10]
b) x[1]
c) x[0]
d) x[2]

Answer: b
Clarification: The element which we want to extract will be in the format of variable[index value of the element] in R script.

2. Point out the correct statement?
a) There are three operators that can be used to extract subsets of R objects
b) The [ operator is used to extract elements of a list or data frame by literal name
c) The [[ operator is used to extract elements of a list or data frame by string name
d) There are five operators that can be used to extract subsets of R objects

Answer: a
Clarification: Three operators are [,[[ and $.

3. Which of the following extracts first four element from the following R vector?

 > x <- c("a", "b", "c", "c", "d", "a")

a) x[0:4]
b) x[1:4]
c) x[0:3]
d) x[4:3]

Answer: b
Clarification: The multiple successive elements which we want to extract will be in the format of variable[index value of the start element:index value of the last element] in R script.

4. What will be the output of the following R code?

> x <- c("a", "b", "c", "c", "d", "a")
> x[c(1, 3, 4)]

a) “a” “b” “c”
b) “a” “c” “c”
c) “a” “c” “b”
d) “b” “c” “b”

Answer: b
Clarification: The sequence does not have to be in order; you can specify any arbitrary integer vector.

5. Point out the wrong statement?
a) $ operator semantics are similar to that of [[
b) The [ operator always returns an object of the same class as the original
c) The $ operator is used to extract elements of a list or a data frame
d) There are three operators that can be used to extract subsets of R objects

Answer: c
Clarification: The [[ operator is used to extract elements of a list or a data frame. It can only be used to extract a single element and the class of the returned object will not necessarily be a list or data frame.

6. What will be the output of the following R code?

> x <- matrix(1:6, 2, 3)
> x[1, 2]

a) 3
b) 2
c) 1
d) 0

Answer: a
Clarification: Matrices can be subsetted in the usual way with (i,j) type indices where i is the row and j is the column numbers.

7. What will be the output of the following R code?

> x <- matrix(1:6, 2, 3)
> x[1, ]

a) 1 3 5
b) 2 3 5
c) 3 3 5
d) file

Answer: a
Clarification: Matrices can be subsetted in the usual way with (i,j) type indices where i is the row and j is the column numbers. If only row or only column number is specified, then the respective full row or column is printed.

8. Which of the following R code extracts the second column for the following matrix?

a) x[2, ]
b) x[1, 2]
c) x[, 2]
d) x[1 1 2]

Answer: c
Clarification: This behavior is used to access entire rows or columns of a matrix.

9. What will be the output of the following R code?

> x <- matrix(1:6, 2, 3)
> x[1, , drop = FALSE]

a)

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

b)

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

c)

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

d) Error

Answer: a
Clarification: By default, when a single element of a matrix is retrieved, it is returned as a vector of length 1 rather than a $1times 1$ matrix.

10. What will be the output of the following R code?

> x <- list(foo = 1:4, bar = 0.6)
> x

a)

$foo
[1] 1 2 3 4
$bar
[1] 0.6

b)

$foo
[1] 0 1 2 3 4
$bar
[1] 0 0.6

c)

$foo
[1] 0 1 2 3 4
$bar
[1] 0.6

d) Error

Answer: a
Clarification: The [[ operator can be used to extract single elements from a list.

Leave a Reply

Your email address will not be published. Required fields are marked *