Skip to content
Mark edited this page Nov 4, 2021 · 13 revisions

R is a programming language used for performing statistical analyses on data sets. It is the primary way we analyze and visualize (graphs, tables) data in our lab. To use R the coding language, you need R installed on your computer. To simplify much of the coding process, we also use R-Studio, which allows us to manipulate, edit, save and load code for a project.

Where to find it/installation

You can download R here. Choose any US location for your installation source. And the free desktop version of R Studio can be found here.

You should not need an activation or installation key for either of these downloads.

Install in this order: R, then R-Studio. After installation, you will only ever need to run R-Studio.

Use

Think of R-Studio as a stripped down version of Windows Explorer (PC) or your Finder (Mac). You'll use R-Studio to open files, modify them, "run" them like a program, etc. The only difference is that it provides tools to make all of this easier, and keep track of changes in case you need to backtrack. One of the biggest differences (and benefits) is that, unlike working with data in Excel or some other spreadsheet program, when you are looking at your data in R Studio, you are never directly modifying the spreadsheet. Instead, all the changes are written, stored, and executed in a separate file. This is called a "source" file. By separating the computations from the raw data, you have maximum control and opportunity to double check work and fix mistakes. You don't have to worry about accidentally applying the wrong changes to your data, or forgetting what changes you applied.

Here is an example of what R-Studio looks like:
In the top left box, is where you will open and/or write code; however, this box does not alter the data set. In the Console box, you can run lines of code which perform analyses on the data set. The boxes on the right display information about your data, terms you’ve assigned values to, packages available to you, and plots/graphs/charts you create. Basic functions

  • Install libraries: The first time you run R, you will want to install libraries you will be using. Libraries are a type of Add-on for a programming language, providing specialized tools for different tasks. If you are working on a team or under a supervisor, ask what libraries will be required. Below are common libraries for conducting language science research and statistics.
install.packages("lmerTest")  
install.packages("lme4")  
install.packages("psych")  
install.packages("tidyverse")  
install.packages("lmSupport")  
install.packages("tidytext")

You should only need to do this once, (unless you need to troubleshoot individual packages).
When you open R-Studio, you are starting a "session." At the beginning of each session, you need to activate the libraries you will be using.

library(lme4)  
library(psych)  
library(lmerTest)  
library(ggplot2)  
library(lmSupport)  
  • Read in data set: Once your libraries are activated, you'll want to load the data that you would like to work with. Usually this is a spreadsheet. Reading this into R will look roughly similar to this.
exp_data <- read_csv(file = "C:\\Users\\USERNAME\\FOLDER\\CSV-NAME.csv")  
# This tells the program where your data set is, what it’s called,  
# and that it has columns that have names.

or this

d = load('datastructure.rda')
# Assumes the file is in the working directory (same folder).
  • Annotations: Any text which follows a # in R is considered annotation—i.e., notes to yourself within a script that don’t “do anything”. This is helpful for remembering what specific commands do, or asking questions someone else may later help with.
    Annotated text appears green in R Studio.
exp <- read.csv(file = exp.path, header = T) # this reads in the data file.

Common issues

  • File type: Your data set needs to be in CSV (comma separated values) format. You can convert from an .xls file to a .csv using the Excel Save As function.
  • Syntax: R syntax may be a bit strange if you're used to other coding languages. Here is an article going over some of these nuances.

Resources:

  • lynda.com has a number of tutorials. They're free as a UW student through my.wisc.edu. (Lynda is now on LinkedIn, but still free.) Search for this series: "Learning R". Chapter 1 and 2 are probably enough to get started.

  • once you've got those basics down, check out "Data Wrangling in R".

  • An extremely thorough, and remarkably straightforward resource is r4ds

  • Cheat sheets: There are many cheat sheets here. This reference card is also helpful, but use it only as a back-up, since it relies on an "older" and more awkward style of coding.

  • YouTube tutorials: How to R
    A playlist of R-Studio tutorials

Clone this wiki locally