#Load Tidyverse

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.3     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()

#Read Data Presidential

library(datasets)
data(presidential)
presidential
## # A tibble: 11 x 4
##    name       start      end        party     
##    <chr>      <date>     <date>     <chr>     
##  1 Eisenhower 1953-01-20 1961-01-20 Republican
##  2 Kennedy    1961-01-20 1963-11-22 Democratic
##  3 Johnson    1963-11-22 1969-01-20 Democratic
##  4 Nixon      1969-01-20 1974-08-09 Republican
##  5 Ford       1974-08-09 1977-01-20 Republican
##  6 Carter     1977-01-20 1981-01-20 Democratic
##  7 Reagan     1981-01-20 1989-01-20 Republican
##  8 Bush       1989-01-20 1993-01-20 Republican
##  9 Clinton    1993-01-20 2001-01-20 Democratic
## 10 Bush       2001-01-20 2009-01-20 Republican
## 11 Obama      2009-01-20 2017-01-20 Democratic

#Knowing the Dimension of Data

dim(presidential)
## [1] 11  4

#Showing The Top 6 Data

head(presidential)
## # A tibble: 6 x 4
##   name       start      end        party     
##   <chr>      <date>     <date>     <chr>     
## 1 Eisenhower 1953-01-20 1961-01-20 Republican
## 2 Kennedy    1961-01-20 1963-11-22 Democratic
## 3 Johnson    1963-11-22 1969-01-20 Democratic
## 4 Nixon      1969-01-20 1974-08-09 Republican
## 5 Ford       1974-08-09 1977-01-20 Republican
## 6 Carter     1977-01-20 1981-01-20 Democratic

#Glimpse

glimpse(presidential)
## Rows: 11
## Columns: 4
## $ name  <chr> "Eisenhower", "Kennedy", "Johnson", "Nixon", "Ford", "Carter", "~
## $ start <date> 1953-01-20, 1961-01-20, 1963-11-22, 1969-01-20, 1974-08-09, 197~
## $ end   <date> 1961-01-20, 1963-11-22, 1969-01-20, 1974-08-09, 1977-01-20, 198~
## $ party <chr> "Republican", "Democratic", "Democratic", "Republican", "Republi~

#Select Name and Party

select(presidential, name, party)
## # A tibble: 11 x 2
##    name       party     
##    <chr>      <chr>     
##  1 Eisenhower Republican
##  2 Kennedy    Democratic
##  3 Johnson    Democratic
##  4 Nixon      Republican
##  5 Ford       Republican
##  6 Carter     Democratic
##  7 Reagan     Republican
##  8 Bush       Republican
##  9 Clinton    Democratic
## 10 Bush       Republican
## 11 Obama      Democratic

#Select Name and Party with Pipes Operator

presidential %>% select(name, party)
## # A tibble: 11 x 2
##    name       party     
##    <chr>      <chr>     
##  1 Eisenhower Republican
##  2 Kennedy    Democratic
##  3 Johnson    Democratic
##  4 Nixon      Republican
##  5 Ford       Republican
##  6 Carter     Democratic
##  7 Reagan     Republican
##  8 Bush       Republican
##  9 Clinton    Democratic
## 10 Bush       Republican
## 11 Obama      Democratic

#Filter Party = Democratic

filter(presidential, party=="Democratic")
## # A tibble: 5 x 4
##   name    start      end        party     
##   <chr>   <date>     <date>     <chr>     
## 1 Kennedy 1961-01-20 1963-11-22 Democratic
## 2 Johnson 1963-11-22 1969-01-20 Democratic
## 3 Carter  1977-01-20 1981-01-20 Democratic
## 4 Clinton 1993-01-20 2001-01-20 Democratic
## 5 Obama   2009-01-20 2017-01-20 Democratic

#Filter party = Democratic with Pipes Operator

Democratic <- presidential %>% filter(party=="Democratic")
Democratic
## # A tibble: 5 x 4
##   name    start      end        party     
##   <chr>   <date>     <date>     <chr>     
## 1 Kennedy 1961-01-20 1963-11-22 Democratic
## 2 Johnson 1963-11-22 1969-01-20 Democratic
## 3 Carter  1977-01-20 1981-01-20 Democratic
## 4 Clinton 1993-01-20 2001-01-20 Democratic
## 5 Obama   2009-01-20 2017-01-20 Democratic

#Nrow of Party=Democratic

nrow(Democratic)
## [1] 5

#Combine Select() and Filter()

presidential %>% filter(start > "1980-01-01", party=="Democratic") %>% select(name)
## # A tibble: 2 x 1
##   name   
##   <chr>  
## 1 Clinton
## 2 Obama

#Mutate

library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
mypresidents <- presidential %>% mutate(term.length = interval(start, end) /years(1))
mypresidents
## # A tibble: 11 x 5
##    name       start      end        party      term.length
##    <chr>      <date>     <date>     <chr>            <dbl>
##  1 Eisenhower 1953-01-20 1961-01-20 Republican        8   
##  2 Kennedy    1961-01-20 1963-11-22 Democratic        2.84
##  3 Johnson    1963-11-22 1969-01-20 Democratic        5.16
##  4 Nixon      1969-01-20 1974-08-09 Republican        5.55
##  5 Ford       1974-08-09 1977-01-20 Republican        2.45
##  6 Carter     1977-01-20 1981-01-20 Democratic        4   
##  7 Reagan     1981-01-20 1989-01-20 Republican        8   
##  8 Bush       1989-01-20 1993-01-20 Republican        4   
##  9 Clinton    1993-01-20 2001-01-20 Democratic        8   
## 10 Bush       2001-01-20 2009-01-20 Republican        8   
## 11 Obama      2009-01-20 2017-01-20 Democratic        8

