dplyr

The “head”" of the dataset looks like this:
head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6
Before we dive into the functions, let’s load up the two packages:
library(datasets)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Filter

The filter function will return all the rows that satisfy a following condition. For example, we can return all rows with Temp greater than 70 as follows:
filter(airquality, Temp > 70)
##     Ozone Solar.R Wind Temp Month Day
## 1      36     118  8.0   72     5   2
## 2      12     149 12.6   74     5   3
## 3       7      NA  6.9   74     5  11
## 4      11     320 16.6   73     5  22
## 5      45     252 14.9   81     5  29
## 6     115     223  5.7   79     5  30
## 7      37     279  7.4   76     5  31
## 8      NA     286  8.6   78     6   1
## 9      NA     287  9.7   74     6   2
## 10     NA     186  9.2   84     6   4
## 11     NA     220  8.6   85     6   5
## 12     NA     264 14.3   79     6   6
## 13     29     127  9.7   82     6   7
## 14     NA     273  6.9   87     6   8
## 15     71     291 13.8   90     6   9
## 16     39     323 11.5   87     6  10
## 17     NA     259 10.9   93     6  11
## 18     NA     250  9.2   92     6  12
## 19     23     148  8.0   82     6  13
## 20     NA     332 13.8   80     6  14
## 21     NA     322 11.5   79     6  15
## 22     21     191 14.9   77     6  16
## 23     37     284 20.7   72     6  17
## 24     12     120 11.5   73     6  19
## 25     13     137 10.3   76     6  20
## 26     NA     150  6.3   77     6  21
## 27     NA      59  1.7   76     6  22
## 28     NA      91  4.6   76     6  23
## 29     NA     250  6.3   76     6  24
## 30     NA     135  8.0   75     6  25
## 31     NA     127  8.0   78     6  26
## 32     NA      47 10.3   73     6  27
## 33     NA      98 11.5   80     6  28
## 34     NA      31 14.9   77     6  29
## 35     NA     138  8.0   83     6  30
## 36    135     269  4.1   84     7   1
## 37     49     248  9.2   85     7   2
## 38     32     236  9.2   81     7   3
## 39     NA     101 10.9   84     7   4
## 40     64     175  4.6   83     7   5
## 41     40     314 10.9   83     7   6
## 42     77     276  5.1   88     7   7
## 43     97     267  6.3   92     7   8
## 44     97     272  5.7   92     7   9
## 45     85     175  7.4   89     7  10
## 46     NA     139  8.6   82     7  11
## 47     10     264 14.3   73     7  12
## 48     27     175 14.9   81     7  13
## 49     NA     291 14.9   91     7  14
## 50      7      48 14.3   80     7  15
## 51     48     260  6.9   81     7  16
## 52     35     274 10.3   82     7  17
## 53     61     285  6.3   84     7  18
## 54     79     187  5.1   87     7  19
## 55     63     220 11.5   85     7  20
## 56     16       7  6.9   74     7  21
## 57     NA     258  9.7   81     7  22
## 58     NA     295 11.5   82     7  23
## 59     80     294  8.6   86     7  24
## 60    108     223  8.0   85     7  25
## 61     20      81  8.6   82     7  26
## 62     52      82 12.0   86     7  27
## 63     82     213  7.4   88     7  28
## 64     50     275  7.4   86     7  29
## 65     64     253  7.4   83     7  30
## 66     59     254  9.2   81     7  31
## 67     39      83  6.9   81     8   1
## 68      9      24 13.8   81     8   2
## 69     16      77  7.4   82     8   3
## 70     78      NA  6.9   86     8   4
## 71     35      NA  7.4   85     8   5
## 72     66      NA  4.6   87     8   6
## 73    122     255  4.0   89     8   7
## 74     89     229 10.3   90     8   8
## 75    110     207  8.0   90     8   9
## 76     NA     222  8.6   92     8  10
## 77     NA     137 11.5   86     8  11
## 78     44     192 11.5   86     8  12
## 79     28     273 11.5   82     8  13
## 80     65     157  9.7   80     8  14
## 81     NA      64 11.5   79     8  15
## 82     22      71 10.3   77     8  16
## 83     59      51  6.3   79     8  17
## 84     23     115  7.4   76     8  18
## 85     31     244 10.9   78     8  19
## 86     44     190 10.3   78     8  20
## 87     21     259 15.5   77     8  21
## 88      9      36 14.3   72     8  22
## 89     NA     255 12.6   75     8  23
## 90     45     212  9.7   79     8  24
## 91    168     238  3.4   81     8  25
## 92     73     215  8.0   86     8  26
## 93     NA     153  5.7   88     8  27
## 94     76     203  9.7   97     8  28
## 95    118     225  2.3   94     8  29
## 96     84     237  6.3   96     8  30
## 97     85     188  6.3   94     8  31
## 98     96     167  6.9   91     9   1
## 99     78     197  5.1   92     9   2
## 100    73     183  2.8   93     9   3
## 101    91     189  4.6   93     9   4
## 102    47      95  7.4   87     9   5
## 103    32      92 15.5   84     9   6
## 104    20     252 10.9   80     9   7
## 105    23     220 10.3   78     9   8
## 106    21     230 10.9   75     9   9
## 107    24     259  9.7   73     9  10
## 108    44     236 14.9   81     9  11
## 109    21     259 15.5   76     9  12
## 110    28     238  6.3   77     9  13
## 111     9      24 10.9   71     9  14
## 112    13     112 11.5   71     9  15
## 113    46     237  6.9   78     9  16
## 114    13      27 10.3   76     9  18
## 115    16     201  8.0   82     9  20
## 116    23      14  9.2   71     9  22
## 117    36     139 10.3   81     9  23
## 118    NA     145 13.2   77     9  27
## 119    14     191 14.3   75     9  28
## 120    18     131  8.0   76     9  29
We can specify multiple conditions. The example below will return all rows with Temp larger than 80 and Month higher than 5
filter(airquality, Temp > 80 & Month > 5)
##    Ozone Solar.R Wind Temp Month Day
## 1     NA     186  9.2   84     6   4
## 2     NA     220  8.6   85     6   5
## 3     29     127  9.7   82     6   7
## 4     NA     273  6.9   87     6   8
## 5     71     291 13.8   90     6   9
## 6     39     323 11.5   87     6  10
## 7     NA     259 10.9   93     6  11
## 8     NA     250  9.2   92     6  12
## 9     23     148  8.0   82     6  13
## 10    NA     138  8.0   83     6  30
## 11   135     269  4.1   84     7   1
## 12    49     248  9.2   85     7   2
## 13    32     236  9.2   81     7   3
## 14    NA     101 10.9   84     7   4
## 15    64     175  4.6   83     7   5
## 16    40     314 10.9   83     7   6
## 17    77     276  5.1   88     7   7
## 18    97     267  6.3   92     7   8
## 19    97     272  5.7   92     7   9
## 20    85     175  7.4   89     7  10
## 21    NA     139  8.6   82     7  11
## 22    27     175 14.9   81     7  13
## 23    NA     291 14.9   91     7  14
## 24    48     260  6.9   81     7  16
## 25    35     274 10.3   82     7  17
## 26    61     285  6.3   84     7  18
## 27    79     187  5.1   87     7  19
## 28    63     220 11.5   85     7  20
## 29    NA     258  9.7   81     7  22
## 30    NA     295 11.5   82     7  23
## 31    80     294  8.6   86     7  24
## 32   108     223  8.0   85     7  25
## 33    20      81  8.6   82     7  26
## 34    52      82 12.0   86     7  27
## 35    82     213  7.4   88     7  28
## 36    50     275  7.4   86     7  29
## 37    64     253  7.4   83     7  30
## 38    59     254  9.2   81     7  31
## 39    39      83  6.9   81     8   1
## 40     9      24 13.8   81     8   2
## 41    16      77  7.4   82     8   3
## 42    78      NA  6.9   86     8   4
## 43    35      NA  7.4   85     8   5
## 44    66      NA  4.6   87     8   6
## 45   122     255  4.0   89     8   7
## 46    89     229 10.3   90     8   8
## 47   110     207  8.0   90     8   9
## 48    NA     222  8.6   92     8  10
## 49    NA     137 11.5   86     8  11
## 50    44     192 11.5   86     8  12
## 51    28     273 11.5   82     8  13
## 52   168     238  3.4   81     8  25
## 53    73     215  8.0   86     8  26
## 54    NA     153  5.7   88     8  27
## 55    76     203  9.7   97     8  28
## 56   118     225  2.3   94     8  29
## 57    84     237  6.3   96     8  30
## 58    85     188  6.3   94     8  31
## 59    96     167  6.9   91     9   1
## 60    78     197  5.1   92     9   2
## 61    73     183  2.8   93     9   3
## 62    91     189  4.6   93     9   4
## 63    47      95  7.4   87     9   5
## 64    32      92 15.5   84     9   6
## 65    44     236 14.9   81     9  11
## 66    16     201  8.0   82     9  20
## 67    36     139 10.3   81     9  23

