0% found this document useful (0 votes)
14 views3 pages

Lecture 9 Exercises

statistics coding example

Uploaded by

Hamzah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Lecture 9 Exercises

statistics coding example

Uploaded by

Hamzah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

9/23/24, 5:50 PM Lecture 9 Exercises

Lecture 9 Exercises
AUTHOR
Hamzah Syed

Exercise 1

library(tidyverse)

── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──


✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.1 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become
errors

library(nycflights13)

flights |>
filter(arr_delay >= 120, carrier %in% c("UA", "AA", "DL"))

# A tibble: 3,280 × 19
year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
<int> <int> <int> <int> <int> <dbl> <int> <int>
1 2013 1 1 957 733 144 1056 853
2 2013 1 1 1114 900 134 1447 1222
3 2013 1 1 1856 1645 131 2212 2005
4 2013 1 1 2205 1720 285 46 2040
5 2013 1 2 833 558 155 1018 727
6 2013 1 2 1412 838 334 1710 1147
7 2013 1 2 1451 1232 139 1749 1533
8 2013 1 2 1607 1030 337 2003 1355
9 2013 1 2 1751 1450 181 2041 1755
10 2013 1 2 2131 1512 379 2340 1741
# ℹ 3,270 more rows
# ℹ 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
# tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
# hour <dbl>, minute <dbl>, time_hour <dttm>

Exercise 2

https://01752a68c867448895c88daeafbaff29.app.posit.cloud/p/32d95e56/ 1/3
9/23/24, 5:50 PM Lecture 9 Exercises

# Select columns and filter for UA flights


flights |>
select(year:day, carrier, contains("dep")) |>
filter(carrier == "UA")

# A tibble: 58,665 × 7
year month day carrier dep_time sched_dep_time dep_delay
<int> <int> <int> <chr> <int> <int> <dbl>
1 2013 1 1 UA 517 515 2
2 2013 1 1 UA 533 529 4
3 2013 1 1 UA 554 558 -4
4 2013 1 1 UA 558 600 -2
5 2013 1 1 UA 558 600 -2
6 2013 1 1 UA 559 600 -1
7 2013 1 1 UA 607 607 0
8 2013 1 1 UA 611 600 11
9 2013 1 1 UA 623 627 -4
10 2013 1 1 UA 628 630 -2
# ℹ 58,655 more rows

# Select columns, filter for UA flights, and arrange by dep_delay in descending order
flights |>
select(year:day, carrier, contains("dep")) |>
filter(carrier == "UA") |>
arrange(desc(dep_delay))

# A tibble: 58,665 × 7
year month day carrier dep_time sched_dep_time dep_delay
<int> <int> <int> <chr> <int> <int> <dbl>
1 2013 7 26 UA 2345 1542 483
2 2013 4 18 UA 2200 1453 427
3 2013 8 28 UA 2356 1652 424
4 2013 9 26 UA 2211 1509 422
5 2013 6 25 UA 1600 900 420
6 2013 6 7 UA 2352 1659 413
7 2013 3 24 UA 2253 1605 408
8 2013 3 14 UA 1346 700 406
9 2013 5 22 UA 146 1900 406
10 2013 6 27 UA 2348 1703 405
# ℹ 58,655 more rows

Exercise 3

# Find how many flights have missing values for dep_time


missing_dep_time <- flights |>
filter(is.na(dep_time))

https://01752a68c867448895c88daeafbaff29.app.posit.cloud/p/32d95e56/ 2/3
9/23/24, 5:50 PM Lecture 9 Exercises

# Count the number of rows with missing dep_time


nrow(missing_dep_time)

[1] 8255

# Find which other variables have missing values in the dataset


colSums(is.na(flights))

year month day dep_time sched_dep_time


0 0 0 8255 0
dep_delay arr_time sched_arr_time arr_delay carrier
8255 8713 0 9430 0
flight tailnum origin dest air_time
0 2512 0 0 9430
distance hour minute time_hour
0 0 0 0

https://01752a68c867448895c88daeafbaff29.app.posit.cloud/p/32d95e56/ 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy