R Logo

R Logo

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

1 Background

R is open source, and therefore free, statistical software with particular strengths in obtaining, analyzing and visualizing data. R has an admittedly steep learning curve. However, I believe that it is possible to teach R in an accessible way, and that a little bit of R can take you a long way.

This document is a brief introduction to R1, in order to create a kind of “cheat sheet” that can be presented in a few pages. Commands that you actually type into R are represented in courier font. mydata is the name of your data set. x and y and z refer to variables in your data. More documentation on any command is usually available via help(command) or ??command. The R interface makes it extremely easy to do rapid interactive data analysis. Hit “Up-Arrow” to recall the most recent command, which you can then quickly edit and resubmit. Remember also that one often submits a command or set of commands from a script window. The general idea of many R commands2 is:

command(data=mydata, ...variables..., options)

or

command(mydata$xvar, options)

Sometimes, it is not necessary to use any options since some authors of R have done a good job of thinking about the defaults. R can make use of long pathnames3 to files like:

C:/Users/user1/Desktop/mydata.sav

2 Base R and Libraries

Much of this guide makes use of what is most often called Base R, the R that you get when you install the R software, and RStudio, on your computer.

A great deal can be accomplished with Base R. However, as you grow in your use of R, you will likely frequently need to make use of libraries, which are invoked by the library(...) command. Before using a library you need to install it. Below is an example of installing the ggplot2 advanced graphics library.

You would need to install the library only once. Installation can also be accomplished from the “Packages” tab in RStudio.

install.packages("ggplot2") 

Then start the library when you are using R by typing…

library(ggplot2)

3 Working Directory

It is often helpful to simply set your working directory to a particular location and by default, files will be accessed from, and saved to, that directory e.g.:

getwd() # "get", or find out, your working directory

setwd("C:/Users/user1/Desktop/") # set your working directory

4 Writing R Code or Script

R is a command or syntax based program, and many advanced functions are only available via syntax.

R Commands are stored in a script or code file that usually ends in .R, e.g. myRscript.R. The command file is distinct from your actual data, stored in an .RData file, e.g. mydata.RData.

5 Graphical User Interface

Nevertheless a good Graphical User Interface (GUI) makes some of the base functionality of R available without the use of syntax. RCommander is the best GUI, and can be installed from the command line by typing:

install.packages("Rcmdr", dependencies=TRUE)

RCommander can make some tasks easier, but the syntax that it produces can sometimes be cryptic. Often it is easiest (and more in the interests of replicable research) just to learn how to write the R code that accomplishes a particular task.