Chapter 3 Learning by Using the Generic Scripts

3.1 Tips for Using and Learning R

  1. As you learn R, type out the commands each time, thinking and understanding each component as you go. This is particularly important in the early stages of learning R. Although it is possible to cut/paste the generic commands from the generic commands pages I provide (replacing the names of the variables, data set, etc.), I can’t emphasize enough how valuable it is to do the work of typing the commands each time. It’s an essential part of the process of getting familiar with the R language. As you get more familiar, you may opt to quickly bring up previously typed commands by using the up arrow key on your keyboard. It will flash through your previous commands in R. This is helpful when you discover a small error or wish to edit and re-run a previous line of code.
  2. Be ready and able to troubleshoot! A lot! You will encounter a lot of errors when working with R. Some of these will be the result of something simple, such as forgetting to install and load needed packages, unbalanced parentheses in your code (there are a lot of parentheses!!!), a spelling mistake, failing to reclassify/declare your factor variables prior to executing a particular function, or simply needing to update your packages so that they’re compatible (packages may be dependent on one another and can get out of sync). When you get an error, always start by looking for a simple error. Other errors may be more mysterious. When you run into an error that isn’t quickly resolved, be ready to copy/paste the error message into google along with ‘R’ and see if you can find how others have resolved the issue. A lot of R forums are devoted to community troubleshooting. You may also find it helpful to track down the various sites/documentation relating to your package/function. More often than not, with a little investigation and a bit of triangulation, you can find a way forward.
  3. Start with the familiar and reward yourself. Since R has a relatively steep learning curve, it’s important to develop some momentum in your learning by starting with the familiar. That’s why I’ve developed these generic commands pages for core tests and graphs. Graphs are a particularly rewarding place to start when learning R.

3.2 Using the Generic Scripts

All of the scripts in the ensuing chapters start with a list of packages that should be loaded into the session via the library() function. If the package is not yet installed, you will need to install it. Don’t forget to use quotation marks when installing.

For your convenience, I have included code near the top of every chapter that automatically checks that the required packages are installed, installs them if needed, and loads them into the session.

install.packages("package_name")

Here is a list of the packages used in the various chapters of this book. Note: It is possible that as packages are updated, new dependencies (of one package on another package) may arise. If you get an error message in the course of running a command that says “error: there is no package called ‘packagename,’” then you will need to install that package as well.

install.packages("tidyverse")
install.packages("ggplot2")
install.packages("ggstatsplot")
install.packages("patchwork")
install.packages("effsize")
install.packages("GGally")
install.packages("moments")
install.packages("Rmisc")
install.packages("ggpubr")
install.packages("plotly")
install.packages("car")
install.packages("rgl")
install.packages("sjPlot")
install.packages("jtools")
install.packages("blorr")
install.packages("DescTools")
install.packages("MASS")
install.packages("stargazer")
install.packages("coefplot")
install.packages("dotwhisker")
install.packages("visreg")
install.packages("ggfortify")
install.packages("olsrr")
install.packages("interactions")

When using the generic commands, certain terms and other elements will need to be replaced (they are placeholders for the specifics of your data and preferences). Common words that need replacing (not an exhaustive list) include:

  • object – an object you are creating; call it whatever you want (no spaces)
  • mydata – the name of your dataset; I’ll often clone my original dataset, calling it mydata, for the sake of simplicity.
mydata <- complicated_long_dataset_name
  • object, object1, object2 – you can name your objects anything you want (no spaces); R will overwrite if same name used
  • mydata – name of your dataset
  • var, var1, var2, newvar, oldvar, origvar, depvar, indvar1, indvar2, etc. – variable(s)
  • intvar – interval or continuous variable
  • catvar – categorical or factor variable
  • groupvar – grouping (categorical/factor) variable
  • rowvar – row variable
  • colvar – column variable
  • hashtag (#) – Sometimes just denotes a note; but sometimes is a placeholder for a number
  • label – the label you want to give an axis, etc.

3.3 Generic RScripts

Although I strongly recommend typing out code as part of the learning process, I have placed the consolidated generic code illustrated in each chapter in the final section of each. You are free to copy/paste that code into an empty RScript if you wish. You may also download the generic RScript files. I have linked each of them here and each is linked at the end of its respective chapter. Once downloaded and saved to the desired folder, you can open the RScript by clicking on File – Open File (and browsing for file) in RStudio. Once open, you can click on the magnifying class at the top of the RScript to find/replace any terms, though this process won’t work in every situation (labels, titles, etc.).

In most cases, the generic code is kept pretty simple. What you have to realize, though, is that for any function there are often many edits or “arguments” that you can add or change (e.g., color scheme, features, etc.). You can really only get to know these by finding package/function-related webpages and studying them a bit. You can also get some assistance from cheatsheets that RStudio makes available.