Mutate ##### Mutate is used to add new variables to the data. For example, let’s adds a new column that displays the temperature in Celsius.

mutate(airquality, TempInC = (Temp - 32) * 5 / 9)
##     Ozone Solar.R Wind Temp Month Day  TempInC
## 1      41     190  7.4   67     5   1 19.44444
## 2      36     118  8.0   72     5   2 22.22222
## 3      12     149 12.6   74     5   3 23.33333
## 4      18     313 11.5   62     5   4 16.66667
## 5      NA      NA 14.3   56     5   5 13.33333
## 6      28      NA 14.9   66     5   6 18.88889
## 7      23     299  8.6   65     5   7 18.33333
## 8      19      99 13.8   59     5   8 15.00000
## 9       8      19 20.1   61     5   9 16.11111
## 10     NA     194  8.6   69     5  10 20.55556
## 11      7      NA  6.9   74     5  11 23.33333
## 12     16     256  9.7   69     5  12 20.55556
## 13     11     290  9.2   66     5  13 18.88889
## 14     14     274 10.9   68     5  14 20.00000
## 15     18      65 13.2   58     5  15 14.44444
## 16     14     334 11.5   64     5  16 17.77778
## 17     34     307 12.0   66     5  17 18.88889
## 18      6      78 18.4   57     5  18 13.88889
## 19     30     322 11.5   68     5  19 20.00000
## 20     11      44  9.7   62     5  20 16.66667
## 21      1       8  9.7   59     5  21 15.00000
## 22     11     320 16.6   73     5  22 22.77778
## 23      4      25  9.7   61     5  23 16.11111
## 24     32      92 12.0   61     5  24 16.11111
## 25     NA      66 16.6   57     5  25 13.88889
## 26     NA     266 14.9   58     5  26 14.44444
## 27     NA      NA  8.0   57     5  27 13.88889
## 28     23      13 12.0   67     5  28 19.44444
## 29     45     252 14.9   81     5  29 27.22222
## 30    115     223  5.7   79     5  30 26.11111
## 31     37     279  7.4   76     5  31 24.44444
## 32     NA     286  8.6   78     6   1 25.55556
## 33     NA     287  9.7   74     6   2 23.33333
## 34     NA     242 16.1   67     6   3 19.44444
## 35     NA     186  9.2   84     6   4 28.88889
## 36     NA     220  8.6   85     6   5 29.44444
## 37     NA     264 14.3   79     6   6 26.11111
## 38     29     127  9.7   82     6   7 27.77778
## 39     NA     273  6.9   87     6   8 30.55556
## 40     71     291 13.8   90     6   9 32.22222
## 41     39     323 11.5   87     6  10 30.55556
## 42     NA     259 10.9   93     6  11 33.88889
## 43     NA     250  9.2   92     6  12 33.33333
## 44     23     148  8.0   82     6  13 27.77778
## 45     NA     332 13.8   80     6  14 26.66667
## 46     NA     322 11.5   79     6  15 26.11111
## 47     21     191 14.9   77     6  16 25.00000
## 48     37     284 20.7   72     6  17 22.22222
## 49     20      37  9.2   65     6  18 18.33333
## 50     12     120 11.5   73     6  19 22.77778
## 51     13     137 10.3   76     6  20 24.44444
## 52     NA     150  6.3   77     6  21 25.00000
## 53     NA      59  1.7   76     6  22 24.44444
## 54     NA      91  4.6   76     6  23 24.44444
## 55     NA     250  6.3   76     6  24 24.44444
## 56     NA     135  8.0   75     6  25 23.88889
## 57     NA     127  8.0   78     6  26 25.55556
## 58     NA      47 10.3   73     6  27 22.77778
## 59     NA      98 11.5   80     6  28 26.66667
## 60     NA      31 14.9   77     6  29 25.00000
## 61     NA     138  8.0   83     6  30 28.33333
## 62    135     269  4.1   84     7   1 28.88889
## 63     49     248  9.2   85     7   2 29.44444
## 64     32     236  9.2   81     7   3 27.22222
## 65     NA     101 10.9   84     7   4 28.88889
## 66     64     175  4.6   83     7   5 28.33333
## 67     40     314 10.9   83     7   6 28.33333
## 68     77     276  5.1   88     7   7 31.11111
## 69     97     267  6.3   92     7   8 33.33333
## 70     97     272  5.7   92     7   9 33.33333
## 71     85     175  7.4   89     7  10 31.66667
## 72     NA     139  8.6   82     7  11 27.77778
## 73     10     264 14.3   73     7  12 22.77778
## 74     27     175 14.9   81     7  13 27.22222
## 75     NA     291 14.9   91     7  14 32.77778
## 76      7      48 14.3   80     7  15 26.66667
## 77     48     260  6.9   81     7  16 27.22222
## 78     35     274 10.3   82     7  17 27.77778
## 79     61     285  6.3   84     7  18 28.88889
## 80     79     187  5.1   87     7  19 30.55556
## 81     63     220 11.5   85     7  20 29.44444
## 82     16       7  6.9   74     7  21 23.33333
## 83     NA     258  9.7   81     7  22 27.22222
## 84     NA     295 11.5   82     7  23 27.77778
## 85     80     294  8.6   86     7  24 30.00000
## 86    108     223  8.0   85     7  25 29.44444
## 87     20      81  8.6   82     7  26 27.77778
## 88     52      82 12.0   86     7  27 30.00000
## 89     82     213  7.4   88     7  28 31.11111
## 90     50     275  7.4   86     7  29 30.00000
## 91     64     253  7.4   83     7  30 28.33333
## 92     59     254  9.2   81     7  31 27.22222
## 93     39      83  6.9   81     8   1 27.22222
## 94      9      24 13.8   81     8   2 27.22222
## 95     16      77  7.4   82     8   3 27.77778
## 96     78      NA  6.9   86     8   4 30.00000
## 97     35      NA  7.4   85     8   5 29.44444
## 98     66      NA  4.6   87     8   6 30.55556
## 99    122     255  4.0   89     8   7 31.66667
## 100    89     229 10.3   90     8   8 32.22222
## 101   110     207  8.0   90     8   9 32.22222
## 102    NA     222  8.6   92     8  10 33.33333
## 103    NA     137 11.5   86     8  11 30.00000
## 104    44     192 11.5   86     8  12 30.00000
## 105    28     273 11.5   82     8  13 27.77778
## 106    65     157  9.7   80     8  14 26.66667
## 107    NA      64 11.5   79     8  15 26.11111
## 108    22      71 10.3   77     8  16 25.00000
## 109    59      51  6.3   79     8  17 26.11111
## 110    23     115  7.4   76     8  18 24.44444
## 111    31     244 10.9   78     8  19 25.55556
## 112    44     190 10.3   78     8  20 25.55556
## 113    21     259 15.5   77     8  21 25.00000
## 114     9      36 14.3   72     8  22 22.22222
## 115    NA     255 12.6   75     8  23 23.88889
## 116    45     212  9.7   79     8  24 26.11111
## 117   168     238  3.4   81     8  25 27.22222
## 118    73     215  8.0   86     8  26 30.00000
## 119    NA     153  5.7   88     8  27 31.11111
## 120    76     203  9.7   97     8  28 36.11111
## 121   118     225  2.3   94     8  29 34.44444
## 122    84     237  6.3   96     8  30 35.55556
## 123    85     188  6.3   94     8  31 34.44444
## 124    96     167  6.9   91     9   1 32.77778
## 125    78     197  5.1   92     9   2 33.33333
## 126    73     183  2.8   93     9   3 33.88889
## 127    91     189  4.6   93     9   4 33.88889
## 128    47      95  7.4   87     9   5 30.55556
## 129    32      92 15.5   84     9   6 28.88889
## 130    20     252 10.9   80     9   7 26.66667
## 131    23     220 10.3   78     9   8 25.55556
## 132    21     230 10.9   75     9   9 23.88889
## 133    24     259  9.7   73     9  10 22.77778
## 134    44     236 14.9   81     9  11 27.22222
## 135    21     259 15.5   76     9  12 24.44444
## 136    28     238  6.3   77     9  13 25.00000
## 137     9      24 10.9   71     9  14 21.66667
## 138    13     112 11.5   71     9  15 21.66667
## 139    46     237  6.9   78     9  16 25.55556
## 140    18     224 13.8   67     9  17 19.44444
## 141    13      27 10.3   76     9  18 24.44444
## 142    24     238 10.3   68     9  19 20.00000
## 143    16     201  8.0   82     9  20 27.77778
## 144    13     238 12.6   64     9  21 17.77778
## 145    23      14  9.2   71     9  22 21.66667
## 146    36     139 10.3   81     9  23 27.22222
## 147     7      49 10.3   69     9  24 20.55556
## 148    14      20 16.6   63     9  25 17.22222
## 149    30     193  6.9   70     9  26 21.11111
## 150    NA     145 13.2   77     9  27 25.00000
## 151    14     191 14.3   75     9  28 23.88889
## 152    18     131  8.0   76     9  29 24.44444
## 153    20     223 11.5   68     9  30 20.00000

