Created
July 28, 2015 04:57
-
-
Save kbroman/6b4ebcabc054d9cee9d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code to check readxl's handling of dates | |
library(readxl) | |
library(testthat) | |
library(lubridate) | |
## date in column names | |
# read data with date in column names | |
d1a <- read_excel("date_in_colnames.xlsx") | |
assert_that(colnames(d1a)[2] == "42212") | |
assert_that(colnames(d1a)[3] == "42205") | |
# try using col_names=FALSE | |
d1b <- read_excel("date_in_colnames.xlsx", col_names=FALSE) | |
assert_that(d1b[1,"X1"] == "42212") | |
assert_that(d1b[1,"X2"] == "42205") | |
# try using col_types="text" | |
d1c <- read_excel("date_in_colnames.xlsx", col_types=rep("text", 3)) | |
assert_that(colnames(d1c)[2] == "42212") | |
assert_that(colnames(d1c)[3] == "42205") | |
# both col_names=FALSE and col_types="text" | |
d1d <- read_excel("date_in_colnames.xlsx", col_names=FALSE, col_types=rep("text", 3)) | |
assert_that(d1d[1,"X1"] == "42212") | |
assert_that(d1d[1,"X2"] == "42205") | |
## date in body of data | |
# read data with date in body | |
d2a <- read_excel("date_in_body.xlsx") | |
assert_that(d2a[1,"date"] == ymd("2015-07-27")) | |
assert_that(d2a[4,"date"] == ymd("2015-07-20")) | |
# try using col_types="text" | |
d2b <- read_excel("date_in_body.xlsx", col_types=rep("text", 3)) | |
assert_that(d2b[1,"date"] == "42212") | |
assert_that(d2b[4,"date"] == "42205") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment