Import your data

# excel file
data <- read_excel("../00_data/MyData.xlsx") %>%
    rename(X = institution_name)

stressors <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-01-11/stressor.csv')
## Rows: 7332 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): months, state, stressor
## dbl (2): year, stress_pct
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
data %>% skimr::skim()
Data summary
Name Piped data
Number of rows 132327
Number of columns 28
_______________________
Column type frequency:
character 17
numeric 11
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
X 0 1 6 65 0 2212 0
city_txt 0 1 2 23 0 1377 0
state_cd 0 1 2 2 0 54 0
classification_name 0 1 4 34 0 19 0
classification_other 0 1 2 50 0 111 0
sector_name 0 1 2 35 0 7 0
partic_men 0 1 1 3 0 218 0
partic_women 0 1 1 3 0 205 0
partic_coed_men 0 1 1 3 0 52 0
partic_coed_women 0 1 1 2 0 67 0
rev_men 0 1 2 9 0 57614 0
rev_women 0 1 1 8 0 63129 0
total_rev_menwomen 0 1 2 9 0 81360 0
exp_men 0 1 2 8 0 57838 0
exp_women 0 1 2 7 0 63450 0
total_exp_menwomen 0 1 2 8 0 81686 0
sports 0 1 4 26 0 38 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
year 0 1 2017.68 1.49 2015 2016 2018 2019 2019 ▂▂▂▂▇
unitid 0 1 184360.44 56064.63 100654 149781 181738 214069 800001 ▇▁▁▁▁
zip_text 45 1 42004919.00 158605462.14 604 23284 49104 79549 997757500 ▇▁▁▁▁
classification_code 0 1 7.78 4.89 1 4 6 12 20 ▇▇▅▂▂
ef_male_count 0 1 2126.25 3014.38 0 513 986 2385 35954 ▇▁▁▁▁
ef_female_count 0 1 2496.21 3276.05 0 652 1248 2860 30325 ▇▁▁▁▁
ef_total_count 0 1 4622.46 6228.14 0 1194 2259 5237 66279 ▇▁▁▁▁
sector_cd 0 1 2.21 2.10 1 1 2 2 99 ▇▁▁▁▁
sportscode 0 1 16.34 10.57 1 7 16 25 38 ▇▅▆▅▃
sum_partic_men 0 1 14.49 25.30 0 0 0 20 331 ▇▁▁▁▁
sum_partic_women 0 1 10.86 17.15 0 0 6 17 327 ▇▁▁▁▁

##Make data smaller

set.seed(123)
data_smaller2 <- data %>%
    select(year, state_cd, city_txt, sports) %>%
    sample_n(10)

set.seed(123)
stressors_small <- stressors %>%
    select(year, months, state, stressor) %>%
    sample_n(10)

data_smaller2
## # A tibble: 10 × 4
##     year state_cd city_txt    sports                    
##    <dbl> <chr>    <chr>       <chr>                     
##  1  2019 TX       El Paso     Beach Volleyball          
##  2  2019 NM       Albuquerque Baseball                  
##  3  2019 TX       Weatherford Track and Field, X-Country
##  4  2017 NY       Syracuse    Basketball                
##  5  2018 OR       Roseburg    Volleyball                
##  6  2017 OH       Cincinnati  Golf                      
##  7  2018 MA       Boston      Volleyball                
##  8  2019 MN       Saint Paul  All Track Combined        
##  9  2015 TX       Arlington   All Track Combined        
## 10  2019 KS       Winfield    Football

Chapter 13

What are primary keys in your data?

X

Can you divide your data into two?

Divide it using dplyr::select in a way the two have a common variable, which you could use to join the two.

data_smaller2 %>% inner_join(stressors_small)
## Joining with `by = join_by(year)`
## Warning in inner_join(., stressors_small): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 4 of `x` matches multiple rows in `y`.
## ℹ Row 10 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
## # A tibble: 16 × 7
##     year state_cd city_txt    sports                     months   state stressor
##    <dbl> <chr>    <chr>       <chr>                      <chr>    <chr> <chr>   
##  1  2019 TX       El Paso     Beach Volleyball           July-Se… Othe… Disesas…
##  2  2019 NM       Albuquerque Baseball                   July-Se… Othe… Disesas…
##  3  2019 TX       Weatherford Track and Field, X-Country July-Se… Othe… Disesas…
##  4  2017 NY       Syracuse    Basketball                 January… Sout… Disesas…
##  5  2017 NY       Syracuse    Basketball                 January… West… Disesas…
##  6  2017 NY       Syracuse    Basketball                 July-Se… New … Pestici…
##  7  2017 NY       Syracuse    Basketball                 October… Wyom… Other   
##  8  2018 OR       Roseburg    Volleyball                 October… Illi… Varroa …
##  9  2017 OH       Cincinnati  Golf                       January… Sout… Disesas…
## 10  2017 OH       Cincinnati  Golf                       January… West… Disesas…
## 11  2017 OH       Cincinnati  Golf                       July-Se… New … Pestici…
## 12  2017 OH       Cincinnati  Golf                       October… Wyom… Other   
## 13  2018 MA       Boston      Volleyball                 October… Illi… Varroa …
## 14  2019 MN       Saint Paul  All Track Combined         July-Se… Othe… Disesas…
## 15  2015 TX       Arlington   All Track Combined         April-J… Virg… Pestici…
## 16  2019 KS       Winfield    Football                   July-Se… Othe… Disesas…
## Joining with `by = join_by(year)`

