install.packages(c("rio", "ggplot2", "psych","correlation",
"GPArotation", "lavaan", "MBESS",
"semTools", "lavaan.mi"),
dependencies = TRUE)1 Getting Started
Before you can successfully complete the R Labs included on this page, you will need to install some software and some packages within that software. This first Lab will help you do so.
1.1 Installing New Software
As the title of this page suggests, all labs will be done using R (and RStudio). To use these programs, you’ll need to install both R and RStudio. Follow the instructions below to install them.
Step 1: Install R
R is a programming language and computing environment specialized for statistical analysis and data manipulation. It’s commonly used for performing statistical tests, creating data visualizations, and writing data analysis reports.
Installing R for Windows Computers
Go to https://cloud.r-project.org/bin/windows/base/ and click the link titled Download R-4.3.2 for Windows (note: the version number might be different, but the remainder of the link will be the same). This will download the R Installer into your Downloads folder, where you can double click on it and follow the prompts on the screen to finish installing R. You can accepts all default settings.
Installing R for Mac Computers
You will need to figure out if you have an Intel Processor or an Apple M Processor. You can do so by clicking on the Apple icon in the top-left corner of your screen and clicking on About this Mac. The window that will pop up will show you an overview of your computer, including the processor/chip used.
Once you know what processor your computer has, go to https://cloud.r-project.org/bin/macosx/, and:
- If your computer has an Intel Processor, click on the file titled R-4.3.2-x86_64.pkg
- If your computer has an Apple M Processor, click on the file titled R-4.3.2-arm64.pkg
Note: the version number might be different, but the remainder of the link will be the same. This will download the R Installer into your Downloads folder, where you can double click on it and follow the prompts on the screen to finish installing R. You can accepts all default settings.
Installing R for Linux Computers
If you are using a Linux-based operating system, use your system’s package manager to install R. For example, here are the instructions for installing R on Ubuntu.
R cannot be installed on Chromebooks, so you’ll need to use the computers available in the classroom/computer labs.
Step 2: Install RStudio
RStudio is an integrated development environment (IDE) for reproducible scientific computing that is developed for the R programming language. An IDE is basically a nicer-looking user interface that can be customized to suit the preferences of the user. This is the actual program that we will use in class!
- Download the latest, free version of RStudio Desktop. Be sure to get the version that is appropriate for your operating system.
- Install RStudio Desktop by launching the installer after it downloads. You can accept all the defaults during installation.
For more detailed instructions for downloading and installing R and RStudio, you can watch this video tutorial on YouTube. To learn about (or review) R basics, you can skim this (free!) book by Navarro (2015): Learning Statistics with R. There is also the SWIRL Interactive R Tutorial that lets you learn about the basics of R while using R.
1.2 Install Necessary Packages
Throughout these labs, we will rely on a set of R packages, which add functionality to the base R language (like expansion sets of a game). These packages are typically available through CRAN or GitHub. You only need to install packages once (but you may need to update them!), so lets do that now.
We will start with a set of packages that we can download from CRAN, using the built-in install.packages function:
Running the code above will install:
rio: makes importing lots of different data file types easy.ggplot2: a versatile visualization package.psych: will help us cover topics such as exploratory factor analysis and reliability.correlation: includes fancy correlation coeficientsGPArotation: helps with exploratory factor analysislavaan: the main structural equation modeling package we will use to cover confirmatory factor analysis and measurement invariance.MBESS: includes additional internal consistency measuressemTools: a package that extends the functionality of thelavaanpackagelavaan.mi: a package that is needed forsemToolsto work properly
By using dependencies = TRUE, the above code will also install any packages that these 9 packages themselves depend on to work properly.
If you experience issues installing the MBESS package on macOS, you likely need to install a few additional tools. Go to this page to download and install those tools: Compile Tools for macOS.
1.3 Data Used in the R Labs
Several of the R Labs require you to download data files to use for the analyses. Links to these data files are included within each lab, accompanied by an explanation and citation.
1.4 Basic R Operations
Loading R packages
Although you only need to install R packages once (until the next time you update R), you need to let R know that you want to use a package each time you open RStudio. You can use the library() function to do so (think of it as borrowing a book from the library so that you have access to the knowledge within). The code below loads the rio package into your R environment.
library(rio)Loading Data
Typically, you will import some data file into your R environment for further analysis. There are many ways of doing this. I will show you two:
- You can use a point-and-click approach by clicking the
Import Datasetbutton in the right-top window. - You can use a function (the one we use is from the
riopackage).
tempice <- import(file = "data/tempice.csv")The function above will attempt to import the file tempice.csv from a folder called data, which is located inside your working directory.
Sometimes, running the code above doesn’t work because R thinks you want to import the data from the wrong folder (which R calls the working directory). We can check what the working directory is:
getwd()If the result of this function is not the folder containing your data file, then you can change the working directory in two ways:
- Use a point-and-click approach by moving your cursor to the bottom-right window to navigate to the correct folder (in the Files tab).
- Use the following R function to change the working directory:
# Mac OS:
setwd("~/Dropbox/Work/Teaching/Measurement/R Labs")
# Windows:
setwd("C:/Users/sonja/Dropbox/Work/Teaching/Measurement/R Labs")
# Note: the folder that you are using for this class will very
# likely be in a different location. Typically, R/RStudio will set the working directory to the folder containing the R file you open. If you start RStudio by itself (instead of opening a file), then the working directory will typically be set to your home folder.
Manipulating data
Instead of importing data, you can also create your own objects in R by assigning them one or more values. Here is an example:
x <- 11
x[1] 11
x + 1[1] 12
In the chunk above, I assigned the value 11 to the object x. When you run the line with the name of the object, R will print the content of the object in the Console. You can also use an object in a later computation (as shown above when I add 1 to x).
There are also functions in R that help us execute more complicated tasks. One example of such a function that is used a lot is the summary function:
summary(tempice) x1 x2
Min. :63.42 Min. :185.0
1st Qu.:70.98 1st Qu.:330.2
Median :74.94 Median :410.0
Mean :75.61 Mean :402.4
3rd Qu.:82.00 3rd Qu.:464.2
Max. :87.18 Max. :614.0
This function outputs some summary information (e.g., min, max, mean, median) about the variables/columns in the object you supply between the parentheses.
You are now ready to continue to the second R Lab, where you will learn more about basic R operations and all about correlation coefficients.