R Programming Questions and Answers – Vectorized Operations – 2

This set of R Programming Interview Questions and Answers for experienced focuses on “Vectorized Operations – 2”

1. Which of the following function gives the day of the week?
a) weekdays
b) months
c) quarters
d) semesters
View Answer

Answer: a
Explanation: Weekdays will give the day of the week. months function give the month name. Quarters divide the year into fourths. Semesters divide the year into halfs.

2. Point out the correct statement?
a) Times use the POSIXct and POSIXlt class
b) Dates and times have special classes in R that allow for numerical and statistical calculations
c) Character strings can be coerced to Date/Time classes using the strptime function
d) All of the mentioned
View Answer

Answer: d
Explanation: Character strings can be coerced to Date/Time classes using the as.Date, as.POSIXlt, or as.POSIXct.

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

> p <- as.POSIXlt(x)
> names(unclass(p))
> p$wday

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

Answer: a
Explanation: The POSIXlt object contains some useful metadata.

advertisement
advertisement

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

> datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
> x <- strptime(datestring, "%B %d, %Y %H:%M")
> x

a) “2012-01-10 10:40:00 EST” “2011-12-09 09:10:00 EST”
b) “2012-01-10 10:40:00 IST” “2011-12-09 09:10:00 IST”
c) “2012-01-10 10:40:00 GMT” “2011-12-09 09:10:00 GMT”
d) Error
View Answer

Answer: a
Explanation: strptime() takes a character vector that has dates and times and converts them into to a POSIXlt object.

advertisement

5. Point out the wrong statement?
a) POSIXct is just a very large integer under the hood
b) POSIXlt stores a bunch of other useful information like the day of the week, day of the year, month, day of the month
c) There are a number of generic functions that work on dates and times to help you extract pieces of dates and/or times
d) None of the mentioned
View Answer

Answer: d
Explanation: POSIXct uses a useful class when you want to store times in something like a data frame.

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

advertisement
> x <- as.Date("2012-01-01")
> y <- strptime("9 Jan 2011 11:34:21", "%d %b %Y %H:%M:%S")
> x-y

a) Time difference of 356.3095 days
b) Warning
c) NULL
d) Error
View Answer

Answer: b
Explanation: You can use mathematical operations on dates and times.

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

> x <- as.Date("2012-03-01")
> y <- as.Date("2012-02-28")
> x-y

a) Time difference of 3 days
b) Time difference of 2 days
c) Time difference of 1 days
d) Time difference of 5 days
View Answer

Answer: b
Explanation: Matrix operations are also vectorized, making for nicely compact notation.

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

> x <- as.POSIXct("2012-10-25 01:00:00")
> y <- as.POSIXct("2012-10-25 06:00:00", tz = "GMT")
> y-x

a) Time difference of 1 hour
b) Time difference of 1 min
c) Time difference of 1 sec
d) Time difference of 5 sec
View Answer

Answer: a
Explanation: POSIXct is just a very large integer under the hood.

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 interviews, 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.