lubridate::floor_date()
Martin Arrigotti
Function of the Week:floor_date()
Martin Arrigotti
2021-02-13
floor_date()
In this document, I will introduce the floor_date() function and show what it’s for.
#load tidyverse up
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.4 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(palmerpenguins)
library(janitor)
##
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
data(penguins)
penguins<- clean_names(penguins_raw)
What is it for?
floor_date() is a relatively simple function that allows you to round a date value into a specified time precision level (so long as the date value contains that level of precision). So if you have a date that gives you the value in seconds, but you only want the year, you can do use floor_date() for that.
#example
# create a date variable with more than the desired level of specificity (I'll do the date and time of this class, with you being 21 seconds late)
# date is of the form YYYY-MM-DD HH-MM-SS
x <- as.POSIXct("2021-02-17 15:15:21",tz="PT")
# lets say we only want to know what day class is on
x_days <- floor_date(x,"days")
x_days
## [1] "2021-02-17"
#for our penguins dataset
floor_date(penguins$date_egg, "years")
## [1] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [6] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [11] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [16] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [21] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [26] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [31] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [36] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [41] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [46] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [51] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [56] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [61] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [66] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [71] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [76] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [81] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [86] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [91] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [96] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [101] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [106] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [111] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [116] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [121] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [126] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [131] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [136] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [141] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [146] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [151] "2009-01-01" "2009-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [156] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [161] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [166] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [171] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [176] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [181] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [186] "2007-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [191] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [196] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [201] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [206] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [211] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [216] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [221] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [226] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [231] "2008-01-01" "2008-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [236] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [241] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [246] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [251] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [256] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [261] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [266] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [271] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [276] "2009-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [281] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [286] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [291] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [296] "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01" "2007-01-01"
## [301] "2007-01-01" "2007-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [306] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [311] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [316] "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01" "2008-01-01"
## [321] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [326] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [331] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [336] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
## [341] "2009-01-01" "2009-01-01" "2009-01-01" "2009-01-01"
Is it helpful?
with the statistical work I’ve done so far, I don’t have much use for this. That said, if you have a study with hundreds or thousands of data points that used a computer program to report the date an event happened (so would likely record down to seconds), and you wanted to know total person-years/days/unit-of-time observed, this would be a very handy tool for getting your data into a format that shows only the most necessary information.