I am attempting the exercise in R for data science (7.5.2.1, #2): Use geom_tile() together with dplyr to explore how average flight delays vary by destination and month of year. What makes the plot difficult to read? How could you improve it?

First, transmute columns.

library(nycflights13) foo <- nycflights13::flights %>% transmute(tot_delay = dep_delay + arr_delay, m = month, d = dest) %>% filter(!is.na(tot_delay)) %>% group_by(m, d) %>% summarise(avg_delay = mean(tot_delay)) 

Now foo appears to be a data frame based on the 'Source' output.

> foo Source: local data frame [1,112 x 3] Groups: m [?] m d avg_delay <int> <chr> <dbl> 1 1 ALB 76.571429 2 1 ATL 8.567982 3 1 AUS 19.017751 4 1 AVL 49.000000 5 1 BDL 32.081081 6 1 BHM 47.043478 7 1 BNA 25.930233 8 1 BOS 2.698517 9 1 BQN 8.516129 10 1 BTV 18.393665 # ... with 1,102 more rows 

It doesn't appear that as_tibble is working, what could I be doing wrong?

> as_tibble(foo) Source: local data frame [1,112 x 3] Groups: m [?] m d avg_delay <int> <chr> <dbl> 1 1 ALB 76.571429 2 1 ATL 8.567982 3 1 AUS 19.017751 4 1 AVL 49.000000 5 1 BDL 32.081081 6 1 BHM 47.043478 7 1 BNA 25.930233 8 1 BOS 2.698517 9 1 BQN 8.516129 10 1 BTV 18.393665 # ... with 1,102 more rows 

Shouldn't the internals be different for a tibble?

> str(foo) Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 1112 obs. of 3 variables: $ m : int 1 1 1 1 1 1 1 1 1 1 ... $ d : chr "ALB" "ATL" "AUS" "AVL" ... $ avg_delay: num 76.57 8.57 19.02 49 32.08 ... - attr(*, "vars")=List of 1 ..$ : symbol m - attr(*, "drop")= logi TRUE > str(as_tibble(foo)) Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 1112 obs. of 3 variables: $ m : int 1 1 1 1 1 1 1 1 1 1 ... $ d : chr "ALB" "ATL" "AUS" "AVL" ... $ avg_delay: num 76.57 8.57 19.02 49 32.08 ... - attr(*, "vars")=List of 1 ..$ : symbol m - attr(*, "drop")= logi TRUE 

Note that as_tibble() works as expected

> packageDescription("tibble") Package: tibble Encoding: UTF-8 Version: 1.3.0 > is_tibble(foo) [1] TRUE 
4

1 Answer

Works for me - foo is a "tibble" and is announced as "A tibble: 112 x 3" in the print:

> foo Source: local data frame [1,112 x 3] Groups: m [?] # A tibble: 1,112 x 3 m d avg_delay <int> <chr> <dbl> 1 1 ALB 76.571429 2 1 ATL 8.567982 

So you possibly have an old version of dplyr. Mine is:

> packageDescription("dplyr") Package: dplyr Type: Package Version: 0.5.0 

And everything else:

> sessionInfo() R version 3.3.1 (2016-06-21) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 14.04.4 LTS locale: [1] LC_CTYPE=en_GB.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_GB.UTF-8 LC_COLLATE=en_GB.UTF-8 [5] LC_MONETARY=en_GB.UTF-8 LC_MESSAGES=en_GB.UTF-8 [7] LC_PAPER=en_GB.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] dplyr_0.5.0 tibble_1.3.1 loaded via a namespace (and not attached): [1] magrittr_1.5 R6_2.2.0 assertthat_0.2.0 DBI_0.5-1 [5] tools_3.3.1 Rcpp_0.12.11 rlang_0.1.1 
3

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.