l’ve tried to do recap exercise of dplyr package with weather data of four travel destinations.I haven’t done all the task yet. The data consists of temperature (in Celsius) history of 4 popular travel destinations (NYC, Amsterdam, London and Venice) between November 2015 and October 2017. Raw data is gathered from Weather Underground and it is only for educational purposes.

setwd("C:/bengi")
load("travel_weather.RData")
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats

Travel Weather Data

travel_weather %>%
  tbl_df()
## # A tibble: 731 x 7
##     year month   day Amsterdam London   NYC Venice
##  * <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2015    11     1         8      8    16     13
##  2  2015    11     2        10     11    15     10
##  3  2015    11     3         9     11    16      9
##  4  2015    11     4        12     11    17     10
##  5  2015    11     5        13     13    18     12
##  6  2015    11     6        16     14    21     13
##  7  2015    11     7        16     14    17     14
##  8  2015    11     8        12     12    11     13
##  9  2015    11     9        13     12    11     11
## 10  2015    11    10        14     14    12     11
## # ... with 721 more rows
glimpse(travel_weather)
## Observations: 731
## Variables: 7
## $ year      <dbl> 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015...
## $ month     <dbl> 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, ...
## $ day       <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1...
## $ Amsterdam <dbl> 8, 10, 9, 12, 13, 16, 16, 12, 13, 14, 13, 13, 11, 11...
## $ London    <dbl> 8, 11, 11, 11, 13, 14, 14, 12, 12, 14, 13, 12, 10, 1...
## $ NYC       <dbl> 16, 15, 16, 17, 18, 21, 17, 11, 11, 12, 12, 13, 11, ...
## $ Venice    <dbl> 13, 10, 9, 10, 12, 13, 14, 13, 11, 11, 9, 11, 8, 11,...

Select

1

travel_weather %>% select(year, month, day, Venice)
## # A tibble: 731 x 4
##     year month   day Venice
##  * <dbl> <dbl> <dbl>  <dbl>
##  1  2015    11     1     13
##  2  2015    11     2     10
##  3  2015    11     3      9
##  4  2015    11     4     10
##  5  2015    11     5     12
##  6  2015    11     6     13
##  7  2015    11     7     14
##  8  2015    11     8     13
##  9  2015    11     9     11
## 10  2015    11    10     11
## # ... with 721 more rows

2

travel_weather %>% select(Amsterdam:Venice)
## # A tibble: 731 x 4
##    Amsterdam London   NYC Venice
##  *     <dbl>  <dbl> <dbl>  <dbl>
##  1         8      8    16     13
##  2        10     11    15     10
##  3         9     11    16      9
##  4        12     11    17     10
##  5        13     13    18     12
##  6        16     14    21     13
##  7        16     14    17     14
##  8        12     12    11     13
##  9        13     12    11     11
## 10        14     14    12     11
## # ... with 721 more rows

3

travel_weather %>% arrange(NYC, desc(Amsterdam)) %>% select(-London, -Venice)
## # A tibble: 731 x 5
##     year month   day Amsterdam   NYC
##    <dbl> <dbl> <dbl>     <dbl> <dbl>
##  1  2016     2    14         2   -14
##  2  2016     2    13         1   -10
##  3  2016     1     5         6    -7
##  4  2017     1     9         6    -7
##  5  2016    12    16         6    -6
##  6  2017     1     8         4    -6
##  7  2016     2    12         2    -6
##  8  2016     1    19        -2    -6
##  9  2017     3    15         9    -5
## 10  2017     3    11         7    -5
## # ... with 721 more rows

4

travel_weather%>%rename('New York'= NYC)
## # A tibble: 731 x 7
##     year month   day Amsterdam London `New York` Venice
##  * <dbl> <dbl> <dbl>     <dbl>  <dbl>      <dbl>  <dbl>
##  1  2015    11     1         8      8         16     13
##  2  2015    11     2        10     11         15     10
##  3  2015    11     3         9     11         16      9
##  4  2015    11     4        12     11         17     10
##  5  2015    11     5        13     13         18     12
##  6  2015    11     6        16     14         21     13
##  7  2015    11     7        16     14         17     14
##  8  2015    11     8        12     12         11     13
##  9  2015    11     9        13     12         11     11
## 10  2015    11    10        14     14         12     11
## # ... with 721 more rows
names(travel_weather)
## [1] "year"      "month"     "day"       "Amsterdam" "London"    "NYC"      
## [7] "Venice"

