This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate summarizing statistics for hflights | |
summarise(hflights, n_obs = n(), n_carrier = n_distinct(UniqueCarrier), | |
n_dest = n_distinct(Dest), dest100 = nth(Dest, 100)) | |
# Filter hflights to keep all American Airline flights: aa | |
aa <- filter(hflights, UniqueCarrier == "American") | |
# Generate summarizing statistics for aa | |
summarise(aa, n_flights = n(), n_canc = sum(Cancelled), | |
p_canc = 100*(n_canc/n_flights), avg_delay = mean(ArrDelay, na.rm = TRUE)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select(arrange(filter(hflights, DepDelay > 360), TaxiIn + TaxiOut), TailNum) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %>% OPERATOR ---------------------------------------------------------------------- | |
# with %>% operator | |
hflights %>% | |
mutate(diff = TaxiOut - TaxiIn) %>% | |
filter(!is.na(diff)) %>% | |
summarise(avg = mean(diff)) | |
# without %>% operator | |
# arguments get further and further apart |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# group_by() ------------------------------------------------------------------------- | |
# Generate a per-carrier summary of hflights with the following variables: n_flights, | |
# the number of flights flown by the carrier; n_canc, the number of cancelled flights; | |
# p_canc, the percentage of cancelled flights; avg_delay, the average arrival delay of | |
# flights whose delay does not equal NA. Next, order the carriers in the summary from | |
# low to high by their average arrival delay. Use percentage of flights cancelled to | |
# break any ties. Which airline scores best based on these statistics? | |
hflights %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Combine group_by with mutate----- | |
# First, discard flights whose arrival delay equals NA. Next, create a by-carrier | |
# summary with a single variable: p_delay, the proportion of flights which are | |
# delayed at arrival. Next, create a new variable rank in the summary which is a | |
# rank according to p_delay. Finally, arrange the observations by this new rank | |
hflights %>% | |
filter(!is.na(ArrDelay)) %>% | |
group_by(UniqueCarrier) %>% | |
summarise(p_delay = sum(ArrDelay >0)/n()) %>% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## if fxregime or strucchange package is absent from installed packages, download it and load it | |
if(!require('fxregime')){ | |
install.packages("fxregime") | |
} | |
if(!require('strucchange')){ | |
install.packages("strucchange") | |
} | |
## load packages | |
library("fxregime") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## if fxregime is absent from installed packages, download it and load it | |
if(!require('fxregime')){ | |
install.packages("fxregime") | |
} | |
## load package | |
library("fxregime") | |
# load the necessary data related to exchange rates - 'FXRatesCHF' | |
# this dataset treats CHF as unit currency | |
# install / load Quandl |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def quicksort(x): | |
if len(x) == 1 or len(x) == 0: | |
return x | |
else: | |
pivot = x[0] | |
i = 0 | |
for j in range(len(x)-1): | |
if x[j+1] < pivot: | |
x[j+1],x[i+1] = x[i+1], x[j+1] | |
i += 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2148 | |
9058 | |
7742 | |
3153 | |
6324 | |
609 | |
7628 | |
5469 | |
7017 | |
504 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env | |
# Case I | |
# First element of the unsorted array is chosen as pivot element for sorting using Quick Sort | |
def countComparisonsWithFirst(x): | |
""" Counts number of comparisons while using Quick Sort with first element of unsorted array as pivot """ | |
global count_pivot_first | |
if len(x) == 1 or len(x) == 0: |