stressors_small %>% inner_join(data_smaller2)
## Joining with `by = join_by(year)`
## Warning in inner_join(., data_smaller2): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 1 of `x` matches multiple rows in `y`.
## ℹ Row 4 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
## # A tibble: 16 × 7
##     year months           state          stressor     state_cd city_txt   sports
##    <dbl> <chr>            <chr>          <chr>        <chr>    <chr>      <chr> 
##  1  2017 January-March    South Carolina Disesases    NY       Syracuse   Baske…
##  2  2017 January-March    South Carolina Disesases    OH       Cincinnati Golf  
##  3  2017 January-March    West Virginia  Disesases    NY       Syracuse   Baske…
##  4  2017 January-March    West Virginia  Disesases    OH       Cincinnati Golf  
##  5  2015 April-June       Virginia       Pesticides   TX       Arlington  All T…
##  6  2018 October-December Illinois       Varroa mites OR       Roseburg   Volle…
##  7  2018 October-December Illinois       Varroa mites MA       Boston     Volle…
##  8  2017 July-September   New York       Pesticides   NY       Syracuse   Baske…
##  9  2017 July-September   New York       Pesticides   OH       Cincinnati Golf  
## 10  2017 October-December Wyoming        Other        NY       Syracuse   Baske…
## 11  2017 October-December Wyoming        Other        OH       Cincinnati Golf  
## 12  2019 July-September   Other States   Disesases    TX       El Paso    Beach…
## 13  2019 July-September   Other States   Disesases    NM       Albuquerq… Baseb…
## 14  2019 July-September   Other States   Disesases    TX       Weatherfo… Track…
## 15  2019 July-September   Other States   Disesases    MN       Saint Paul All T…
## 16  2019 July-September   Other States   Disesases    KS       Winfield   Footb…
## Joining with `by = join_by(year)`

Can you join the two together?

Use tidyr::left_join or other joining functions.

data_smaller2 %>% left_join(stressors_small)
## Joining with `by = join_by(year)`
## Warning in left_join(., stressors_small): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 4 of `x` matches multiple rows in `y`.
## ℹ Row 10 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
## # A tibble: 16 × 7
##     year state_cd city_txt    sports                     months   state stressor
##    <dbl> <chr>    <chr>       <chr>                      <chr>    <chr> <chr>   
##  1  2019 TX       El Paso     Beach Volleyball           July-Se… Othe… Disesas…
##  2  2019 NM       Albuquerque Baseball                   July-Se… Othe… Disesas…
##  3  2019 TX       Weatherford Track and Field, X-Country July-Se… Othe… Disesas…
##  4  2017 NY       Syracuse    Basketball                 January… Sout… Disesas…
##  5  2017 NY       Syracuse    Basketball                 January… West… Disesas…
##  6  2017 NY       Syracuse    Basketball                 July-Se… New … Pestici…
##  7  2017 NY       Syracuse    Basketball                 October… Wyom… Other   
##  8  2018 OR       Roseburg    Volleyball                 October… Illi… Varroa …
##  9  2017 OH       Cincinnati  Golf                       January… Sout… Disesas…
## 10  2017 OH       Cincinnati  Golf                       January… West… Disesas…
## 11  2017 OH       Cincinnati  Golf                       July-Se… New … Pestici…
## 12  2017 OH       Cincinnati  Golf                       October… Wyom… Other   
## 13  2018 MA       Boston      Volleyball                 October… Illi… Varroa …
## 14  2019 MN       Saint Paul  All Track Combined         July-Se… Othe… Disesas…
## 15  2015 TX       Arlington   All Track Combined         April-J… Virg… Pestici…
## 16  2019 KS       Winfield    Football                   July-Se… Othe… Disesas…
## Joining with `by = join_by(year)`

Right join

