Import data

# excel file
Roundabouts <- read_excel("00_data/Raw data Roundabout.xlsx")

Roundabouts
## # A tibble: 27,887 × 18
##    name    address town_city county_area state_region country    lat  long type 
##    <chr>   <chr>   <chr>     <chr>       <chr>        <chr>    <dbl> <dbl> <chr>
##  1 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  2 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  3 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  4 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  5 Bagley… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  6 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  7 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  8 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  9 Kendal… Madiso… Madison   Dane Co.    WI           United…  -89.4  43.1 Traf…
## 10 Bagley… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
## # ℹ 27,877 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>
list.files(".", recursive = TRUE)
##  [1] "00_data/Raw data Roundabout.xlsx"                                                    
##  [2] "01_module4/data/Salaries.csv"                                                        
##  [3] "01_module4/data/Salaries.xlsx"                                                       
##  [4] "01_module4/img/PSU-logo.png"                                                         
##  [5] "01_module4/rmarkdown_primer.html"                                                    
##  [6] "01_module4/rmarkdown_primer.pdf"                                                     
##  [7] "01_module4/rmarkdown_primer.Rmd"                                                     
##  [8] "01_module4/rsconnect/documents/Testpublish.Rmd/rpubs.com/rpubs/Document.dcf"         
##  [9] "01_module4/Scaramuzzarmarkdown_primer.Rmd"                                           
## [10] "01_module4/Testpublish.html"                                                         
## [11] "01_module4/Testpublish.Rmd"                                                          
## [12] "02_module5/Apply4.html"                                                              
## [13] "02_module5/Apply4.Rmd"                                                               
## [14] "02_module5/CodeAlong4_shell.html"                                                    
## [15] "02_module5/CodeAlong4_shell.Rmd"                                                     
## [16] "02_module5/rsconnect/documents/Apply4.Rmd/rpubs.com/rpubs/Document.dcf"              
## [17] "02_module5/rsconnect/documents/CodeAlong4_shell.Rmd/rpubs.com/rpubs/Document.dcf"    
## [18] "03_module6/Apply5.html"                                                              
## [19] "03_module6/Apply5.Rmd"                                                               
## [20] "03_module6/CodeAlong5_Ch4_shell.html"                                                
## [21] "03_module6/CodeAlong5_Ch4_shell.Rmd"                                                 
## [22] "03_module6/CodeAlong5_Ch5_shell.html"                                                
## [23] "03_module6/CodeAlong5_Ch5_shell.Rmd"                                                 
## [24] "03_module6/rsconnect/documents/CodeAlong5_Ch5_shell.Rmd/rpubs.com/rpubs/Document.dcf"
## [25] "PSU_DAT3000_IntroToDA.Rproj"

Apply the following dplyr verbs to your data

Filter rows