Summarise ##### The summarise function is used to summarise multiple values into a single value. It is very powerful when used in conjunction with the other functions in the dplyr package, as demonstrated below. na.rm = TRUE will remove all NA values while calculating the mean, so that it doesn’t produce spurious results.

summarise(airquality, mean(Temp, na.rm = TRUE))
##   mean(Temp, na.rm = TRUE)
## 1                 77.88235

Group By ##### The group_by function is used to group data by one or more variables. For example, we can group the data together based on the Month, and then use the summarise function to calculate and display the mean temperature for each month.

summarise(group_by(airquality, Month), mean(Temp, na.rm = TRUE))
## # A tibble: 5 x 2
##   Month `mean(Temp, na.rm = TRUE)`
##   <int>                      <dbl>
## 1     5                       65.5
## 2     6                       79.1
## 3     7                       83.9
## 4     8                       84.0
## 5     9                       76.9

Sample ##### The sample function is used to select random rows from a table. The first line of code randomly selects ten rows from the dataset, and the second line of code randomly selects 15 rows (10% of the original 153 rows) from the dataset.

sample_n(airquality, size = 10)
##     Ozone Solar.R Wind Temp Month Day
## 70     97     272  5.7   92     7   9
## 66     64     175  4.6   83     7   5
## 23      4      25  9.7   61     5  23
## 107    NA      64 11.5   79     8  15
## 12     16     256  9.7   69     5  12
## 135    21     259 15.5   76     9  12
## 1      41     190  7.4   67     5   1
## 52     NA     150  6.3   77     6  21
## 6      28      NA 14.9   66     5   6
## 126    73     183  2.8   93     9   3
sample_frac(airquality, size = 0.1)
##     Ozone Solar.R Wind Temp Month Day
## 54     NA      91  4.6   76     6  23
## 138    13     112 11.5   71     9  15
## 135    21     259 15.5   76     9  12
## 59     NA      98 11.5   80     6  28
## 37     NA     264 14.3   79     6   6
## 34     NA     242 16.1   67     6   3
## 35     NA     186  9.2   84     6   4
## 50     12     120 11.5   73     6  19
## 38     29     127  9.7   82     6   7
## 45     NA     332 13.8   80     6  14
## 100    89     229 10.3   90     8   8
## 58     NA      47 10.3   73     6  27
## 99    122     255  4.0   89     8   7
## 56     NA     135  8.0   75     6  25
## 11      7      NA  6.9   74     5  11

