So I am trying to make a package (I have not included my roxygen2 headers below):
I have this function:
date_from_text <- function(df, x){
x <- rlang::enquo(x)
name <- rlang::quo_name(x)
df %>%
dplyr::mutate(!!name := lubridate::ymd_hms(!!x))
}
And when the datetime column has the right class then I use this to extract all the component.
date_columns <- function(df, x){
x <- rlang::enquo(x)
df %>%
dplyr::mutate(year=lubridate::year(!!x),
ydag=lubridate::yday(!!x),
weekday=lubridate::wday(!!x, label=FALSE),
hour = lubridate::hour(!!x),
hour_min= hms::as.hms(lubridate::force_tz(!!x)),
week_num = lubridate::week(!!x),
month = lubridate::month(!!x),
date = lubridate::date(!!x))
}
I don't wnat the date_from_text
function to be included in the NAMESPACE
and I want to include it somehow in the date_columns
function. Something like checking if the timestamp has the right class and if not then change the class.. and then create all the datetime components.
I don't know how to call the first function inside the other function.
data for testing :
df <- structure(list(time = c("2018-01-30 20:08:18", "2018-02-01 21:01:25",
"2018-01-31 23:25:12", "2018-01-28 23:45:34", "2018-01-31 12:00:55",
"2018-02-04 09:15:31", "2018-01-27 21:08:02", "2018-02-08 01:50:31",
"2018-02-01 03:15:43", "2018-02-04 01:04:52"), type = c("A",
"D", "B", "B", "B", "D", "C", "C", "C", "A")), .Names = c("time",
"type"), row.names = c(NA, -10L), class = c("tbl_df", "tbl",
"data.frame"))
UPDATE: So I have now included date_from_text
into the date_columns
date_columns <- function(df, x){
x <- rlang::enquo(x)
out <- df %>%
date_from_text(!!x) %>%
dplyr::mutate(year=lubridate::year(!!x),
ydag=lubridate::yday(!!x),
weekday=lubridate::wday(!!x, label=FALSE),
hour = lubridate::hour(!!x),
hour_min= hms::as.hms(lubridate::force_tz(!!x)),
week_num = lubridate::week(!!x),
month = lubridate::month(!!x),
date = lubridate::date(!!x))
out
}
So I don't understand why I have to use !!x
again inside date_columns
?? It is already included inside date_from_text
. I am calling the function not creating it...