Created
March 28, 2019 00:02
-
-
Save ateucher/d6e4f61cae0bcab391e7eaa1f1eb9664 to your computer and use it in GitHub Desktop.
This file contains 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
library(dplyr) | |
library(lubridate) | |
# sample table | |
foo <- tibble( | |
a = rnorm(5), | |
date = seq(Sys.Date(), by = "1 day", length.out = 5), | |
he = 1:5 | |
) | |
assign_time <- function(data_sent, date_var = "date", he_var = "he") { | |
# Capture strings as 'symbols': | |
date_var <- sym(date_var) | |
he_var <- sym(he_var) | |
# Use mutate, unquote symbols with !! | |
mutate( | |
data_sent, | |
time = ymd_h(paste(!!date_var, !!he_var)) | |
) | |
} | |
assign_time(foo) | |
## Using just base functions, no dplyr or lubridate: | |
assign_time_base <- function(data_sent, date_var = "date", he_var = "he") { | |
data_sent$time <- as.POSIXct(paste(data_sent[[date_var]], data_sent[[he_var]])) | |
data_sent | |
} | |
assign_time_base(foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment