re: #1 & #2 when and why to use
vars() - if it is of any help here is a short example of how some of these things save me lots of time & typing.
In my daily workflow i get data coming in that has many columns, say 50 - and a subset of them (like 10) are dates and datetimes unfortunately coded as characters. Think of them as start date, end date, departure date, return date etc. I want to convert them to POSIX datetimes and maybe further extract days of week, days of month and similar features.
So first approach would be to do this separately for each char-date column
# the format is something like "2017-09-16 15:30:00")
my_format <- "%Y-%m-%d %H:%M:%S"
col1_date = as.POSIXct(col1_date, format = my_format),
col2_date = as.POSIXct(col2_date, format = my_format),
col10_date = as.POSIXct(col10_date, format = my_format)
In this case it comes really handy that one can simply do
funs(posix = as.POSIXct),
format = my_format
and be done with all of them in one call.
Some assumptions that make this easier and possible are that all the char-date columns have “date” in their name which make the
vars() call simple. This might not be always the case in general but it’s easy to rename such columns by selecting them by hand and appending “date” to their name for example.
Note also the convenience that by supplying the name
_posix in the
funs() call will result in the new column names having “_posix” appended to their original name automatically.
Furthermore, to get each of the new posix dates columns, day of week for example i could just supply
funs( wday = lubridate::wday) in another call and get all of their days of week in one go.
Hope this small example helps a bit to show how practical these tools are.