R Programming Questions and Answers – Subsetting – 2

This set of R Programming Questions and Answers for experienced focuses on “Subsetting – 2”.

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

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

a) x[[1]]
b) x[1]
c) x[[0]]
d) x[0]
View Answer

Answer: a
Explanation: The [[ operator can also use named indices so that you don’t have to remember the exact ordering of every element of the list.

2. Point out the correct statement?
a) You can also use the $ operator to extract elements by name
b) $ operator can be used with computed indices
c) The [[ operator can only be used with literal names
d) $ operator semantics are similar to that of [[
View Answer

Answer: a
Explanation: You don’t need the quotes when you use the $ operator.

advertisement
advertisement

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

> x <- list(foo = 1:4, bar = 0.6, baz = "hello")
> name <- "foo"
> x$name

a) 1
b) 2
c) 3
d) NULL
View Answer

Answer: d
Explanation: Element “name” doesn’t exist.

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

advertisement
> x <- list(foo = 1:4, bar = 0.6, baz = "hello")
> name <- "foo"
> x[[name]]

a) 1 2 3 4
b) 0 1 2 3
c) 1 2 3 4 5
d) All of the mentioned
View Answer

Answer: a
Explanation: One thing that differentiates the [[ operator from the $ is that the [[ operator can be used with computed indices.

advertisement

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

> x <- list(a = list(10, 12, 14), b = c(3.14, 2.81))
> x[[c(1, 3)]]

a) 13
b) 14
c) 15
d) 18
View Answer

Answer: b
Explanation: The [[ operator can take an integer sequence if you want to extract a nested element of a list.

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

> x <- list(aardvark = 1:5)
> x$a

a) 1 2 3 4 5
b) 2 3 5
c) 1 3 3 5
d) 1 2 3
View Answer

Answer: a
Explanation: Partial matching of names is allowed with [[ and $.

7. Which of the following code extracts 1st element of the 2nd element?

 > x <- list(a = list(10, 12, 14), b = c(3.14, 2.81))

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

Answer: a
Explanation: [ operator always returns an object of the same class as the original.

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

> x <- list(aardvark = 1:5)
> x[["a", exact = FALSE]]

a) 1 2 3 4 5
b) 2 3 5
c) 1 3 3 5
d) 1 2 3
View Answer

Answer: a
Explanation: This is often very useful during interactive work if the object you’re working with has very long element names.

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

> x <- c(1, 2, NA, 4, NA, 5)
> bad <- is.na(x)
> print(bad)

a) FALSE FALSE FALSE FALSE TRUE FALSE
b) FALSE TRUE TRUE FALSE TRUE FALSE
c) FALSE FALSE TRUE FALSE TRUE FALSE
d) FALSE TRUE TRUE TRUE TRUE FALSE
View Answer

Answer: c
Explanation: A common task in data analysis is removing missing values (NAs).

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 for Experienced , Here is complete set of 1000+ Multiple Choice Questions and Answers.

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.