I want your feedback to make the book better for you and other readers. If you find typos, errors, or places where the text may be improved, please let me know. The best ways to provide feedback are by GitHub or hypothes.is annotations.
Opening an issue or submitting a pull request on GitHub: https://github.com/isaactpetersen/Fantasy-Football-Analytics-Textbook
Adding an annotation using hypothes.is. To add an annotation, select some text and then click the symbol on the pop-up menu. To see the annotations of others, click the symbol in the upper right-hand corner of the page.
3 Getting Started with R
for Data Analysis
The book uses R
for statistical analyses (http://www.r-project.org). R is a free software environment; you can download it at no charge here: https://cran.r-project.org.
3.1 Learning R
Here are a various resources for learning R
:
- Intro to
R
: https://www.statmethods.net - Video training courses in
R
skills: https://www.pluralsight.com/search?q=R - Browse the
Cookbook for R
to find solutions to common tasks and problems: http://www.cookbook-r.com - Browse the
R Graph Gallery
to find examples of various graphs: https://r-graph-gallery.com - Free
Codeacademy
course onR
: https://www.codecademy.com/learn/learn-r - Free
Coursera
courses onR
: https://www.coursera.org/search?query=R - Watch these videos from
Coursera
: https://blog.revolutionanalytics.com/2012/12/coursera-videos.html -
Posit
/Rstudio
Webinars: https://posit.co/resources/videos/ - UCLA Stats Website: https://stats.idre.ucla.edu/r/
- Introduction to
R
course onDatacamp
: https://www.datacamp.com/courses/free-introduction-to-r - Teaching
R
in a Kinder, Gentler, More Effective Manner: https://github.com/matloff/TidyverseSkeptic - Learn
R
interactively withswirl
: https://swirlstats.com - Use the
learnr
package: https://rstudio.github.io/learnr/ - Resources for learning
tidyverse
, which is a collection ofR
packages for data management: https://www.tidyverse.org/learn/ - You will sometimes find relevant articles on
R-bloggers
: https://www.r-bloggers.com
3.2 Getting Help with R
If you have R
questions, you can ask them in a number of places:
- Forums:
-
Posit
: https://forum.posit.co -
StackOverflow
: https://stackoverflow.com/questions/tagged/r -
Reddit
: https://www.reddit.com/r/rstats/
-
- The
R
mailing list: https://stat.ethz.ch/mailman/listinfo/r-help
The following article provides additional resources and good guidance: https://www.r-bloggers.com/where-to-get-help-with-your-r-question/.
When posting a question on forums or mailing lists, keep a few things in mind:
- Read the posting guidelines before posting!
- Be respectful of other people and their time.
R
is free software. People are offering their free time to help. They are under no obligation to help you. If you are disrespectful or act like they owe you anything, you will rub people the wrong way and will be less likely to get help. - Provide a minimal, reproducible example. Providing a minimal, reproducible example can be crucial for getting a helpful response. By going to the trouble of creating a minimal, reproducible example and identifying the minimum conditions necessary to reproduce the issue, you will often figure out how to resolve it. Here are guidelines on providing a minimal, reproducible example: https://stackoverflow.com/help/minimal-reproducible-example (archived at https://perma.cc/6NUB-UTYF). Here are a good example and guidelines for providing a minimal, reproducible example in
R
: https://stackoverflow.com/a/5963610 (archived at https://perma.cc/PC9L-DQZG). My strong recommendation is to provide areprex
whenever possible: https://reprex.tidyverse.org.
3.3 Initial Setup
To get started, follow the following steps:
Install
R
: https://cran.r-project.orgInstall
RStudio Desktop
: https://posit.co/download/rstudio-desktop-
After installing
RStudio
, openRStudio
and run the following code in the console to install several keyR
packages: -
Some necessary packages, including the
ffanalytics
package, are hosted in GitHub and need to be installed using the following code (after installing theremotes
package above):
If you are in Dr. Petersen’s class, also perform the following steps:
- Download and install
git
: https://git-scm.com/downloads - Set up a free account on GitHub.com.
- Download and install
GitHub Desktop
: https://desktop.github.com - Make sure you are logged into your GitHub account on GitHub.com.
- Go to the following GitHub repository: https://github.com/isaactpetersen/QuartoBlogFantasyFootball and complete the following steps:
- Click “Use this Template” (in the top right of the screen) > “Create a new repository”
- Make sure the checkbox is selected for the following option: “Include all branches”
- Make sure your Owner account is selected
- Specify the repository name to whatever you want, such as
FantasyFootballBlog
- Type a brief description, such as
Files for my fantasy football blog
- Keep the repository public (this is necessary for generating your blog)
- Select “Create repository”
- After creating the new repository, make sure you are on the page of of your new repository and complete the following steps:
- Click “Settings” (in the top of the screen)
- Click “Actions” (in the left sidebar) > “General”
- Make sure the following are selected:
- “Read and write permissions” (under “Workflow permissions”)
- “Allow GitHub Actions to create and approve pull requests”
- then click “Save”
- Click “Pages” (in the left sidebar)
- Make sure the following are selected:
- “Deploy from a branch” (under “Source”)
- “gh-pages/(root)” (under “Branch”)
- then click “Save”
- Clone the repository to your local computer by clicking “Code” > “Open with GitHub Desktop”, select the folder where you want the repository to be saved on your local computer, and click “Clone”
3.4 Installing Packages
You can install R
packages using the following syntax:
For instance, you can use the following code to install the tidyverse
package:
3.5 Load Packages
3.6 Using Functions and Arguments
You can learn about a particular function and its arguments by entering a question mark before the name of the function:
Below, we provide examples for how to learn about and use functions and arguments, by using the seq()
function as an example. The seq()
function creates a sequence of numbers. To learn about the seq()
function, which creates a sequence of numbers, you can execute the following command:
This is what the documentation shows for the seq()
function in the Usage
section:
Based on this information, we know that the seq()
function takes the following arguments:
from
to
by
length.out
along.with
...
The arguments have default values that are used if the user does not specify values for the arguments. The default values are provided in the Usage
section and are in Table 3.1:
seq()
function. Arguments with a default of NULL
are not used unless a value is provided by the user.
Argument | Default Value for Argument |
---|---|
from |
1 |
to |
1 |
by |
((to - from)/(length.out - 1)) |
length.out |
NULL |
along.with |
NULL |
What each argument represents (i.e., the meaning of from
, to
, by
, etc.) is provided in the Arguments
section of the documentation. You can specify a function and its arguments either by providing values for each argument in the order indicated by the function, or by naming its arguments.
Here is an example of providing values to the arguments in the order indicated by the function, to create a sequence of numbers from 1 to 9:
Here is an example of providing values to the arguments by naming its arguments:
If you provide values to arguments by naming the arguments, you can reorder the arguments and get the same answer:
There are various combinations of arguments that one could use to obtain the same result. For instance, here is code to generate a sequence from 1 to 9 by 2:
Or, alternatively, you could specify the length of the desired sequence (5 values):
If you want to generate a series with decimal values, you could specify a long desired sequence of 81 values:
[1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
[20] 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7
[39] 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6
[58] 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5
[77] 8.6 8.7 8.8 8.9 9.0
This is equivalent to specifying a sequence from 1 to 9 by 0.1:
[1] 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8
[20] 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7
[39] 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6
[58] 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5
[77] 8.6 8.7 8.8 8.9 9.0
Hopefully, that provides an example for how to learn about a particular function, its arguments, and how to use them.
3.7 Create a Vector
A vector is a series of elements that can be numeric or character. It has one dimension (length). To create a vector, use the c()
to combine elements into a vector. And, we use the assignment operator (<-
) to assign the vector to an object named exampleVector
, so we can access it later.
We can then access the contents of the object by calling its name:
3.8 Create a Data Frame
A data frame has two dimensions: rows and columns. Here is an example of creating a data frame, while using the assignment operator (<-
) to assign the data frame to an object so we can access it later:
Code
players <- data.frame(
ID = 1:12,
name = c(
"Ken Cussion",
"Ben Sacked",
"Chuck Downfield",
"Ron Ingback",
"Rhonda Ball",
"Hugo Long",
"Lionel Scrimmage",
"Drew Blood",
"Chase Emdown",
"Justin Time",
"Spike D'Ball",
"Isac Ulooz"),
position = c("QB","QB","QB","RB","RB","WR","WR","WR","WR","TE","TE","LB"),
age = c(40, 30, 24, 20, 18, 23, 27, 32, 26, 23, NA, 37)
)
fantasyPoints <- data.frame(
ID = c(2, 7, 13, 14),
fantasyPoints = c(250, 170, 65, 15)
)
fantasyPoints_weekly <- expand.grid(
ID = 1:12,
season = c(2022, 2023),
week = 1:17
)
set.seed(52242)
fantasyPoints_weekly$fantasyPoints <- sample(
0:35,
size = nrow(fantasyPoints_weekly),
replace = TRUE
)
3.9 Create a List
A list can store multiple data frames in one object:
3.10 Load a Data Frame
Here is how you load a .RData
file using a relative path (i.e., a path relative to the working directory, where the working directory is represented by a period):
The following code loads a file from an absolute path:
Here is how you load a .csv
file:
3.11 Save a Data Frame
Here is how you save a .RData
file using a relative path:
The following code saves a file to an absolute path:
Here is how you save a .csv
file:
3.12 Variable Names
To see the names of variables in a data frame, use the following syntax:
[1] "gsis_id" "status"
[3] "display_name" "first_name"
[5] "last_name" "esb_id"
[7] "birth_date" "college_name"
[9] "position_group" "position"
[11] "jersey_number" "height"
[13] "weight" "years_of_experience"
[15] "team_abbr" "team_seq"
[17] "current_team_id" "football_name"
[19] "entry_year" "rookie_year"
[21] "draft_club" "draft_number"
[23] "college_conference" "status_description_abbr"
[25] "status_short_description" "gsis_it_id"
[27] "short_name" "smart_id"
[29] "headshot" "suffix"
[31] "uniform_number" "draft_round"
[1] "ID" "name" "position" "age"
[1] "ID" "fantasyPoints"
3.13 Logical Operators
3.13.1 Is Equal To: ==
3.13.2 Is Not Equal To: !=
3.13.3 Is Greater Than: >
3.13.4 Is Less Than: <
3.13.5 Is Greater Than or Equal To: >=
3.13.6 Is Less Than or Equal To: <=
3.13.7 Is In a Value of Another Vector: %in%
3.13.8 Is Not In a Value of Another Vector: !(%in%)
3.13.9 Is Missing: is.na()
3.13.10 Is Not Missing: !is.na()
3.13.11 And: &
3.13.12 Or: |
3.14 Piping
In base R
, if you want to perform multiple operations, it is common to either a) nest the operations, or b) save the object at each step.
Below is an example of nested operations:
Below is an example of saving the intermediate object at each step:
[1] "gsis_id" "status"
[3] "display_name" "first_name"
[5] "last_name" "esb_id"
[7] "birth_date" "college_name"
[9] "position_group" "position"
[11] "jersey_number" "height"
[13] "weight" "years_of_experience"
[15] "team_abbr" "team_seq"
[17] "current_team_id" "football_name"
[19] "entry_year" "rookie_year"
[21] "draft_club" "draft_number"
[23] "college_conference" "status_description_abbr"
[25] "status_short_description" "gsis_it_id"
[27] "short_name" "smart_id"
[29] "headshot" "suffix"
[31] "uniform_number" "draft_round"
[1] 32
Code for performing nested operations can be challenging to read. Saving the intermediate object can be a waste of time to do if you are not interested in the intermediate object, and can take up unnecessary memory and computational resources. An alternative approach is to use piping. Piping allows taking the result from one computation and sending it to the next computation, thus allowing a chain of computations without saving the intermediate object at each step.
In base R
, you can perform piping with the |>
expression. In tidyverse
you can perform piping with the %>%
expression.
3.14.0.1 Base R
3.14.0.2 Tidyverse
3.15 Subset
To subset a data frame, use brackets to specify the subset of rows and columns to keep, where the value/vector before the comma specifies the rows to keep, and the value/vector after the comma specifies the columns to keep:
You can subset by using any of the following:
- numeric indices of the rows/columns to keep (or drop)
- names of the rows/columns to keep (or drop)
- values of
TRUE
andFALSE
corresponding to which rows/columns to keep
3.15.1 One Variable
To subset one variable, use the following syntax:
[1] "Ken Cussion" "Ben Sacked" "Chuck Downfield" "Ron Ingback"
[5] "Rhonda Ball" "Hugo Long" "Lionel Scrimmage" "Drew Blood"
[9] "Chase Emdown" "Justin Time" "Spike D'Ball" "Isac Ulooz"
or:
3.15.2 Particular Rows of One Variable
To subset one variable, use the following syntax:
or:
3.15.3 Particular Columns (Variables)
To subset particular columns/variables, use the following syntax:
3.15.3.1 Base R
Or, to drop columns:
3.15.3.2 Tidyverse
Or, to drop columns:
3.15.4 Particular Rows
To subset particular rows, use the following syntax:
3.15.4.1 Base R
3.15.4.2 Tidyverse
3.15.5 Particular Rows and Columns
To subset particular rows and columns, use the following syntax:
3.15.5.1 Base R
3.15.5.2 Tidyverse
3.16 View Data
3.16.1 All Data
To view data, use the following syntax:
3.16.2 First 6 Rows/Elements
To view only the first six rows (if a data frame) or elements (if a vector), use the following syntax:
3.17 Data Characteristics
3.17.1 Data Structure
nflvrs_d [20,751 × 32] (S3: nflverse_data/tbl_df/tbl/data.table/data.frame)
$ gsis_id : chr [1:20751] "00-0004866" "00-0032889" "00-0037845" "00-0039793" ...
$ status : chr [1:20751] "RET" "ACT" "DEV" "ACT" ...
$ display_name : chr [1:20751] "'Omar Ellison" "A'Shawn Robinson" "A.J. Arcuri" "A.J. Barner" ...
$ first_name : chr [1:20751] "'Omar" "A'Shawn" "A.J." "A.J." ...
$ last_name : chr [1:20751] "Ellison" "Robinson" "Arcuri" "Barner" ...
$ esb_id : chr [1:20751] "ELL711319" "ROB367960" "ARC716900" "BAR235889" ...
$ birth_date : chr [1:20751] "1971-10-08" "1995-03-21" "1997-08-13" "2002-05-03" ...
$ college_name : chr [1:20751] NA "Alabama" "Michigan State" "Michigan" ...
$ position_group : chr [1:20751] "WR" "DL" "OL" "TE" ...
$ position : chr [1:20751] "WR" "DT" "T" "TE" ...
$ jersey_number : int [1:20751] 84 94 61 88 24 11 60 6 81 63 ...
$ height : num [1:20751] 73 76 79 78 72 72 75 76 69 76 ...
$ weight : int [1:20751] 200 330 320 251 191 226 325 220 190 280 ...
$ years_of_experience : chr [1:20751] "2" "9" "2" "0" ...
$ team_abbr : chr [1:20751] "LAC" "CAR" "LA" "SEA" ...
$ team_seq : int [1:20751] NA 1 NA NA 1 1 1 1 NA NA ...
$ current_team_id : chr [1:20751] "4400" "0750" "2510" "4600" ...
$ football_name : chr [1:20751] NA "A'Shawn" "A.J." "A.J." ...
$ entry_year : int [1:20751] NA 2016 2022 2024 2013 2019 2015 2019 NA NA ...
$ rookie_year : int [1:20751] NA 2016 2022 2024 2013 2019 2015 2019 NA NA ...
$ draft_club : chr [1:20751] NA "DET" "LA" "SEA" ...
$ draft_number : int [1:20751] NA 46 261 121 NA 51 67 NA NA NA ...
$ college_conference : chr [1:20751] NA "Southeastern Conference" "Big Ten Conference" "Big Ten Conference" ...
$ status_description_abbr : chr [1:20751] NA "A01" "P01" "A01" ...
$ status_short_description: chr [1:20751] NA "Active" "Practice Squad" "Active" ...
$ gsis_it_id : int [1:20751] NA 43335 54726 57242 40688 47834 42410 48335 NA NA ...
$ short_name : chr [1:20751] NA "A.Robinson" "A.Arcuri" "A.Barner" ...
$ smart_id : chr [1:20751] "3200454c-4c71-1319-728e-d49d3d236f8f" "3200524f-4236-7960-bf20-bc060ac0f49c" "32004152-4371-6900-5185-8cdd66b2ad11" "32004241-5223-5889-95d9-0ba3aeeb36ed" ...
$ headshot : chr [1:20751] NA "https://static.www.nfl.com/image/private/f_auto,q_auto/league/qgiwxchd1lmgszfunys8" NA "https://static.www.nfl.com/image/upload/f_auto,q_auto/league/msnzbeyjoemcas9dm8vt" ...
$ suffix : chr [1:20751] NA NA NA NA ...
$ uniform_number : chr [1:20751] NA "94" "61" "88" ...
$ draft_round : chr [1:20751] NA NA NA NA ...
- attr(*, "nflverse_type")= chr "players"
- attr(*, "nflverse_timestamp")= POSIXct[1:1], format: "2024-09-04 02:56:52"
- attr(*, ".internal.selfref")=<externalptr>
3.17.2 Data Dimensions
Number of rows and columns:
Number of rows:
Number of columns:
3.17.3 Number of Elements
3.17.4 Number of Missing Elements
3.17.5 Number of Non-Missing Elements
3.18 Create New Variables
To create a new variable, use the following syntax:
Here is an example of creating a new variable:
3.19 Recode Variables
Here is an example of recoding a variable:
Code
players$oldVar1 <- NA
players$oldVar1[which(players$position == "QB")] <- "quarterback"
players$oldVar1[which(players$position == "RB")] <- "running back"
players$oldVar1[which(players$position == "WR")] <- "wide receiver"
players$oldVar1[which(players$position == "TE")] <- "tight end"
players$oldVar2 <- NA
players$oldVar2[which(players$age < 30)] <- "young"
players$oldVar2[which(players$age >= 30)] <- "old"
Recode multiple variables:
3.20 Rename Variables
Using a vector of variable names:
3.21 Convert the Types of Variables
One variable:
Multiple variables:
3.22 Merging/Joins
3.22.1 Overview
Merging (also called joining) merges two data objects using a shared set of variables called “keys.” The keys are the variable(s) that are used to align the rows from the two objects. The data for the given key(s) in the first object get paired with (i.e., get placed in the same row as) the data for that same key in the second object. In general, each row should have a value on each of the keys; there should be no missingness in the keys. To merge two objects, the key(s) that will be used to match the records must be present in both objects. The keys are used to merge the variables in object 1 (x
) with the variables in object 2 (y
). Different merge types select different rows to merge.
For some data objects, you might want to combine information for the same player from multiple data objects. If each data object is in player
form (i.e., player_id
uniquely identifies each row), you might merge by the player’s identification number (e.g., player_id
). In this case, the key uniquely identifies each row.
However, some data objects have multiple keys. For instance, in long form data objects, each player may have multiple rows corresponding to multiple seasons. In this case, the keys may be player_id
and season
—that is, the data are in player
-season
form. If object 1 and object 2 are both in player
-season
form, we would use player_id
and season
as the keys to merge the two objects. In this case, the keys uniquely identify each row; that is, they account for the levels of nesting.
However, if the data objects are of different form, we would select the keys as the variable(s) that represent the lowest common denominator of variables used to join the data objects that are present in both objects. For instance, assume that object 1 is in player
-season
form. For object 2, each player has multiple rows corresponding to seasons and games/weeks—in this case, object 2 is in player
-season
-week
form. Object 1 does not have the week
variable, so it cannot be used to join the objects. Thus, we would use player_id
and season
as the keys to merge the two objects, because both variables are present in both objects.
It is important not to have rows with duplicate values on the keys. For instance, if there is more than one row with the same player_id
in each object (or multiple rows in object 2 with the same combination of player_id
, season
, and week
), then each row with that player_id
in object 1 gets paired with each row with that player_id
in object 2. The many possible combinations can lead to the resulting object greatly expanding in terms of the number of rows. Thus, you want the keys to uniquely identify each row. In the example below, player
is present in each object, so we can merge by player
; however, each object has multiple rows with the same player. For example, mergeExample1A
has three rows for player
A; mergeExample1B
has two rows for player
A. Thus, when we merge them, the resulting object has many more rows than each respective object (even though neither object has players that the other object does not).
Code
[1] 10 3
Note: if the two objects include variables with the same name (apart from the keys), R
will not know how you want each to appear in the merged object. So, it will add a suffix (e.g., .x
, .y
) to each common variable to indicate which object (i.e., object x
or object y
) the variable came from, where object x
is the first object—i.e., the object to which object y
(the second object) is merged. In general, apart from the keys, you should not include variables with the same name in two objects to be merged. To prevent this, either remove or rename the shared variable in one of the objects, or include the shared variable as a key. However, as described above, you should include it as a key only if you want to use its values to align the rows from each object. Below is an example of merging two objects with the same variable name (i.e., points) that is not used as a key.
Code
When two objects are merged that have different formats, the resulting data object inherits the format of the data object that has more levels of nesting. For instance, consider that you want to merge two objects, object A and object B. Object A is in player
form and object B is in player
-season
-week
form. When you merge them, the resulting data object will be in player
-season
-week
form.
Code
mergeExample3A <- data.frame(
player = c("A","B","C","D","E"),
weight = c(225,250,275,300,325)
)
mergeExample3B <- data.frame(
player = c("A","A","A","A","B","B"),
season = c(2023,2023,2024,2024,2024,2024),
week = c(1,2,1,2,3,4),
points = c(10,15,20,25,30,35)
)
mergeExample3 <- full_join(
mergeExample3A,
mergeExample3B,
by = "player")
mergeExample3
3.22.2 Data Before Merging
Here are the data in the players
object:
The data are structured in ID form. That is, every row in the dataset is uniquely identified by the variable, ID
.
Here are the data in the fantasyPoints
object:
3.22.3 Types of Joins
3.22.3.1 Visual Overview of Join Types
Below is a visual that depicts various types of merges/joins. Object x
is the circle labeled as x
. Object y
is the circle labeled as y
. The area of overlap in the Venn diagram indicates the rows on the keys that are shared between the two objects (e.g., the same player_id
, season
, and week
). The non-overlapping area indicates the rows on the keys that are unique to each object. The shaded blue area indicates which rows (on the keys) are kept in the merged object from each of the two objects, when using each of the merge types. For instance, a left outer join keeps the shared rows and the rows that are unique to object x
, but it drops the rows that are unique to object y
.
3.22.3.2 Full Outer Join
A full outer join includes all rows in x
or y
. It returns columns from x
and y
. Here is how to merge two data frames using a full outer join (i.e., “full join”):
3.22.3.3 Left Outer Join
A left outer join includes all rows in x
. It returns columns from x
and y
. Here is how to merge two data frames using a left outer join (“left join”):
3.22.3.4 Right Outer Join
A right outer join includes all rows in y
. It returns columns from x
and y
. Here is how to merge two data frames using a right outer join (“right join”):
3.22.3.5 Inner Join
An inner join includes all rows that are in both x
and y
. An inner join will return one row of x
for each matching row of y
, and can duplicate values of records on either side (left or right) if x
and y
have more than one matching record. It returns columns from x
and y
. Here is how to merge two data frames using an inner join:
3.22.3.6 Semi Join
A semi join is a filter. A left semi join returns all rows from x
with a match in y
. That is, it filters out records from x
that are not in y
. Unlike an inner join, a left semi join will never duplicate rows of x
, and it includes columns from only x
(not from y
). Here is how to merge two data frames using a left semi join:
3.22.3.7 Anti Join
An anti join is a filter. A left anti join returns all rows from x
without a match in y
. That is, it filters out records from x
that are in y
. It returns columns from only x
(not from y
). Here is how to merge two data frames using a left anti join:
3.22.3.8 Cross Join
A cross join combines each row in x
with each row in y
.
3.23 Transform Data from Long to Wide
Depending on the analysis, it may be important to restructure the data to be in long or wide form. When the data are in wide form, each player has only one row. When the data are in long form, each player has multiple rows—e.g., a row for each game. The data structure is called wide or long form because a dataset in wide form has more columns and fewer rows (i.e., it appears wider and shorter), whereas a dataset in long form has more rows and fewer columns (i.e., it appears narrower and taller).
Here are the original data in long form. The data are structured in “player
-season
-week
form”. That is, every row in the dataset is uniquely identified by the combination of variables, ID
, season
, and week
—these are the keys. This is an example of long form, because each player has multiple rows.
[1] 408 12
[1] "ID" "name" "position" "newVar1"
[5] "newVar2" "factorVar" "numericVar" "integerVar"
[9] "characterVar" "season" "week" "fantasyPoints"
Below, we widen the data by two variables (season
and week
), using tidyverse
, so that the data are now in “player
form” (where each row is uniquely identified by the ID
variable):
Code
[1] 12 43
[1] "ID" "name"
[3] "position" "newVar1"
[5] "newVar2" "factorVar"
[7] "numericVar" "integerVar"
[9] "characterVar" "fantasyPoints_2022_week1"
[11] "fantasyPoints_2023_week1" "fantasyPoints_2022_week2"
[13] "fantasyPoints_2023_week2" "fantasyPoints_2022_week3"
[15] "fantasyPoints_2023_week3" "fantasyPoints_2022_week4"
[17] "fantasyPoints_2023_week4" "fantasyPoints_2022_week5"
[19] "fantasyPoints_2023_week5" "fantasyPoints_2022_week6"
[21] "fantasyPoints_2023_week6" "fantasyPoints_2022_week7"
[23] "fantasyPoints_2023_week7" "fantasyPoints_2022_week8"
[25] "fantasyPoints_2023_week8" "fantasyPoints_2022_week9"
[27] "fantasyPoints_2023_week9" "fantasyPoints_2022_week10"
[29] "fantasyPoints_2023_week10" "fantasyPoints_2022_week11"
[31] "fantasyPoints_2023_week11" "fantasyPoints_2022_week12"
[33] "fantasyPoints_2023_week12" "fantasyPoints_2022_week13"
[35] "fantasyPoints_2023_week13" "fantasyPoints_2022_week14"
[37] "fantasyPoints_2023_week14" "fantasyPoints_2022_week15"
[39] "fantasyPoints_2023_week15" "fantasyPoints_2022_week16"
[41] "fantasyPoints_2023_week16" "fantasyPoints_2022_week17"
[43] "fantasyPoints_2023_week17"
3.24 Transform Data from Wide to Long
Conversely, we can also restructure data from wide to long. Here are the data in long form, after they have been transformed from wide form using tidyverse
:
Code
[1] 408 12
[1] "ID" "name" "position" "newVar1"
[5] "newVar2" "factorVar" "numericVar" "integerVar"
[9] "characterVar" "season" "week" "fantasyPoints"
3.25 Loops
If you want to perform the same computation multiple times, it can be faster to do it in a loop compared to writing out the same computation many times. For instance, here is a loop that runs from 1 to 12 (the number of players in the players
object), incrementing by 1 after each iteration. The loop prints each element of a vector (i.e., the player’s name) and the loop index (i
) that indicates where the loop is in terms of its iterations:
Code
[1] "The loop is at index: 1"
[1] "My favorite player is: Ken Cussion"
[1] "The loop is at index: 2"
[1] "My favorite player is: Ben Sacked"
[1] "The loop is at index: 3"
[1] "My favorite player is: Chuck Downfield"
[1] "The loop is at index: 4"
[1] "My favorite player is: Ron Ingback"
[1] "The loop is at index: 5"
[1] "My favorite player is: Rhonda Ball"
[1] "The loop is at index: 6"
[1] "My favorite player is: Hugo Long"
[1] "The loop is at index: 7"
[1] "My favorite player is: Lionel Scrimmage"
[1] "The loop is at index: 8"
[1] "My favorite player is: Drew Blood"
[1] "The loop is at index: 9"
[1] "My favorite player is: Chase Emdown"
[1] "The loop is at index: 10"
[1] "My favorite player is: Justin Time"
[1] "The loop is at index: 11"
[1] "My favorite player is: Spike D'Ball"
[1] "The loop is at index: 12"
[1] "My favorite player is: Isac Ulooz"
3.26 Session Info
At the end of each chapter in which R
code is used, I provide the session information, which describes the system and operating system the code was run on and the versions of each package. That way, if you get different results from me, you can see which differ, to help with reproducibility. If you run the (all of) the exact same code as is provided in the text, in the exact same order, with the exact same setup (platform, operating system, package versions, etc.), you should get the exact same answer as is in the text. That is the idea of reproducibility—getting the exact same result with the exact same inputs. Reproducibility is crucial for studies to achieve greater confidence in their findings and to ensure better replicability of findings across studies.
R version 4.4.2 (2024-10-31)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 24.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
time zone: UTC
tzcode source: system (glibc)
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] lubridate_1.9.4 forcats_1.0.0 stringr_1.5.1 dplyr_1.1.4
[5] purrr_1.0.2 readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[9] ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] gtable_0.3.6 jsonlite_1.8.9 compiler_4.4.2 tidyselect_1.2.1
[5] scales_1.3.0 yaml_2.3.10 fastmap_1.2.0 R6_2.5.1
[9] generics_0.1.3 knitr_1.49 htmlwidgets_1.6.4 munsell_0.5.1
[13] pillar_1.10.0 tzdb_0.4.0 rlang_1.1.4 stringi_1.8.4
[17] xfun_0.49 timechange_0.3.0 cli_3.6.3 withr_3.0.2
[21] magrittr_2.0.3 digest_0.6.37 grid_4.4.2 hms_1.1.3
[25] lifecycle_1.0.4 vctrs_0.6.5 evaluate_1.0.1 glue_1.8.0
[29] colorspace_2.1-1 rmarkdown_2.29 tools_4.4.2 pkgconfig_2.0.3
[33] htmltools_0.5.8.1