Skip to content

Instantly share code, notes, and snippets.

View anirudhjayaraman's full-sized avatar
🏠
Working from home

Anirudh Jayaraman anirudhjayaraman

🏠
Working from home
View GitHub Profile
# 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))
select(arrange(filter(hflights, DepDelay > 360), TaxiIn + TaxiOut), TailNum)
@anirudhjayaraman
anirudhjayaraman / pipingOperator.r
Created December 19, 2015 07:47
Piping Operator %>% in dplyr
# %>% OPERATOR ----------------------------------------------------------------------
# with %>% operator
hflights %>%
mutate(diff = TaxiOut - TaxiIn) %>%
filter(!is.na(diff)) %>%
summarise(avg = mean(diff))
# without %>% operator
# arguments get further and further apart
@anirudhjayaraman
anirudhjayaraman / group_by.R
Created December 19, 2015 09:34
group_by illustrative examples
# 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 %>%
# 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()) %>%
@anirudhjayaraman
anirudhjayaraman / FrankelWei_2005_2010.r
Last active May 15, 2016 20:12
Auto-detecting Structural Breaks in China’s FX Regime
## 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")
@anirudhjayaraman
anirudhjayaraman / FrankelWei_2010_2016.r
Created May 15, 2016 20:26
Frankel Wei Regression for 2010-2016
## 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
@anirudhjayaraman
anirudhjayaraman / quicksort.py
Last active June 19, 2024 23:58
Python code for the Quick Sort Algorithm
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
@anirudhjayaraman
anirudhjayaraman / QuickSort_List.txt
Created July 12, 2016 23:11
Integer list to sort using Quick Sort (Exercise)
2148
9058
7742
3153
6324
609
7628
5469
7017
504
@anirudhjayaraman
anirudhjayaraman / countComparisons.py
Created July 13, 2016 17:55
Computing Work Done (Total Comparisons) by Quick Sort
#!/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: