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
@anirudhjayaraman
anirudhjayaraman / verbs04.r
Created December 17, 2015 16:07
Adding multiple variables at once using mutate()
# Add a second variable loss_percent to the dataset: m1
m1 <- mutate(hflights, loss = ArrDelay - DepDelay, loss_percent = ((ArrDelay - DepDelay)/DepDelay)*100)
# mutate() allows you to use a new variable while creating a next variable in the same call
# Copy and adapt the previous command to reduce redendancy: m2
m2 <- mutate(hflights, loss = ArrDelay - DepDelay, loss_percent = (loss/DepDelay) * 100 )
# Add the three variables as described in the third instruction: m3
m3 <- mutate(hflights, TotalTaxi = TaxiIn + TaxiOut, ActualGroundTime = ActualElapsedTime - AirTime, Diff = TotalTaxi - ActualGroundTime)
# Add the new variable ActualGroundTime to a copy of hflights and save the result as g1.
g1 <- mutate(hflights, ActualGroundTime = ActualElapsedTime - AirTime)
# Add the new variable GroundTime to a g1. Save the result as g2.
g2 <- mutate(g1, GroundTime = TaxiIn + TaxiOut)
# Add the new variable AverageSpeed to g2. Save the result as g3.
g3 <- mutate(g2, AverageSpeed = Distance / AirTime * 60)
# Print out g3
@anirudhjayaraman
anirudhjayaraman / comparisons01.R
Created December 17, 2015 13:30
Comparisons between base R and dplyr`
# Some comparisons to basic R
# both hflights and dplyr are available
ex1r <- hflights[c("TaxiIn","TaxiOut","Distance")]
ex1d <- select(hflights, TaxiIn, TaxiOut, Distance)
ex2r <- hflights[c("Year","Month","DayOfWeek","DepTime","ArrTime")]
ex2d <- select(hflights, Year:ArrTime, -DayofMonth)
ex3r <- hflights[c("TailNum","TaxiIn","TaxiOut")]
@anirudhjayaraman
anirudhjayaraman / verbs02.R
Created December 17, 2015 13:20
Helper functions used along with select()
# Helper functions used with dplyr
# Print out a tbl containing just ArrDelay and DepDelay
select(hflights, ArrDelay, DepDelay)
# Use a combination of helper functions and variable names to print out
# only the UniqueCarrier, FlightNum, TailNum, Cancelled, and CancellationCode
# columns of hflights
select(hflights, UniqueCarrier, FlightNum, contains("Tail"), contains("Cancel"))
# Find the most concise way to return the following columns with select and its
@anirudhjayaraman
anirudhjayaraman / verbs01.R
Created December 17, 2015 12:37
Select and Mutate
hflights[c('ActualElapsedTime','ArrDelay','DepDelay')]
# Equivalently, using dplyr:
select(hflights, ActualElapsedTime, ArrDelay, DepDelay)
# Print out a tbl with the four columns of hflights related to delay
select(hflights, ActualElapsedTime, AirTime, ArrDelay, DepDelay)
# Print out hflights, nothing has changed!
hflights
@anirudhjayaraman
anirudhjayaraman / introduction.R
Last active December 17, 2015 06:51
Code from the 1st section of Datacamp's course on Data Manipulation in R with dplyr
# INTRODUCTION TO dplyr AND tbls
# Load the dplyr package
library(dplyr)
# Load the hflights package
library(hflights)
# Call both head() and summary() on hflights
head(hflights)
summary(hflights)
@anirudhjayaraman
anirudhjayaraman / Section2_7_13.m
Created December 14, 2015 15:34
Solution to Problem 13, Section 2.7 Linear Algebra (Gilbert Strang)
% Solution for part (a)
p = permMatrices(3);
n = size(p,3); % number of permutation matrices
v = zeros(n,1); % vector of zeros with dimension equalling number of permutation matrices
% check for permutation matrices other than identity matrix with 3rd power equalling identity matrix
for i = 1:n
if p(:,:,i)^3 == eye(3)
v(i,1) = 1;
end
end
@anirudhjayaraman
anirudhjayaraman / output13a.txt
Created December 14, 2015 13:18
Output to 13(a)
ans(:,:,1) =
0 1 0
0 0 1
1 0 0
ans(:,:,2) =
0 0 1
1 0 0
@anirudhjayaraman
anirudhjayaraman / output13b.txt
Created December 14, 2015 12:15
Output to 13B
m =
ans(:,:,1) =
0 1 0 0
0 0 1 0
1 0 0 0
0 0 0 1
ans(:,:,2) =
@anirudhjayaraman
anirudhjayaraman / permMatrices.m
Created December 14, 2015 08:29
Create a list of permuation matrices in Octave / Matlab
% function to generate permutation matrices given the size of the desired permutation matrices
function x = permMatrices(n)
x = zeros(n,n,factorial(n));
permutations = perms(1:n);
for i = 1:size(x,3)
x(:,:,i) = eye(n)(permutations(i,:),:);
end
endfunction