Introduction to dplyr

Code and Text for Quiz 3.

Load packages we need

read data into R

corp_tax <- read_excel(here("corp_tax.xlsx"))

Let’s look at NVR in the corp_tax tibble.

result <- corp_tax %>% 
  filter(company == "NVR")
result
# A tibble: 1 x 5
  company profit   tax tax_rate industry                  
  <chr>    <dbl> <dbl>    <dbl> <chr>                     
1 NVR       923.  126.    0.137 Engineering & construction

NVR is in the Engineering & construction industry. It had a profit of $922.694 million and tax of $126.358 million. Its tax rate was 13.7%


Let’s look at Occidental Petroleum in the corp_tax tibble.

result <- corp_tax %>% 
  filter(company == "Occidental Petroleum")
result
# A tibble: 1 x 5
  company              profit   tax tax_rate industry            
  <chr>                 <dbl> <dbl>    <dbl> <chr>               
1 Occidental Petroleum   3379   -23 -0.00681 Oil, gas & pipelines

Occidental Petroleum is in the Oil, gas & pipelines industry. It had a profit of $3379 million and tax of $-23 million. Its tax rate was -0.7%


Let’s find the company in the Utilities, gas and electric industry with the highest profit.

result <- corp_tax %>% 
  filter(industry == 'Utilities, gas and electric') %>% 
  slice_max(profit, n = 1)
result
# A tibble: 1 x 5
  company        profit   tax tax_rate industry                   
  <chr>           <dbl> <dbl>    <dbl> <chr>                      
1 NextEra Energy   7289    30  0.00412 Utilities, gas and electric

NextEra Energyis the company in Utilities, gas and electric industry with the highest profit. It had a profit of $7289 million and tax of $30 million. Its tax rate was 0.4%


Let’s find the company in the Motor vehicles and parts industry with the highest profit.

result <- corp_tax %>% 
  filter(industry == 'Motor vehicles and parts') %>% 
  slice_max(profit, n = 1)
result
# A tibble: 1 x 5
  company        profit   tax tax_rate industry                
  <chr>           <dbl> <dbl>    <dbl> <chr>                   
1 General Motors   4320  -104  -0.0241 Motor vehicles and parts

General Motorsis the company in Motor vehicles and parts industry with the highest profit. It had a profit of $4320 million and tax of $-104 million. Its tax rate was -2.4%