diff --git a/Gilbert_floor_date_and_ceiling_date.qmd b/Gilbert_floor_date_and_ceiling_date.qmd new file mode 100644 index 0000000..132af81 --- /dev/null +++ b/Gilbert_floor_date_and_ceiling_date.qmd @@ -0,0 +1,104 @@ +--- +title: "Floor_date() and Ceiling_date()" +Author: "Jeffrey Gilbert" +format: html +--- + +```{r} +library(lubridate) +library(tidyverse) +library(dplyr) +data("storms") +``` + +First, the data needs to be modified so that it is in a workable format. +```{r} +stormsdate <- storms %>% + mutate(date = paste(year,"-", month,"-", day, " ", hour, sep = "")) #merging the year, month, day, and hour columns into a single column. +``` + +```{r} +tidystormsdate <- ymd_h(stormsdate$date) #changing the date into a readable format for the lubridate functions. ymd stands for year, month, day, and h stands for hour. +``` + +What are the functions floor_date() and ceiling_date()? +```{r} +?floor_date() +``` + +```{r} +?ceiling_date() +``` + +The floor function allows the user to round down to a designated date or time boundary. Similarly, the ceiling function will do this, but it will round up instead of down. With floor_date() we can round down to the year, halfyear, quarter, bimonth, month, day, hour, minute, and second. Ceiling_date() will also round up by these date/time intervals. The dataset that I am using does not include minutes or seconds, but we can still see how the time interval functions with the hours. + +Floor_date and ceiling_date are useful functions for users who want to examine whole numbers or dates when dealing with precise data. For example, if the user has times that include both minutes and seconds and the user only wants to examine the information in minutes, then these functions would allow the user to only analyze the minutes by rounding the seconds up or down (depending on whether they decide to use floor_date or ceiling_date). This can also apply to dates. If the user is concerned with the decade and not the individual year, then the user can round up or down by ten years to only see the information by the decade. + +We will begin with how floor_date() works by rounding down by the year. +```{r} +floor_date(tidystormsdate, "year") #Regarding the output, UTC stands for Universal Time Coordinated. +``` +As we can see, it is rounding down by each year, beginning with January 1st and omitting the time. It also produces a large number of entries, so we will only be looking at the head and tail results for the following examples. + +Perhaps we do not want to look at each individual year, but rather the decade. We can do this by setting it to round down by the (x)year: +```{r} +head(floor_date(tidystormsdate, "10 years")) +``` +Even though the dataset begins at at 1975, the floor_date function is rounding down to the decade, which is 1970. +```{r} +tail(floor_date(tidystormsdate, "10 years")) +``` +This dataset stops at the year 2022, and with it rounding down, all of the years begin at 2020. + +What happens if we use ceiling date? +```{r} +head(ceiling_date(tidystormsdate, "10 years")) +``` + +```{r} +tail(ceiling_date(tidystormsdate, "10 years")) +``` +Unlike floor_date, ceiling_date is rounding up. So instead of 1970, the head results are 1980. Additionally, the tail results are 2030 instead of 2020. + +How does this work with units of time? +```{r} +head(floor_date(tidystormsdate, "hour")) +``` +When specifically looking at time, the hour, minutes, and seconds are included alongside the date. Since this dataset is only using time in 6 hour intervals, without minutes and seconds, there is not really a need to round up or down. For example, if we had 06:25:00, then it would have rounded down to 06:00:00. + +We can, however, use floor_date to round down to a specific hour: +```{r} +head(floor_date(tidystormsdate, "12 hours")) +``` + +```{r} +head(floor_date(tidystormsdate, "6 hours")) +``` +When rounding down to 12 hours, we are only getting 12:00 and 00:00. If we drop it down to 6 hours, we now see that 18:00 and 06:00 are included in the output. + +```{r} +head(ceiling_date(tidystormsdate, "12 hours")) +``` + +```{r} +head(ceiling_date(tidystormsdate, "6 hours")) +``` +Ceiling_date is producing results in a similar fashion, but just rounding up by six hours instead of down. + +Just to reinforce how this function works, let's quickly look at months. +```{r} +head(floor_date(tidystormsdate, "month")) +``` + +```{r} +head(ceiling_date(tidystormsdate, "month")) +``` +When floor_date and ceiling_date are rounding by the month, we can see how the initial dates for floor_date are being rounded down to 06, while the ceiling_date dates are being rounded up to 07. + +We can also round down by two months. +```{r} +head(floor_date(tidystormsdate, "bimonth")) +``` +Instead of rounding down to 06, we now see the output being rounded down to 05. + +Overall, floor_date and ceiling_date are useful functions for managing times and dates, especially for aggregating data. \ No newline at end of file