This set of R Programming Interview Questions and Answers for experienced focuses on “Vectorized Operations”
1. Which of the following function gives the day of the week ?
a) weekdays
b) months
c) quarters
d) all of the mentioned
View Answer
Explanation: months function give the month name.
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
Explanation: Character strings can be coerced to Date/Time classes using the as.Date, as.POSIXlt, or as.POSIXct.
3. What would be the output of the following code ?
> p <- as.POSIXlt(x) > names(unclass(p)) > p$wday
a) 1
b) 2
c) 3
d) NULL
View Answer
Explanation: The POSIXlt object contains some useful metadata.
4. What would be the output of the following 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) All of the mentioned
View Answer
Explanation: strptime() takes a character vector that has dates and times and converts them into to a POSIXlt object.
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
Explanation: POSIXct uses a useful class when you want to store times in something like a data frame.
6. What would be the output of the following code ?
> 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) All of the mentioned
View Answer
Explanation: You can use mathematical operations on dates and times.
7. What would be the output of the following 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) All of the mentioned
View Answer
Explanation: Matrix operations are also vectorized, making for nicely compact notation.
8. Which of the followin 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) all of the mentioned
View Answer
Explanation: Date/time classes is that they keep track of all the annoying things about dates and times, like leap years, leap seconds, daylight savings, and time zones..
9. What would be the output of the following 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) None of the mentioned
View Answer
Explanation: POSIXct is just a very large integer under the hood.
10.What would be the output of the following code ?
> x <- matrix(1:4, 2, 2) > y <- matrix(rep(10, 4), 2, 2) > x %*% y
a)
[,1] [,2] [1,] 40 40 [2,] 60 60
b)
[,1] [,2] [1,] 40 40 [2,] 80 60
c)
[,1] [,2] [1,] 40 60 [2,] 60 60
d) None of the mentioned
View Answer
Explanation: We can do element-by-element operations on matrices without having to loop over every element.
Sanfoundry Global Education & Learning Series – R Programming Language.
Here’s the list of Best Reference Books in R Programming Language.