Home
51.
Which of the three built-in classes would you use to store dates with times in a data frame?

POSIXct classes must be used. Dates don’t store the time information, and POS IXlt dates store their data as lists, which won’t fit inside a data frame.

52.
When is the origin (time 0) for POSIXct and Date dates?

Midnight at the start of January 1, 1970.

53.
What formatting string would you use to display a date as the complete month name followed by the four-digit year number?

"%B %Y"

54.
How would you shift a POSIXct date one hour into the future?

Add 3,600 seconds to it. For example:
x <- Sys.time()
x + 3600
## [1] "2013-07-17 22:44:55 BST"

55.
Using the lubridate package, consider two intervals starting on January 1, 2016. Which will finish later, a duration of one year or a period of one year?

The period will be longer, because 2016 is a leap year. A duration is always exactly 60 * 60 * 24 * 365 seconds. A period of one year will be 366 days in a leap year.

56.
How do you find all the datasets built into R and the packages on your machine?

Call the data function with no arguments.

57.
What is the difference between the read.csv and read.csv2 functions?

read.csv assumes that a decimal place is represented by a full stop (period) and that each item is separated by a comma, whereas read.csv2 assumes that a decimal place is represented by a comma and that each item is separated by a semicolon. read.csv is used for data created in locales where a period is used as a decimal place (most English-speaking locales, for example). read.csv2 is for data created in locales where a comma is used (most European locales, for example). If you are unsure, simply open your data file in a text editor.

58.
How would you import data from an Excel spreadsheet into R?

read.xlsx2 from the xlsx package is a good first choice, but there is also read.xlsx in the same package, and different functions in several other packages.

59.
How would you import data from a CSV file found on the Internet?

You can simply pass the URL to read.csv, or use download.file to get a local copy.

60.
The DBI package provides a consistent interface to several database management systems. Which ones are supported?

Currently SQLite, MySQL, PostgreSQL, and Oracle databases are supported.