Filter

1

  travel_weather %>% filter(day<=3)
## # A tibble: 72 x 7
##     year month   day Amsterdam London   NYC Venice
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2015    11     1         8      8    16     13
##  2  2015    11     2        10     11    15     10
##  3  2015    11     3         9     11    16      9
##  4  2015    12     1         9     11     9      6
##  5  2015    12     2        10     12    11      8
##  6  2015    12     3         9     11    10      8
##  7  2016     1     1         4      3     3      2
##  8  2016     1     2         6     10     2      0
##  9  2016     1     3         7      8     4      3
## 10  2016     2     1        10     12    11      6
## # ... with 62 more rows

2

travel_weather %>% filter(month==11 & Venice > NYC)
## # A tibble: 20 x 7
##     year month   day Amsterdam London   NYC Venice
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2015    11     8        12     12    11     13
##  2  2015    11    14        11     10     8     11
##  3  2015    11    15        12     14     9     11
##  4  2015    11    17        13     13     8      9
##  5  2015    11    23         3      3     4      6
##  6  2015    11    24         5      8     4      6
##  7  2016    11     1        10      9     9     11
##  8  2016    11     6         7      4    11     12
##  9  2016    11     7         4      6     8     11
## 10  2016    11    12         1      8     7      9
## 11  2016    11    19         6      4    10     11
## 12  2016    11    20         7      7     3     11
## 13  2016    11    21        10     10     4     12
## 14  2016    11    22        10      9     4     14
## 15  2016    11    23         8      7     4     14
## 16  2016    11    24         6      9     6     13
## 17  2016    11    25         3      7    10     13
## 18  2016    11    26         3      6     7     12
## 19  2016    11    27         5      7     7     11
## 20  2016    11    28         1      6     7      8

3

travel_weather %>% filter(month == 7 & Amsterdam > (London | Venice))
## # A tibble: 62 x 7
##     year month   day Amsterdam London   NYC Venice
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2016     7     1        16     16    23     25
##  2  2016     7     2        16     14    21     25
##  3  2016     7     3        14     15    22     24
##  4  2016     7     4        16     16    24     24
##  5  2016     7     5        16     17    26     23
##  6  2016     7     6        14     16    28     24
##  7  2016     7     7        16     18    28     26
##  8  2016     7     8        18     19    25     25
##  9  2016     7     9        18     19    21     27
## 10  2016     7    10        20     20    23     28
## # ... with 52 more rows
travel_weather%>%filter()
## # A tibble: 731 x 7
##     year month   day Amsterdam London   NYC Venice
##  * <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2015    11     1         8      8    16     13
##  2  2015    11     2        10     11    15     10
##  3  2015    11     3         9     11    16      9
##  4  2015    11     4        12     11    17     10
##  5  2015    11     5        13     13    18     12
##  6  2015    11     6        16     14    21     13
##  7  2015    11     7        16     14    17     14
##  8  2015    11     8        12     12    11     13
##  9  2015    11     9        13     12    11     11
## 10  2015    11    10        14     14    12     11
## # ... with 721 more rows

Arrange

1

travel_weather %>% arrange(NYC)
## # A tibble: 731 x 7
##     year month   day Amsterdam London   NYC Venice
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2016     2    14         2      3   -14      6
##  2  2016     2    13         1      2   -10      4
##  3  2016     1     5         6      8    -7      2
##  4  2017     1     9         6      7    -7     -2
##  5  2016     1    19        -2      0    -6      1
##  6  2016     2    12         2      1    -6      6
##  7  2016    12    16         6      6    -6      4
##  8  2017     1     8         4      9    -6     -2
##  9  2017     1     7         1      8    -5     -3
## 10  2017     3    11         7     10    -5      9
## # ... with 721 more rows