filter(Roundabouts, year_completed > 2000)
## # A tibble: 10,916 × 18
##    name    address town_city county_area state_region country    lat  long type 
##    <chr>   <chr>   <chr>     <chr>       <chr>        <chr>    <dbl> <dbl> <chr>
##  1 Capito… Spring… Springfi… Sangamon C… IL           United…  -89.6  39.8 Roun…
##  2 Ridgew… Clearw… Clearwat… Pinellas C… FL           United…  -82.8  28.0 Traf…
##  3 Darien… Twinsb… Twinsburg Summit Co.  OH           United…  -81.4  41.3 Traf…
##  4 Old Mi… Wattsv… Wattsvil… Accomack C… VA           United…  -75.5  38.0 Traf…
##  5 NH 10 … Hanove… Hanover   Grafton Co. NH           United…  -72.3  43.7 Roun…
##  6 Guilfo… Baltim… Baltimore Baltimore … MD           United…  -76.6  39.3 Roun…
##  7 Pulask… Buffal… Buffalo   Wright Co.  MN           United…  -93.8  45.2 Other
##  8 6th St… West F… West Far… Cass Co.    ND           United…  -96.9  46.9 Traf…
##  9 S 34th… Grand … Grand Fo… Grand Fork… ND           United…  -97.1  47.9 Roun…
## 10 W Dimo… Anchor… Anchorage Anchorage   AK           United… -150.   61.1 Roun…
## # ℹ 10,906 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>
filter(Roundabouts, year_completed < 2000)
## # A tibble: 16,806 × 18
##    name    address town_city county_area state_region country    lat  long type 
##    <chr>   <chr>   <chr>     <chr>       <chr>        <chr>    <dbl> <dbl> <chr>
##  1 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  2 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  3 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  4 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  5 Bagley… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  6 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  7 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  8 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  9 Kendal… Madiso… Madison   Dane Co.    WI           United…  -89.4  43.1 Traf…
## 10 Bagley… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
## # ℹ 16,796 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>
filter(Roundabouts, approaches >= 3)
## # A tibble: 27,278 × 18
##    name    address town_city county_area state_region country    lat  long type 
##    <chr>   <chr>   <chr>     <chr>       <chr>        <chr>    <dbl> <dbl> <chr>
##  1 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  2 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  3 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  4 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  5 Kendal… Madiso… Madison   Dane Co.    WI           United…  -89.4  43.1 Traf…
##  6 Capito… Spring… Springfi… Sangamon C… IL           United…  -89.6  39.8 Roun…
##  7 Darien… Twinsb… Twinsburg Summit Co.  OH           United…  -81.4  41.3 Traf…
##  8 Westbu… Huntsv… Huntsvil… Madison Co. AL           United…  -86.6  34.7 Traf…
##  9 <![CDA… Huntsv… Huntsvil… Madison Co. AL           United…  -86.6  34.7 Traf…
## 10 Old Mi… Wattsv… Wattsvil… Accomack C… VA           United…  -75.5  38.0 Traf…
## # ℹ 27,268 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>
filter(Roundabouts, year_completed > 2000, approaches >= 3)
## # A tibble: 10,730 × 18
##    name    address town_city county_area state_region country    lat  long type 
##    <chr>   <chr>   <chr>     <chr>       <chr>        <chr>    <dbl> <dbl> <chr>
##  1 Capito… Spring… Springfi… Sangamon C… IL           United…  -89.6  39.8 Roun…
##  2 Darien… Twinsb… Twinsburg Summit Co.  OH           United…  -81.4  41.3 Traf…
##  3 Old Mi… Wattsv… Wattsvil… Accomack C… VA           United…  -75.5  38.0 Traf…
##  4 Guilfo… Baltim… Baltimore Baltimore … MD           United…  -76.6  39.3 Roun…
##  5 Pulask… Buffal… Buffalo   Wright Co.  MN           United…  -93.8  45.2 Other
##  6 6th St… West F… West Far… Cass Co.    ND           United…  -96.9  46.9 Traf…
##  7 S 34th… Grand … Grand Fo… Grand Fork… ND           United…  -97.1  47.9 Roun…
##  8 W Dimo… Anchor… Anchorage Anchorage   AK           United… -150.   61.1 Roun…
##  9 D St. … Fairba… Fairbanks Fairbanks … AK           United… -148.   64.9 Traf…
## 10 D St. … Fairba… Fairbanks Fairbanks … AK           United… -148.   64.9 Traf…
## # ℹ 10,720 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>

Arrange rows

