Last active
March 28, 2019 03:12
-
-
Save apreshill/e5550098e69a9f1537b32fb63734e282 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
``` r | |
library(tidyverse) | |
#> Warning: package 'forcats' was built under R version 3.5.2 | |
library(lubridate) | |
#> | |
#> Attaching package: 'lubridate' | |
#> The following object is masked from 'package:base': | |
#> | |
#> date | |
gbbo <- tibble::tribble( | |
~series, ~episodes, ~premiere, ~finale, ~winner, ~avg_uk_viewers, ~day_of_week, ~timeslot, | |
1, 6, "17 August 2010", "21 September 2010", "Edd Kimber", 2.77, "Tuesday", "8:00pm", | |
2, 8, "14 August 2011", "4 October 2011", "Joanne Wheatley", 4, "Tuesday", "8:00pm", | |
3, 10, "14 August 2012", "16 October 2012", "John Whaite", 5, "Tuesday", "8:00pm", | |
4, 10, "20 August 2013", "22 October 2013", "Frances Quinn", 7.35, "Tuesday", "8:00pm", | |
5, 10, "6 August 2014", "8 October 2014", "Nancy Birtwhistle", 10.04, "Wednesday", "8:00pm", | |
6, 10, "5 August 2015", "7 October 2015", "Nadiya Hussain", 12.5, "Wednesday", "8:00pm", | |
7, 10, "24 August 2016", "26 October 2016", "Candice Brown", 13.85, "Wednesday", "8:00pm", | |
8, 10, "29 August 2017", "31 October 2017", "Sophie Faldo", 9.29, "Tuesday", "8:00pm" | |
) %>% | |
mutate_at(vars(premiere, finale), dmy) %>% | |
mutate(timeslot = parse_number(timeslot)) %>% | |
select(series, premiere, finale, timeslot) | |
# column will always be named "new_time" | |
assign_time <- function(data, date_var, he_var){ | |
quo_date <- enquo(date_var) | |
quo_he <- enquo(he_var) | |
data %>% | |
mutate(new_time = ymd_h(paste(year(!!quo_date), "-", | |
month(!!quo_date), "-", | |
day(!!quo_date), "-", | |
!!quo_he, sep = ""))) | |
} | |
assign_time(gbbo, premiere, timeslot) | |
#> # A tibble: 8 x 5 | |
#> series premiere finale timeslot new_time | |
#> <dbl> <date> <date> <dbl> <dttm> | |
#> 1 1 2010-08-17 2010-09-21 8 2010-08-17 08:00:00 | |
#> 2 2 2011-08-14 2011-10-04 8 2011-08-14 08:00:00 | |
#> 3 3 2012-08-14 2012-10-16 8 2012-08-14 08:00:00 | |
#> 4 4 2013-08-20 2013-10-22 8 2013-08-20 08:00:00 | |
#> 5 5 2014-08-06 2014-10-08 8 2014-08-06 08:00:00 | |
#> 6 6 2015-08-05 2015-10-07 8 2015-08-05 08:00:00 | |
#> 7 7 2016-08-24 2016-10-26 8 2016-08-24 08:00:00 | |
#> 8 8 2017-08-29 2017-10-31 8 2017-08-29 08:00:00 | |
assign_time(gbbo, finale, timeslot) | |
#> # A tibble: 8 x 5 | |
#> series premiere finale timeslot new_time | |
#> <dbl> <date> <date> <dbl> <dttm> | |
#> 1 1 2010-08-17 2010-09-21 8 2010-09-21 08:00:00 | |
#> 2 2 2011-08-14 2011-10-04 8 2011-10-04 08:00:00 | |
#> 3 3 2012-08-14 2012-10-16 8 2012-10-16 08:00:00 | |
#> 4 4 2013-08-20 2013-10-22 8 2013-10-22 08:00:00 | |
#> 5 5 2014-08-06 2014-10-08 8 2014-10-08 08:00:00 | |
#> 6 6 2015-08-05 2015-10-07 8 2015-10-07 08:00:00 | |
#> 7 7 2016-08-24 2016-10-26 8 2016-10-26 08:00:00 | |
#> 8 8 2017-08-29 2017-10-31 8 2017-10-31 08:00:00 | |
# now say I want to name that new column from a user-provided string | |
assign_new_time <- function(data, date_var, he_var, new_time){ | |
quo_date <- enquo(date_var) | |
quo_he <- enquo(he_var) | |
data %>% | |
mutate(!!new_time := ymd_h(paste(year(!!quo_date), "-", | |
month(!!quo_date), "-", | |
day(!!quo_date), "-", | |
!!quo_he, sep = ""))) | |
} | |
assign_new_time(gbbo, premiere, timeslot, "premiere_time") | |
#> # A tibble: 8 x 5 | |
#> series premiere finale timeslot premiere_time | |
#> <dbl> <date> <date> <dbl> <dttm> | |
#> 1 1 2010-08-17 2010-09-21 8 2010-08-17 08:00:00 | |
#> 2 2 2011-08-14 2011-10-04 8 2011-08-14 08:00:00 | |
#> 3 3 2012-08-14 2012-10-16 8 2012-08-14 08:00:00 | |
#> 4 4 2013-08-20 2013-10-22 8 2013-08-20 08:00:00 | |
#> 5 5 2014-08-06 2014-10-08 8 2014-08-06 08:00:00 | |
#> 6 6 2015-08-05 2015-10-07 8 2015-08-05 08:00:00 | |
#> 7 7 2016-08-24 2016-10-26 8 2016-08-24 08:00:00 | |
#> 8 8 2017-08-29 2017-10-31 8 2017-08-29 08:00:00 | |
assign_new_time(gbbo, finale, timeslot, "finale_time") | |
#> # A tibble: 8 x 5 | |
#> series premiere finale timeslot finale_time | |
#> <dbl> <date> <date> <dbl> <dttm> | |
#> 1 1 2010-08-17 2010-09-21 8 2010-09-21 08:00:00 | |
#> 2 2 2011-08-14 2011-10-04 8 2011-10-04 08:00:00 | |
#> 3 3 2012-08-14 2012-10-16 8 2012-10-16 08:00:00 | |
#> 4 4 2013-08-20 2013-10-22 8 2013-10-22 08:00:00 | |
#> 5 5 2014-08-06 2014-10-08 8 2014-10-08 08:00:00 | |
#> 6 6 2015-08-05 2015-10-07 8 2015-10-07 08:00:00 | |
#> 7 7 2016-08-24 2016-10-26 8 2016-10-26 08:00:00 | |
#> 8 8 2017-08-29 2017-10-31 8 2017-10-31 08:00:00 | |
``` | |
<sup>Created on 2019-03-27 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)</sup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment