From 4a0e4a5dc4ce4a363585c0281ad99f91d48d9ad0 Mon Sep 17 00:00:00 2001 From: mkshaw <52977155+mkshaw@users.noreply.github.com> Date: Mon, 14 Oct 2019 11:44:32 -0400 Subject: [PATCH] Update transform_solutions.Rmd Easier solution to Exercise 4.3.1 number 3, using the air_time variable given in the flights data frame. --- transform_solutions.Rmd | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/transform_solutions.Rmd b/transform_solutions.Rmd index 87bb8f9..8552cc7 100644 --- a/transform_solutions.Rmd +++ b/transform_solutions.Rmd @@ -96,21 +96,8 @@ arrange(flights, dep_delay) ### 3. Sort flights to find the fastest flights. ```{r} -# Note - this is a bit tricky since the time stamps are just encoded as integers -# so if a flight left at midnight (i.e. dep_time=2400) and arrived at 00:54 (arr_time=54), -# it's hard to just do arr_time - dep_time to get the travel time (you get back -2346, which doesn't make sense). -# Taking absolute values doesn't help either. -# A workaround solution is just to add 2400 if the travel time is ever negative. -# A better solution is to properly encode the times as timestamps -# note: we use the `mutate` function and the pipe character `%>%`, which haven't been introduced yet - -flights %>% mutate(travel_time = ifelse((arr_time - dep_time < 0), - 2400+(arr_time - dep_time), - arr_time - dep_time)) %>% - arrange(travel_time) %>% select(arr_time, dep_time, travel_time) - -# for demonstration purposes, the naive solution is -arrange(flights, (arr_time - dep_time)) +# use the variable air_time, which gives amount of time spent in the air, in minutes. +arrange(flights, air_time) ```