#Mean of Term.Length then Round(1)

mypresidents$term.length %>% mean() %>% round(1)
## [1] 5.8

#Summarize The Mean of Term Length of Each Party

mypresidents %>% group_by(party) %>% summarise(mean=mean(term.length))
## # A tibble: 2 x 2
##   party       mean
##   <chr>      <dbl>
## 1 Democratic   5.6
## 2 Republican   6

#Mutate with Pipes Operator

mypresidents <- mypresidents %>% mutate(elected = year(start) - 1)
mypresidents
## # A tibble: 11 x 6
##    name       start      end        party      term.length elected
##    <chr>      <date>     <date>     <chr>            <dbl>   <dbl>
##  1 Eisenhower 1953-01-20 1961-01-20 Republican        8       1952
##  2 Kennedy    1961-01-20 1963-11-22 Democratic        2.84    1960
##  3 Johnson    1963-11-22 1969-01-20 Democratic        5.16    1962
##  4 Nixon      1969-01-20 1974-08-09 Republican        5.55    1968
##  5 Ford       1974-08-09 1977-01-20 Republican        2.45    1973
##  6 Carter     1977-01-20 1981-01-20 Democratic        4       1976
##  7 Reagan     1981-01-20 1989-01-20 Republican        8       1980
##  8 Bush       1989-01-20 1993-01-20 Republican        4       1988
##  9 Clinton    1993-01-20 2001-01-20 Democratic        8       1992
## 10 Bush       2001-01-20 2009-01-20 Republican        8       2000
## 11 Obama      2009-01-20 2017-01-20 Democratic        8       2008

#Arrange from Term.Length

mypresidents %>% arrange(term.length)
## # A tibble: 11 x 6
##    name       start      end        party      term.length elected
##    <chr>      <date>     <date>     <chr>            <dbl>   <dbl>
##  1 Ford       1974-08-09 1977-01-20 Republican        2.45    1973
##  2 Kennedy    1961-01-20 1963-11-22 Democratic        2.84    1960
##  3 Carter     1977-01-20 1981-01-20 Democratic        4       1976
##  4 Bush       1989-01-20 1993-01-20 Republican        4       1988
##  5 Johnson    1963-11-22 1969-01-20 Democratic        5.16    1962
##  6 Nixon      1969-01-20 1974-08-09 Republican        5.55    1968
##  7 Eisenhower 1953-01-20 1961-01-20 Republican        8       1952
##  8 Reagan     1981-01-20 1989-01-20 Republican        8       1980
##  9 Clinton    1993-01-20 2001-01-20 Democratic        8       1992
## 10 Bush       2001-01-20 2009-01-20 Republican        8       2000
## 11 Obama      2009-01-20 2017-01-20 Democratic        8       2008

#Arrange Descending of Term.Length and Elected with Pipes Operator

mypresidents %>% arrange(desc(term.length), elected)
## # A tibble: 11 x 6
##    name       start      end        party      term.length elected
##    <chr>      <date>     <date>     <chr>            <dbl>   <dbl>
##  1 Eisenhower 1953-01-20 1961-01-20 Republican        8       1952
##  2 Reagan     1981-01-20 1989-01-20 Republican        8       1980
##  3 Clinton    1993-01-20 2001-01-20 Democratic        8       1992
##  4 Bush       2001-01-20 2009-01-20 Republican        8       2000
##  5 Obama      2009-01-20 2017-01-20 Democratic        8       2008
##  6 Nixon      1969-01-20 1974-08-09 Republican        5.55    1968
##  7 Johnson    1963-11-22 1969-01-20 Democratic        5.16    1962
##  8 Carter     1977-01-20 1981-01-20 Democratic        4       1976
##  9 Bush       1989-01-20 1993-01-20 Republican        4       1988
## 10 Kennedy    1961-01-20 1963-11-22 Democratic        2.84    1960
## 11 Ford       1974-08-09 1977-01-20 Republican        2.45    1973

#Mutate Rename term.length with term_length

mypresidents %>% rename(term_length = term.length)
## # A tibble: 11 x 6
##    name       start      end        party      term_length elected
##    <chr>      <date>     <date>     <chr>            <dbl>   <dbl>
##  1 Eisenhower 1953-01-20 1961-01-20 Republican        8       1952
##  2 Kennedy    1961-01-20 1963-11-22 Democratic        2.84    1960
##  3 Johnson    1963-11-22 1969-01-20 Democratic        5.16    1962
##  4 Nixon      1969-01-20 1974-08-09 Republican        5.55    1968
##  5 Ford       1974-08-09 1977-01-20 Republican        2.45    1973
##  6 Carter     1977-01-20 1981-01-20 Democratic        4       1976
##  7 Reagan     1981-01-20 1989-01-20 Republican        8       1980
##  8 Bush       1989-01-20 1993-01-20 Republican        4       1988
##  9 Clinton    1993-01-20 2001-01-20 Democratic        8       1992
## 10 Bush       2001-01-20 2009-01-20 Republican        8       2000
## 11 Obama      2009-01-20 2017-01-20 Democratic        8       2008

#Summarize

mypresidents %>% summarise(N = n(), first_year = min(year(start)), last_year = max(year(end)), num_dems = sum(party == "Democratic"), years = sum(term.length), avg_term.length = mean(term.length))
## # A tibble: 1 x 6
##       N first_year last_year num_dems years avg_term.length
##   <int>      <dbl>     <dbl>    <int> <dbl>           <dbl>
## 1    11       1953      2017        5    64            5.82