README.md

TimeKeeper package vignette

General Instructions on how to download this package is here.

Basically:

# library(devtools);   # loads the devtools library to allow developer tools
#  install_github("hochoy/timekeeper")      # downloads and installs my timekeeper package

This package is called Time Keeper because it tracks the date, time, and day/night of a given dataframe's creation-time. It is especially useful when we have many versions of the same/similar dataframe and we want to keep track of when each dataframe was created. It does however require the user to remember to use the functions in this package immediately after making their dataframe.

The functions

This package was originally the foofactors basic package from lecture. However, I have removed their roxygen export tags to exclude the foofactors functions from the NAMESPACE. Instead, I have created 3 simple functions in this package:

  1. is_daynight()
  2. add_datetime()
  3. add_daynight()

is_daynight()

This function allows me to determine if a colon-separated character/factor string representing time is in the day (before 12pm) or night (after 12pm). This function is a dependency of another function, add_daynight() as we will see later.

library(timekeeper)
a <- "12:30:12"
b <- "4:40:50"
is_daynight(a) # if the hour > 12
is_daynight(b) # if the hour < 12
## [1] "night"
## [1] "day"

add_datetime()

This function allows me to add two columns for date and time to any dataframe. The date and time uses Sys.time() to set the time of dataframe creation. Its purpose is to log the creation of any new dataframe so that rbind-merged dataframes still retain their date and time of creation. This function is a dependency of the add_daynight() function.

library(gridExtra)
fruit_harvest <- data.frame(apples = c(4,10,12), oranges = c(8,9,10))
fruit_harvest_dt <- add_datetime(fruit_harvest)
knitr::kable(fruit_harvest) #without date time

| apples| oranges| |-------:|--------:| | 4| 8| | 10| 9| | 12| 10|

knitr::kable(fruit_harvest_dt) #with date and time

| apples| oranges| date | time | |-------:|--------:|:-----------|:---------| | 4| 8| 2015-11-28 | 13:49:02 | | 10| 9| 2015-11-28 | 13:49:02 | | 12| 10| 2015-11-28 | 13:49:02 |

add_daynight()

The final function add_daynight requires the first two functions to work. Specifically, it needs add_datetime to add the date and time columns to a dataframe and it needs is_daynight to determine if the time is before or after 12pm. This function has been designed to handle both time-less and time-available dataframes. Both will yield dataframes with date,time and day_or_night columns

suppressPackageStartupMessages(library(dplyr))
fruit_harvest <- data.frame(apples = c(4,10,12), oranges = c(8,9,10))
fruit_harvest %>% add_daynight() %>% knitr::kable()   # data without time will have time added as well as day/night

| apples| oranges| date | time | day_or_night | |-------:|--------:|:-----------|:---------|:---------------| | 4| 8| 2015-11-28 | 13:49:02 | night | | 10| 9| 2015-11-28 | 13:49:02 | night | | 12| 10| 2015-11-28 | 13:49:02 | night |

fruit_harvest_dt %>% add_daynight() %>% knitr::kable() # data with time will have day/night only added

| apples| oranges| date | time | day_or_night | |-------:|--------:|:-----------|:---------|:---------------| | 4| 8| 2015-11-28 | 13:49:02 | night | | 10| 9| 2015-11-28 | 13:49:02 | night | | 12| 10| 2015-11-28 | 13:49:02 | night |



hochoy/r_timekeeper documentation built on May 17, 2019, 4:36 p.m.