2

travel_weather %>% arrange(NYC, desc(Amsterdam))
## # A tibble: 731 x 7
##     year month   day Amsterdam London   NYC Venice
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2016     2    14         2      3   -14      6
##  2  2016     2    13         1      2   -10      4
##  3  2016     1     5         6      8    -7      2
##  4  2017     1     9         6      7    -7     -2
##  5  2016    12    16         6      6    -6      4
##  6  2017     1     8         4      9    -6     -2
##  7  2016     2    12         2      1    -6      6
##  8  2016     1    19        -2      0    -6      1
##  9  2017     3    15         9     11    -5     10
## 10  2017     3    11         7     10    -5      9
## # ... with 721 more rows

3

travel_weather %>% arrange(desc(year),desc(month),desc(day))
## # A tibble: 731 x 7
##     year month   day Amsterdam London   NYC Venice
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>
##  1  2017    10    31         9      9    11     11
##  2  2017    10    30         8      6    12     13
##  3  2017    10    29        11     11    18      9
##  4  2017    10    28        12     10    17     10
##  5  2017    10    27        12      9    13     13
##  6  2017    10    26        13     10    13     13
##  7  2017    10    25        13     14    17     13
##  8  2017    10    24        13     16    21     13
##  9  2017    10    23        13     13    20     13
## 10  2017    10    22        11     11    19     13
## # ... with 721 more rows

mutate/transmute

1

travel_weather %>% mutate(VAdiff = Venice - Amsterdam)
## # A tibble: 731 x 8
##     year month   day Amsterdam London   NYC Venice VAdiff
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>  <dbl>
##  1  2015    11     1         8      8    16     13      5
##  2  2015    11     2        10     11    15     10      0
##  3  2015    11     3         9     11    16      9      0
##  4  2015    11     4        12     11    17     10     -2
##  5  2015    11     5        13     13    18     12     -1
##  6  2015    11     6        16     14    21     13     -3
##  7  2015    11     7        16     14    17     14     -2
##  8  2015    11     8        12     12    11     13      1
##  9  2015    11     9        13     12    11     11     -2
## 10  2015    11    10        14     14    12     11     -3
## # ... with 721 more rows

2

travel_weather %>% mutate(VwarmerA = Venice>Amsterdam)
## # A tibble: 731 x 8
##     year month   day Amsterdam London   NYC Venice VwarmerA
##    <dbl> <dbl> <dbl>     <dbl>  <dbl> <dbl>  <dbl>    <lgl>
##  1  2015    11     1         8      8    16     13     TRUE
##  2  2015    11     2        10     11    15     10    FALSE
##  3  2015    11     3         9     11    16      9    FALSE
##  4  2015    11     4        12     11    17     10    FALSE
##  5  2015    11     5        13     13    18     12    FALSE
##  6  2015    11     6        16     14    21     13    FALSE
##  7  2015    11     7        16     14    17     14    FALSE
##  8  2015    11     8        12     12    11     13     TRUE
##  9  2015    11     9        13     12    11     11    FALSE
## 10  2015    11    10        14     14    12     11    FALSE
## # ... with 721 more rows

group_by/summarise

1

travel_weather %>% summarise(Venice_avg=mean(Venice), NYC_avg=mean(NYC))
## # A tibble: 1 x 2
##   Venice_avg  NYC_avg
##        <dbl>    <dbl>
## 1   14.31601 14.41313

2

travel_weather %>% group_by(month) %>% summarise(Amsterdam_mean=mean(Amsterdam))%>% round(digit=2)
## # A tibble: 12 x 2
##    month Amsterdam_mean
##    <dbl>          <dbl>
##  1     1           3.00
##  2     2           4.32
##  3     3           6.92
##  4     4           8.43
##  5     5          14.48
##  6     6          17.28
##  7     7          18.02
##  8     8          17.68
##  9     9          15.95
## 10    10          11.68
## 11    11           7.65
## 12    12           6.97