data_smaller2 %>% right_join(stressors_small)
## Joining with `by = join_by(year)`
## Warning in right_join(., stressors_small): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 4 of `x` matches multiple rows in `y`.
## ℹ Row 10 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
## # A tibble: 19 × 7
##     year state_cd city_txt    sports                     months   state stressor
##    <dbl> <chr>    <chr>       <chr>                      <chr>    <chr> <chr>   
##  1  2019 TX       El Paso     Beach Volleyball           July-Se… Othe… Disesas…
##  2  2019 NM       Albuquerque Baseball                   July-Se… Othe… Disesas…
##  3  2019 TX       Weatherford Track and Field, X-Country July-Se… Othe… Disesas…
##  4  2017 NY       Syracuse    Basketball                 January… Sout… Disesas…
##  5  2017 NY       Syracuse    Basketball                 January… West… Disesas…
##  6  2017 NY       Syracuse    Basketball                 July-Se… New … Pestici…
##  7  2017 NY       Syracuse    Basketball                 October… Wyom… Other   
##  8  2018 OR       Roseburg    Volleyball                 October… Illi… Varroa …
##  9  2017 OH       Cincinnati  Golf                       January… Sout… Disesas…
## 10  2017 OH       Cincinnati  Golf                       January… West… Disesas…
## 11  2017 OH       Cincinnati  Golf                       July-Se… New … Pestici…
## 12  2017 OH       Cincinnati  Golf                       October… Wyom… Other   
## 13  2018 MA       Boston      Volleyball                 October… Illi… Varroa …
## 14  2019 MN       Saint Paul  All Track Combined         July-Se… Othe… Disesas…
## 15  2015 TX       Arlington   All Track Combined         April-J… Virg… Pestici…
## 16  2019 KS       Winfield    Football                   July-Se… Othe… Disesas…
## 17  2016 <NA>     <NA>        <NA>                       October… West… Varroa …
## 18  2016 <NA>     <NA>        <NA>                       July-Se… Nebr… Unknown 
## 19  2016 <NA>     <NA>        <NA>                       January… Arka… Other p…
## Joining with `by = join_by(year)`

Full join

data_smaller2 %>% full_join(stressors_small)
## Joining with `by = join_by(year)`
## Warning in full_join(., stressors_small): Detected an unexpected many-to-many relationship between `x` and `y`.
## ℹ Row 4 of `x` matches multiple rows in `y`.
## ℹ Row 10 of `y` matches multiple rows in `x`.
## ℹ If a many-to-many relationship is expected, set `relationship =
##   "many-to-many"` to silence this warning.
## # A tibble: 19 × 7
##     year state_cd city_txt    sports                     months   state stressor
##    <dbl> <chr>    <chr>       <chr>                      <chr>    <chr> <chr>   
##  1  2019 TX       El Paso     Beach Volleyball           July-Se… Othe… Disesas…
##  2  2019 NM       Albuquerque Baseball                   July-Se… Othe… Disesas…
##  3  2019 TX       Weatherford Track and Field, X-Country July-Se… Othe… Disesas…
##  4  2017 NY       Syracuse    Basketball                 January… Sout… Disesas…
##  5  2017 NY       Syracuse    Basketball                 January… West… Disesas…
##  6  2017 NY       Syracuse    Basketball                 July-Se… New … Pestici…
##  7  2017 NY       Syracuse    Basketball                 October… Wyom… Other   
##  8  2018 OR       Roseburg    Volleyball                 October… Illi… Varroa …
##  9  2017 OH       Cincinnati  Golf                       January… Sout… Disesas…
## 10  2017 OH       Cincinnati  Golf                       January… West… Disesas…
## 11  2017 OH       Cincinnati  Golf                       July-Se… New … Pestici…
## 12  2017 OH       Cincinnati  Golf                       October… Wyom… Other   
## 13  2018 MA       Boston      Volleyball                 October… Illi… Varroa …
## 14  2019 MN       Saint Paul  All Track Combined         July-Se… Othe… Disesas…
## 15  2015 TX       Arlington   All Track Combined         April-J… Virg… Pestici…
## 16  2019 KS       Winfield    Football                   July-Se… Othe… Disesas…
## 17  2016 <NA>     <NA>        <NA>                       October… West… Varroa …
## 18  2016 <NA>     <NA>        <NA>                       July-Se… Nebr… Unknown 
## 19  2016 <NA>     <NA>        <NA>                       January… Arka… Other p…
## Joining with `by = join_by(year)`

Semi join

stressors_small %>% semi_join(data_smaller2)
## Joining with `by = join_by(year)`
## # A tibble: 7 × 4
##    year months           state          stressor    
##   <dbl> <chr>            <chr>          <chr>       
## 1  2017 January-March    South Carolina Disesases   
## 2  2017 January-March    West Virginia  Disesases   
## 3  2015 April-June       Virginia       Pesticides  
## 4  2018 October-December Illinois       Varroa mites
## 5  2017 July-September   New York       Pesticides  
## 6  2017 October-December Wyoming        Other       
## 7  2019 July-September   Other States   Disesases
## Joining with `by = join_by(year)`

Anti join - filtering

data_smaller2 %>% anti_join(stressors_small)
## Joining with `by = join_by(year)`
## # A tibble: 0 × 4
## # ℹ 4 variables: year <dbl>, state_cd <chr>, city_txt <chr>, sports <chr>
## Joining with `by = join_by(year)`