--- title: "Apply and Dates, Updated using Map" author: "Prof. Eric A. Suess" date: "09/30/2020" output: pdf_document: default html_notebook: default word_document: default --- # Apply and Dates Some examples from Chapter 5 ### apply, updated using map() and map_df() With the map functions there is no need for the MARGIN option to specify rows (1) or columns (2). ```{r message = FALSE} library(pacman) p_load(tidyverse, purr, stringr, Lahman) Teams %>% select(15:40) %>% apply(MARGIN = 2, FUN = mean, na.rm = TRUE) Teams %>% select(15:40) %>% map(., mean, na.rm=TRUE) %>% unlist() Teams %>% select(15:40) %>% map_df(., mean, na.rm=TRUE) ``` ### lapply and sapply Note that lapply() returns a list. The map() ```{r} angles <- Teams %>% filter(franchID == "ANA") %>% group_by(teamID, name) %>% summarize(began = first(yearID), ended = last(yearID)) %>% arrange(began) angles ``` ```{r} angles_names <- angles %>% ungroup(teamID) %>% select(name) class(angles_names) nchar(angles_names[1,1]) nchar(angles_names[2,1]) nchar(angles_names[3,1]) nchar(angles_names[4,1]) ``` ```{r} x <- lapply(angles_names, FUN = nchar) class(x) x ``` ```{r} y <- sapply(angles_names, FUN = nchar) class(y) y ``` ```{r} z <- angles_names %>% map(., str_length) class(z) z ``` ```{r} z <- angles_names %>% map_df(., str_length) class(z) z ``` ### Example of a function used with lapply ```{r} top5 <- function(x, teamnames) { x %>% filter(name == teamnames) %>% select(teamID, yearID, W, L, name) %>% arrange(desc(W)) %>% head(n = 5) } angles_list <- lapply(angles_names, FUN = top5, x = Teams) class(angles_list) angles_list angles_list <- angles_names %>% map(., top5, x = Teams) class(angles_list) angles_list ``` ### Dates ```{r} library(mdsr) OrdwayBirds OrdwayBirds %>% select(Timestamp, Year, Month, Day) %>% glimpse() ``` covert to numbers ```{r} OrdwayBirds %>% select(Timestamp, Year, Month, Day) %>% glimpse() ``` convert Timestamp ```{r} library(lubridate) WhenAndWho <- OrdwayBirds %>% mutate(When = mdy_hms(Timestamp)) %>% select(Timestamp, Year, Month, Day, When, DataEntryPerson) %>% glimpse() ``` ```{r} WhenAndWho %>% ggplot(aes(x = When, y = DataEntryPerson)) + geom_point(alpha = 0.1, position = "jitter") ``` ```{r} WhenAndWho %>% group_by(DataEntryPerson) %>% summarize(start = first(When), finish = last(When)) %>% mutate( duration = interval(start, finish) / ddays(1) ) ``` ```{r} now() as.Date(now()) today() as.Date(today()) as.Date(now()) - as.Date(today()) ``` ```{r} now() as_date(now()) today() as_date(today()) as_date(now()) - as_date(today()) ``` How many days have you woken up in the morning? Change the date. What is wrong with this? ```{r} as.Date(today()) - as.Date("01/01/1970") ``` Note the use of the date format. ```{r} as.Date(today()) - as.Date("01/01/1970", "%m/%d/%Y") ``` In the lubradate package there is the mdy() function. ```{r} as_date(today()) as_date(mdy("01/01/1970")) as_date(today()) - as_date(mdy("01/01/1970")) ```