arrange(Roundabouts, year_completed)
## # A tibble: 27,887 × 18
##    name    address town_city county_area state_region country    lat  long type 
##    <chr>   <chr>   <chr>     <chr>       <chr>        <chr>    <dbl> <dbl> <chr>
##  1 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  2 Arapah… Boulde… Boulder   Boulder Co. CO           United… -105.   40.0 Traf…
##  3 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  4 10th A… Belmar… Belmar    Monmouth C… NJ           United…  -74.0  40.2 Traf…
##  5 Bagley… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  6 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  7 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  8 8th Av… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
##  9 Bagley… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
## 10 Meridi… Seattl… Seattle   King Co.    WA           United… -122.   47.7 Traf…
## # ℹ 27,877 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>
arrange(Roundabouts, desc(year_completed))
## # A tibble: 27,887 × 18
##    name    address town_city county_area state_region country    lat  long type 
##    <chr>   <chr>   <chr>     <chr>       <chr>        <chr>    <dbl> <dbl> <chr>
##  1 E 106t… Carmel… Carmel    Hamilton C… IN           United…  -86.1  39.9 Roun…
##  2 S 68th… Hickma… Hickman   Lancaster … NE           United…  -96.6  40.6 Roun…
##  3 Stonew… South … South Fu… Fulton Co.  GA           United…  -84.6  33.7 Roun…
##  4 Stonew… South … South Fu… Fulton Co.  GA           United…  -84.6  33.7 Roun…
##  5 Hunt R… Blue A… Blue Ash  Hamilton C… OH           United…  -84.4  39.2 Roun…
##  6 Rockli… Rockli… Rocklin   Placer Co.  CA           United… -121.   38.8 Roun…
##  7 Chapel… New Ha… New Haven New Haven … CT           United…  -73.0  41.3 Roun…
##  8 Front … Panama… Panama C… Bay Co.     FL           United…  -85.9  30.2 Roun…
##  9 Mullan… Missou… Missoula  Missoula C… MT           United… -114.   46.9 Roun…
## 10 W 171s… Westfi… Westfield Hamilton C… IN           United…  -86.2  40.0 Roun…
## # ℹ 27,877 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>
arrange(Roundabouts, approaches)
## # A tibble: 27,887 × 18
##    name     address town_city county_area state_region country   lat  long type 
##    <chr>    <chr>   <chr>     <chr>       <chr>        <chr>   <dbl> <dbl> <chr>
##  1 Bagley … Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  2 8th Ave… Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  3 8th Ave… Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  4 8th Ave… Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  5 Bagley … Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  6 Meridia… Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  7 Burke A… Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  8 Corliss… Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
##  9 Meridia… Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
## 10 Bagley … Seattl… Seattle   King Co.    WA           United… -122.  47.7 Traf…
## # ℹ 27,877 more rows
## # ℹ 9 more variables: status <chr>, year_completed <dbl>, approaches <dbl>,
## #   driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>

Select columns

select(Roundabouts, year_completed, approaches)
## # A tibble: 27,887 × 2
##    year_completed approaches
##             <dbl>      <dbl>
##  1              0          3
##  2              0          3
##  3              0          4
##  4              0          4
##  5              0          0
##  6              0          0
##  7              0          0
##  8              0          0
##  9           1997          4
## 10              0          0
## # ℹ 27,877 more rows
select(Roundabouts, year_completed:approaches)
## # A tibble: 27,887 × 2
##    year_completed approaches
##             <dbl>      <dbl>
##  1              0          3
##  2              0          3
##  3              0          4
##  4              0          4
##  5              0          0
##  6              0          0
##  7              0          0
##  8              0          0
##  9           1997          4
## 10              0          0
## # ℹ 27,877 more rows
select(Roundabouts, contains("year_completed"))
## # A tibble: 27,887 × 1
##    year_completed
##             <dbl>
##  1              0
##  2              0
##  3              0
##  4              0
##  5              0
##  6              0
##  7              0
##  8              0
##  9           1997
## 10              0
## # ℹ 27,877 more rows
select(Roundabouts, contains("approach"))
## # A tibble: 27,887 × 1
##    approaches
##         <dbl>
##  1          3
##  2          3
##  3          4
##  4          4
##  5          0
##  6          0
##  7          0
##  8          0
##  9          4
## 10          0
## # ℹ 27,877 more rows
select(Roundabouts, year_completed, approaches, everything())
## # A tibble: 27,887 × 18
##    year_completed approaches name     address town_city county_area state_region
##             <dbl>      <dbl> <chr>    <chr>   <chr>     <chr>       <chr>       
##  1              0          3 Arapaho… Boulde… Boulder   Boulder Co. CO          
##  2              0          3 Arapaho… Boulde… Boulder   Boulder Co. CO          
##  3              0          4 10th Av… Belmar… Belmar    Monmouth C… NJ          
##  4              0          4 10th Av… Belmar… Belmar    Monmouth C… NJ          
##  5              0          0 Bagley … Seattl… Seattle   King Co.    WA          
##  6              0          0 8th Ave… Seattl… Seattle   King Co.    WA          
##  7              0          0 8th Ave… Seattl… Seattle   King Co.    WA          
##  8              0          0 8th Ave… Seattl… Seattle   King Co.    WA          
##  9           1997          4 Kendall… Madiso… Madison   Dane Co.    WI          
## 10              0          0 Bagley … Seattl… Seattle   King Co.    WA          
## # ℹ 27,877 more rows
## # ℹ 11 more variables: country <chr>, lat <dbl>, long <dbl>, type <chr>,
## #   status <chr>, driveways <dbl>, lane_type <chr>, functional_class <chr>,
## #   control_type <chr>, other_control_type <chr>, previous_control_type <chr>