Count ##### The count function tallies observations based on a group. It is slightly similar to the table function in the base package. For example:

count(airquality, Month)
## # A tibble: 5 x 2
##   Month     n
##   <int> <int>
## 1     5    31
## 2     6    30
## 3     7    31
## 4     8    31
## 5     9    30
This means that there are 31 rows with Month = 5, 30 rows with Month = 6, and so on.

Arrange ##### The arrange function is used to arrange rows by variables. Currently, the airquality dataset is arranged based on Month, and then Day. We can use the arrange function to arrange the rows in the descending order of Month, and then in the ascending order of Day.

arrange(airquality, desc(Month), Day)
##     Ozone Solar.R Wind Temp Month Day
## 1      96     167  6.9   91     9   1
## 2      78     197  5.1   92     9   2
## 3      73     183  2.8   93     9   3
## 4      91     189  4.6   93     9   4
## 5      47      95  7.4   87     9   5
## 6      32      92 15.5   84     9   6
## 7      20     252 10.9   80     9   7
## 8      23     220 10.3   78     9   8
## 9      21     230 10.9   75     9   9
## 10     24     259  9.7   73     9  10
## 11     44     236 14.9   81     9  11
## 12     21     259 15.5   76     9  12
## 13     28     238  6.3   77     9  13
## 14      9      24 10.9   71     9  14
## 15     13     112 11.5   71     9  15
## 16     46     237  6.9   78     9  16
## 17     18     224 13.8   67     9  17
## 18     13      27 10.3   76     9  18
## 19     24     238 10.3   68     9  19
## 20     16     201  8.0   82     9  20
## 21     13     238 12.6   64     9  21
## 22     23      14  9.2   71     9  22
## 23     36     139 10.3   81     9  23
## 24      7      49 10.3   69     9  24
## 25     14      20 16.6   63     9  25
## 26     30     193  6.9   70     9  26
## 27     NA     145 13.2   77     9  27
## 28     14     191 14.3   75     9  28
## 29     18     131  8.0   76     9  29
## 30     20     223 11.5   68     9  30
## 31     39      83  6.9   81     8   1
## 32      9      24 13.8   81     8   2
## 33     16      77  7.4   82     8   3
## 34     78      NA  6.9   86     8   4
## 35     35      NA  7.4   85     8   5
## 36     66      NA  4.6   87     8   6
## 37    122     255  4.0   89     8   7
## 38     89     229 10.3   90     8   8
## 39    110     207  8.0   90     8   9
## 40     NA     222  8.6   92     8  10
## 41     NA     137 11.5   86     8  11
## 42     44     192 11.5   86     8  12
## 43     28     273 11.5   82     8  13
## 44     65     157  9.7   80     8  14
## 45     NA      64 11.5   79     8  15
## 46     22      71 10.3   77     8  16
## 47     59      51  6.3   79     8  17
## 48     23     115  7.4   76     8  18
## 49     31     244 10.9   78     8  19
## 50     44     190 10.3   78     8  20
## 51     21     259 15.5   77     8  21
## 52      9      36 14.3   72     8  22
## 53     NA     255 12.6   75     8  23
## 54     45     212  9.7   79     8  24
## 55    168     238  3.4   81     8  25
## 56     73     215  8.0   86     8  26
## 57     NA     153  5.7   88     8  27
## 58     76     203  9.7   97     8  28
## 59    118     225  2.3   94     8  29
## 60     84     237  6.3   96     8  30
## 61     85     188  6.3   94     8  31
## 62    135     269  4.1   84     7   1
## 63     49     248  9.2   85     7   2
## 64     32     236  9.2   81     7   3
## 65     NA     101 10.9   84     7   4
## 66     64     175  4.6   83     7   5
## 67     40     314 10.9   83     7   6
## 68     77     276  5.1   88     7   7
## 69     97     267  6.3   92     7   8
## 70     97     272  5.7   92     7   9
## 71     85     175  7.4   89     7  10
## 72     NA     139  8.6   82     7  11
## 73     10     264 14.3   73     7  12
## 74     27     175 14.9   81     7  13
## 75     NA     291 14.9   91     7  14
## 76      7      48 14.3   80     7  15
## 77     48     260  6.9   81     7  16
## 78     35     274 10.3   82     7  17
## 79     61     285  6.3   84     7  18
## 80     79     187  5.1   87     7  19
## 81     63     220 11.5   85     7  20
## 82     16       7  6.9   74     7  21
## 83     NA     258  9.7   81     7  22
## 84     NA     295 11.5   82     7  23
## 85     80     294  8.6   86     7  24
## 86    108     223  8.0   85     7  25
## 87     20      81  8.6   82     7  26
## 88     52      82 12.0   86     7  27
## 89     82     213  7.4   88     7  28
## 90     50     275  7.4   86     7  29
## 91     64     253  7.4   83     7  30
## 92     59     254  9.2   81     7  31
## 93     NA     286  8.6   78     6   1
## 94     NA     287  9.7   74     6   2
## 95     NA     242 16.1   67     6   3
## 96     NA     186  9.2   84     6   4
## 97     NA     220  8.6   85     6   5
## 98     NA     264 14.3   79     6   6
## 99     29     127  9.7   82     6   7
## 100    NA     273  6.9   87     6   8
## 101    71     291 13.8   90     6   9
## 102    39     323 11.5   87     6  10
## 103    NA     259 10.9   93     6  11
## 104    NA     250  9.2   92     6  12
## 105    23     148  8.0   82     6  13
## 106    NA     332 13.8   80     6  14
## 107    NA     322 11.5   79     6  15
## 108    21     191 14.9   77     6  16
## 109    37     284 20.7   72     6  17
## 110    20      37  9.2   65     6  18
## 111    12     120 11.5   73     6  19
## 112    13     137 10.3   76     6  20
## 113    NA     150  6.3   77     6  21
## 114    NA      59  1.7   76     6  22
## 115    NA      91  4.6   76     6  23
## 116    NA     250  6.3   76     6  24
## 117    NA     135  8.0   75     6  25
## 118    NA     127  8.0   78     6  26
## 119    NA      47 10.3   73     6  27
## 120    NA      98 11.5   80     6  28
## 121    NA      31 14.9   77     6  29
## 122    NA     138  8.0   83     6  30
## 123    41     190  7.4   67     5   1
## 124    36     118  8.0   72     5   2
## 125    12     149 12.6   74     5   3
## 126    18     313 11.5   62     5   4
## 127    NA      NA 14.3   56     5   5
## 128    28      NA 14.9   66     5   6
## 129    23     299  8.6   65     5   7
## 130    19      99 13.8   59     5   8
## 131     8      19 20.1   61     5   9
## 132    NA     194  8.6   69     5  10
## 133     7      NA  6.9   74     5  11
## 134    16     256  9.7   69     5  12
## 135    11     290  9.2   66     5  13
## 136    14     274 10.9   68     5  14
## 137    18      65 13.2   58     5  15
## 138    14     334 11.5   64     5  16
## 139    34     307 12.0   66     5  17
## 140     6      78 18.4   57     5  18
## 141    30     322 11.5   68     5  19
## 142    11      44  9.7   62     5  20
## 143     1       8  9.7   59     5  21
## 144    11     320 16.6   73     5  22
## 145     4      25  9.7   61     5  23
## 146    32      92 12.0   61     5  24
## 147    NA      66 16.6   57     5  25
## 148    NA     266 14.9   58     5  26
## 149    NA      NA  8.0   57     5  27
## 150    23      13 12.0   67     5  28
## 151    45     252 14.9   81     5  29
## 152   115     223  5.7   79     5  30
## 153    37     279  7.4   76     5  31

Pipe

The pipe operator in R, represented by %>% can be used to chain code together. It is very useful when you are performing several operations on data, and don’t want to save the output at each intermediate step.For example, let’s say we want to remove all the data corresponding to Month = 5, group the data by month, and then find the mean of the temperature each month. The conventional way to write the code for this would be:
filteredData <- filter(airquality, Month != 5)
groupedData <- group_by(filteredData, Month)
summarise(groupedData, mean(Temp, na.rm = TRUE))
## # A tibble: 4 x 2
##   Month `mean(Temp, na.rm = TRUE)`
##   <int>                      <dbl>
## 1     6                       79.1
## 2     7                       83.9
## 3     8                       84.0
## 4     9                       76.9
With piping, the above code can be rewritten as:
airquality %>% 
  filter(Month != 5) %>% 
  group_by(Month) %>% 
  summarise(mean(Temp, na.rm = TRUE))
## # A tibble: 4 x 2
##   Month `mean(Temp, na.rm = TRUE)`
##   <int>                      <dbl>
## 1     6                       79.1
## 2     7                       83.9
## 3     8                       84.0
## 4     9                       76.9
This is a very basic example, and the usefulness may not be very apparent, but as the number of operations/functions perfomed on the data increase, the pipe operator becomes more and more useful!