A.3 Pipe Operator

Instead of using filter in this way, we’ll typically use the pipe operator %>%, which is a nice way to string together many operations in a readable way. These two lines of code give the same result.

Code
temp = filter(d, lg == 'nba' & season == 2022)
temp = d %>% filter(lg == 'nba' & season == 2022) ## same

The way to read this is that the object, often a data frame, to the left of the pipe (in this case, d) is piped into the first argument of the function to the right of the pipe (filter). The result of filter can them be piped into another function, say head.

Code
temp = d %>% 
  filter(lg == 'nba' & season == 2022) %>%
  head()
temp  
        date away home ascore hscore  lg season season.type      gid
1 2021-10-19  BKN  MIL    104    127 nba   2022         reg 22100001
2 2021-10-19  GSW  LAL    121    114 nba   2022         reg 22100002
3 2021-10-20  OKC  UTA     86    107 nba   2022         reg 22100011
4 2021-10-20  SAC  POR    124    121 nba   2022         reg 22100013
5 2021-10-20  DEN  PHX    110     98 nba   2022         reg 22100012
6 2021-10-20  ORL  SAS     97    123 nba   2022         reg 22100010
                    gkey
1 nba.2021-10-19.BKNvMIL
2 nba.2021-10-19.GSWvLAL
3 nba.2021-10-20.OKCvUTA
4 nba.2021-10-20.SACvPOR
5 nba.2021-10-20.DENvPHX
6 nba.2021-10-20.ORLvSAS

Note that after every %>% we put a new line, which helps improve readability. The usefulness of pipe will become more apparent as we string together more operations.

We will also often put new arguments or logical expressions on a new line. If it’s not too crazy, we’ll sometimes line up the =, ==, ,, etc., for readability. Below, as an example, we put season == 2022 on it’s own line and add extra spaces to lg == 'nba' to line up the == in the two lines of code.

Code
temp = d %>% 
  filter(lg     == 'nba' & 
         season == 2022) %>%
  head()
temp  
        date away home ascore hscore  lg season season.type      gid
1 2021-10-19  BKN  MIL    104    127 nba   2022         reg 22100001
2 2021-10-19  GSW  LAL    121    114 nba   2022         reg 22100002
3 2021-10-20  OKC  UTA     86    107 nba   2022         reg 22100011
4 2021-10-20  SAC  POR    124    121 nba   2022         reg 22100013
5 2021-10-20  DEN  PHX    110     98 nba   2022         reg 22100012
6 2021-10-20  ORL  SAS     97    123 nba   2022         reg 22100010
                    gkey
1 nba.2021-10-19.BKNvMIL
2 nba.2021-10-19.GSWvLAL
3 nba.2021-10-20.OKCvUTA
4 nba.2021-10-20.SACvPOR
5 nba.2021-10-20.DENvPHX
6 nba.2021-10-20.ORLvSAS

One final note: in filter we can write , instead of &, which speeds up typing.

Code
temp = d %>% filter(lg == 'nba' & season == 2022)
temp = d %>% filter(lg == 'nba',  season == 2022) ## same

In this case, since we wanted to compare those two very similar lines, we didn’t add a new line after pipe or a new line for season == 2022.