R Programming Questions and Answers Part-18

1. Point out the wrong statement?
a) Very less operations in R are vectorized
b) Vectorization allows you to write code that is efficient, concise, and easier to read than in non-vectorized languages
c) vectorized means that operations occur in parallel in certain R objects
d) Matrix operations are also vectorized

Answer: a
Explanation: Many operations in R are vectorized.

2. What will be the output of the following R code?
> x < - 1:4
> y < - 6:9
> z < - x + y
> z
a) 7 9 11 13
b) 7 9 11 13 14
c) 9 7 11 13
d) NULL

Answer: a
Explanation: This is simplest example of adding two vectors together.

3. What will be the output of the following R code?
> x < - 1:4
> x > 2
a) 1 2 3 4
b) FALSE FALSE TRUE TRUE
c) 1 2 3 4 5
d) 5 4 3 1 2 1

Answer: b
Explanation: Another operation you can do in a vectorized manner is logical comparisons.

4. Point out the wrong statement?
a) Dates are represented by the Date class
b) Times are represented by the POSIXct or the POSIXlt class
c) Dates are represented by the DateTime class
d) Times can be coerced from a character string

Answer: c
Explanation: Dates are stored internally as the number of days since 1970-01-01 while times are stored internally as the number of seconds since 1970-01-01.

5. Which of the following code represents internal representation of a Date object?
a) class(as.Date(“1970-01-02”))
b) unclass(as.Date(“1970-01-02”))
c) unclassint(as.Date(“1970-01-02”))
d) classint(as.Date(“1970-02-02”))

Answer: b
Explanation: You can see the internal representation of a Date object by using the unclass() function.

6. What will be the output of the following R code?
> x < - as.Date("1970-01-01")
> x
a) “1970-01-01”
b) “1970-01-02”
c) “1970-02-01”
d) “1970-02-02”

Answer: a
Explanation: Dates are represented by the Date class and can be coerced from a character string using the as.Date() function.

7. What will be the output of the following R code?
> x < - Sys.time()
> class(x)
a) “POSIXct” “POSIXt”
b) “POSIXXt” “POSIXt”
c) “POSIXct” “POSIct”
d) “POSIXt” “POSIXt”

Answer: a
Explanation: Times can be coerced from a character string using the as.POSIXlt or as.POSIXct function.

8. Which of the following return a subset of the columns of a data frame?
a) select
b) retrieve
c) get
d) set

Answer: a
Explanation: One important contribution of the dplyr package is that it provides a “grammar” for data manipulation and for operating on data frames.

9. Point out the correct statement?
a) The data frame is a key data structure in statistics and in R
b) R has an internal implementation of data frames that is likely the one you will use most often
c) There are packages on CRAN that implement data frames via things like relational databases that allow you to operate on very very large data frames
d) All of the mentioned

Answer: d
Explanation: The basic structure of a data frame is that there is one observation per row and each column represents a variable, a measure, feature, or characteristic of that observation.

10. _________ extract a subset of rows from a dataframe based on logical conditions.
a) rename
b) filter
c) set
d) subset

Answer: a
Explanation: rename is used to rename variables in a dataframe.