This file contains 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
# 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) |
This file contains 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
# 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 |
This file contains 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
# 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")] |
This file contains 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
# 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 |
This file contains 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
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 |
This file contains 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
# 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) |
This file contains 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
% 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 |
This file contains 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
ans(:,:,1) = | |
0 1 0 | |
0 0 1 | |
1 0 0 | |
ans(:,:,2) = | |
0 0 1 | |
1 0 0 |
This file contains 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
m = | |
ans(:,:,1) = | |
0 1 0 0 | |
0 0 1 0 | |
1 0 0 0 | |
0 0 0 1 | |
ans(:,:,2) = |
This file contains 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
% 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 |