Add columns

mutate(Roundabouts,
       Time_Period = ifelse(year_completed < 2000, "Before 2000", "2000 or Later")) %>%
    
    select(year_completed, Time_Period)
## # A tibble: 27,887 × 2
##    year_completed Time_Period
##             <dbl> <chr>      
##  1              0 Before 2000
##  2              0 Before 2000
##  3              0 Before 2000
##  4              0 Before 2000
##  5              0 Before 2000
##  6              0 Before 2000
##  7              0 Before 2000
##  8              0 Before 2000
##  9           1997 Before 2000
## 10              0 Before 2000
## # ℹ 27,877 more rows
mutate(Roundabouts,
       approach_Group = ifelse(approaches < 3, "Fewer than 3", "3 or More")) %>%
    
    select(approaches, approach_Group)
## # A tibble: 27,887 × 2
##    approaches approach_Group
##         <dbl> <chr>         
##  1          3 3 or More     
##  2          3 3 or More     
##  3          4 3 or More     
##  4          4 3 or More     
##  5          0 Fewer than 3  
##  6          0 Fewer than 3  
##  7          0 Fewer than 3  
##  8          0 Fewer than 3  
##  9          4 3 or More     
## 10          0 Fewer than 3  
## # ℹ 27,877 more rows

Summarize by groups

summarize(Roundabouts,
          average_Approaches = mean(approaches, na.rm = TRUE))
## # A tibble: 1 × 1
##   average_Approaches
##                <dbl>
## 1               3.68
Roundabouts %>%
    
    group_by(year_completed) %>%
    
    summarize(
      Average_approaches = mean(approaches, na.rm = TRUE),
      Count = n()
    ) %>%
    
    arrange(year_completed)
## # A tibble: 77 × 3
##    year_completed Average_approaches Count
##             <dbl>              <dbl> <int>
##  1              0               3.70 16367
##  2           1791               4        1
##  3           1807               8        1
##  4           1815               4        1
##  5           1820               4        1
##  6           1876               4        1
##  7           1890               4        1
##  8           1900               4        1
##  9           1905               5        1
## 10           1909               4        1
## # ℹ 67 more rows
Roundabouts %>%

    group_by(approaches) %>%

    summarize(Count = n()) %>%

    arrange(approaches)
## # A tibble: 12 × 2
##    approaches Count
##         <dbl> <int>
##  1          0   133
##  2          1    19
##  3          2   457
##  4          3  8894
##  5          4 17118
##  6          5  1031
##  7          6   190
##  8          7    23
##  9          8    18
## 10         10     2
## 11         11     1
## 12         12     1