R Programming Questions and Answers – Subsetting – 1

This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Subsetting – 1”.

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

Answer: b
Explanation: 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
View Answer

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

advertisement
advertisement

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

Answer: b
Explanation: 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?

advertisement
> 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”
View Answer

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

advertisement

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
View Answer

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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?

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

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

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: By default, when a single element of a matrix is retrieved, it is returned as a vector of length 1 rather than a $1\times 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
View Answer

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

Sanfoundry Global Education & Learning Series – R Programming Language.

Here’s the list of Best Books in R Programming Language.

To practice all areas of R Programming Language, Here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.