In this tutorial I give you a quick introduction to the pipe. The pipe has a lot of advantages but here I simply talk about how it works and why your code gets more organised because of it

Here is the link to the datacamp article about the pipe: https://www.datacamp.com/community/tutorials/pipe-r-tutorial

The video:

PS. I made a small mistake in the video. You actually need the data.table package to read in the data with fread. But everything according to the pipe still makes sense.

The code:

setwd("YOURPATHTOYOURWORKINGDIRECTORY")

library(tidyverse)

data <- fread("batting_players_2018.csv")
data <- as_tibble(data)

#Let's filter out the Leauge Average
filter(data, Name != "League Average per 600 PA")

#unique Teams without the pipe
unique(data$Tm)

#average age with the pipe
data$Tm %>% unique()

#filter for the American League
  data %>% filter(Lg == "AL") %>%
  #select Name, Tm, AB, H, BA columsn
  select(Name, Tm, AB, H, BA) %>%
  #calculate BA again
  mutate(avg = H / AB) %>%
  #select second and third column with the . operator
  .[,2:3]

#how the code would look without the pipe
mutate(select(filter(data, Lg == "AL"), Name, Tm, AB, H, BA), 
       avg = H / AB)[, 2:3]