Skip to content
Mark edited this page May 7, 2018 · 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. As of Spring 2017 R is not installed on the RA office or running room computers.

Install them in this order: R, then R-Studio. After installation, run R-Studio for everything

Use

It is easiest to use R-Studio to run R. Here is an example of what R-Studio looks like:
In the top left box, you can open 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 any libraries you will be using.
install.packages("lmerTest")  
install.packages("lme4")  
install.packages("psych")  
install.packages("ggplot2")  
install.packages("lmSupport")  

You only need to do this once, ever (unless you need to troubleshoot individual packages).

  • Activate libraries: 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: You need to tell R what data set (i.e. .CSV file) you will be analyzing. Reading this into R will look roughly similar to this
exp_data <- read.csv(file = "C:\\Users\\USERNAME\\FOLDER\\CSV-NAME.csv", header = T)  
# 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.
  • 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 (search lynda.com). At lynda.com search for this series "Learning R". Chapter 1 and 2 are probably good.
  • once you've got those basics down, check out "Data Wrangling in R" on lynda.com.
  • Cheat sheets: There are many cheat sheets here. This reference card is also helpful (though not very glamorous.)
  • YouTube tutorials: How to R
    A playlist of R-Studio tutorials
  • Use the Slack #debug channel to post questions to see if we can help.
  • We are thinking of creating an LCNL R tutorial! Stay posted.

Clone this wiki locally