forked from gertstulp/ggplotgui
-
Notifications
You must be signed in to change notification settings - Fork 2
/
time_detector.R
51 lines (40 loc) · 1.56 KB
/
time_detector.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#' time.detector
#'
#' This function takes character-class columns and attempts to coerce them into Date-class, if reasonable test whether a column conforms to standard date formats.
#'
#' @usage
#' time_detector(df)
#'
#' @param df
#' Define this parameter.
#'
#' @return
#' What does this function return?
#'
#' @export
#'
time_detector <- function(df) {
if(all(!as.vector(t(as.data.frame(sapply(df, class))[,1])) %in% c("Date", "POSIXc", "POSIXlt", "POSIXct"))) {
for(i in 1:dim(df)[2]) {
if(is.character(df[,i]) | is.factor(df[,i])) {
var <- as.character(unique(df[,i]))
formatted.date.col <- NA
date.formats <- c("%d%b%Y", "%d%.%m%.%Y", "%m%.%d%.%Y", "%d%.%m%.%y", "%m%.%d%.%y", "%d%.%b%.%Y", "%b%.%d%.%Y", "%Y%.%m%.%d", "%Y%.%d%.%m", "%y%.%m%.%d", "%b %d, %Y")
date.tests <- data.frame(matrix(NA, nrow = length(var), ncol = length(date.formats)), row.names = as.character(var))
colnames(date.tests) <- date.formats
date.tests2 <- as.data.frame(
lapply(as.list(date.formats), function(x){
ifelse(is.na(parse_date(var, x)), NA, 1)
}))
names(date.tests2) <- date.formats
max.dates.coerced <- max(colSums(date.tests2), na.rm = T)
coerced.date <- NA
if(!is.infinite(max.dates.coerced)) {
coerced.date <- parse_date(df[,i], date.formats[colSums(date.tests2) %in% max.dates.coerced])
}
df$coerced.date <- coerced.date
} else {next}
}
}
return(df)
}