Apply and Dates

Some examples from Chapter 5

apply

Teams %>% select(15:40) %>%
  apply(MARGIN = 2, FUN = mean, na.rm = TRUE)
           R           AB            H          X2B          X3B           HR           BB           SO 
 682.3992945 5146.4733686 1346.9393298  228.1276896   46.9135802  102.0451499  473.9590829  744.1528545 
          SB           CS          HBP           SF           RA           ER          ERA           CG 
 111.9628391   48.5323887   56.0333333   44.4294118  682.3992945  571.9523810    3.8188783   49.9760141 
         SHO           SV       IPouts           HA          HRA          BBA          SOA            E 
   9.6592593   23.8673721 4025.6451499 1346.7516755  102.0451499  474.3167549  737.2416226  185.3654321 
          DP           FP 
 140.2374901    0.9654473 

lapply and sapply

angles_names
[1] "Los Angeles Angels"            "California Angels"             "Anaheim Angels"               
[4] "Los Angeles Angels of Anaheim"
lapply(angles_names, FUN = nchar)
[[1]]
[1] 18

[[2]]
[1] 17

[[3]]
[1] 14

[[4]]
[1] 29
sapply(angles_names, FUN = nchar)
           Los Angeles Angels             California Angels                Anaheim Angels 
                           18                            17                            14 
Los Angeles Angels of Anaheim 
                           29 

Example of a function used with lapply

angles_list
[[1]]

[[2]]

[[3]]

[[4]]
NA

Dates

OrdwayBirds %>% select(Timestamp, Year, Month, Day) %>%
  glimpse()
Observations: 15,829
Variables: 4
$ Timestamp <chr> "4/14/2010 13:20:56", "", "5/13/2010 16:00:30", "5/13/2010 16:02:15", "5/13/2010 16:03:...
$ Year      <chr> "1972", "", "1972", "1972", "1972", "1972", "1972", "1972", "1972", "1972", "1972", "19...
$ Month     <chr> "7", "", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7", "7"...
$ Day       <chr> "16", "", "16", "16", "16", "16", "16", "16", "16", "16", "17", "18", "18", "18", "18",...

covert to numbers

OrdwayBirds %>% select(Timestamp, Year, Month, Day) %>%
  glimpse()
Observations: 15,829
Variables: 4
$ Timestamp <chr> "4/14/2010 13:20:56", "", "5/13/2010 16:00:30", "5/13/2010 16:02:15", "5/13/2010 16:03:...
$ Year      <dbl> 1972, NA, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972,...
$ Month     <dbl> 7, NA, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,...
$ Day       <dbl> 16, NA, 16, 16, 16, 16, 16, 16, 16, 16, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 20,...

convert Timestamp

WhenAndWho <- OrdwayBirds %>%
  mutate(When = mdy_hms(Timestamp)) %>%
  select(Timestamp, Year, Month, Day, When, DataEntryPerson) %>%
  glimpse()
Observations: 15,829
Variables: 6
$ Timestamp       <chr> "4/14/2010 13:20:56", "", "5/13/2010 16:00:30", "5/13/2010 16:02:15", "5/13/2010...
$ Year            <dbl> 1972, NA, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972...
$ Month           <dbl> 7, NA, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7...
$ Day             <dbl> 16, NA, 16, 16, 16, 16, 16, 16, 16, 16, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, ...
$ When            <dttm> 2010-04-14 13:20:56, NA, 2010-05-13 16:00:30, 2010-05-13 16:02:15, 2010-05-13 1...
$ DataEntryPerson <chr> "Jerald Dosch", "Caitlin Baker", "Caitlin Baker", "Caitlin Baker", "Caitlin Bake...

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.

as.Date(today()) - as.Date("01/01/1970")
LS0tCnRpdGxlOiAiQXBwbHkgYW5kIERhdGVzIgphdXRob3I6ICJQcm9mLiBFcmljIEEuIFN1ZXNzIgpkYXRlOiAiMTAvMDMvMjAxOCIKb3V0cHV0OgogIHdvcmRfZG9jdW1lbnQ6IGRlZmF1bHQKICBwZGZfZG9jdW1lbnQ6I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