R Tutorial

Training Homepage | Tutorial Main Menu | Using R for Basic Calculations

Section 1: Introduction to R

R is an implementation of the S programming language combined with lexical scoping. This means an unqualified name can be completely determined by looking at the program text. R is a GNU project, which grants users the freedom to take control over their software through sharing, collaboration, and modification. The source code for the R software environment is written primarily in C, FORTRAN, and R itself. R uses a command line interface; however, several graphical user interfaces are available for use such as Rstudio and R Commander.


This is the main menu of the R interface as of Ver. 3.01

Figure 1-1

The R Menu: Button Overview

 Figure 1-2

Below is a list of the buttons from the main interface along with a description of their function:

 Open a script

 Loads a previous workspace

 Saves your current workspace

 Makes a copy of your current workspace


 Copy and paste

 Stops current computation(R will continue to compute most recent code until it is stopped)

 Prints current workspace

Unlike SPSS and STATA, there is little to accomplish through R’s menu bar. R needs to be told what to do using code. Every line of code is a unique function that has a purpose. If you can make yourself familiar with some common syntax, it can be a very powerful tool with statistical analysis. In Figure 1-3 and Figure 1-4 below, you will see examples of a sample script and a complex formulation of code.

Opening Packages/Scripts with R

R can handle several types of data, including numbers, character strings, vectors and matrices, as well as more complex data structures.To open a script in R, under File, select Open Script, then choose the location of the file. In this example we will be importing a comma separated values file which has an extension of .csv. CSV files store tabular data into plain text form, which makes importing from excel much easier. 

Figure 1-3

NOTE: In order for R to properly read and locate the file it MUST be placed in the local directory. To find the directory R is using, use the code getwd(). This code will display the working directory under the line of code as shown in Figure 1-6 below.

Figure 1-4


Packages are add-ons that extend the functionality of software. In R, such packages can be installed using the CRAN(Comprehensive R Archive Network) mirror from the main console. This is a file server which allows you to download new content for R. These pieces of content are organized into packages. To download a package, click the Packages drop-down menu, then select Install package.

Figure 1-5

This displays the list of packages available to download

Figure 1-6  

Choose a mirror(download server) somewhere in the U.S. to ensure quick download speeds, then click ok.

Figure 1-7    

R will then install and unpack each component of the chosen package.

NOTE: After R has been closed it will store the installed packages into the directory. However, when you reload R you MUST select the Packages drop-down menu then choose Load package in order to load the installed package. This needs to be done EACH TIME you open R.                                                                      

Alternative Interfaces 

Although the standard download for R may work for some, there is a nice alternative that may be helpful to others. This interface is known as RStudio; a separate workspace that has an automated syntax fill,(similar to .NET) a cleaner interface and user-friendly formatting. All code that is used with R(currently version 3.02) is also usable with RStudio.

You can download RStudio here: https://www.rstudio.com/ide/download/

This is the R 3.02 interface when compared to RStudio from top to bottom respectively.

Figure 1-8

Figure 1-9

Rcommander is an optional GUI for R to make graphing and importing data sets easier. It is downloaded as an add-on package through R from the CRAN mirror and it MUST be loaded each and every time you start the program if you wish to use it.  

NOTE: When you choose to install Rcommander, you may receive an error message stating there are missing components. Choose Yes to download those missing components and R will install them.

This is the Rcommander interface(appears in a separate window than R)

Figure 1-10

To install Rcommander in R(not Rstudio), go to the Packages drop-down menu, then click Install Package. You will then select a download location, then choose Rmcdr upon clicking OK. To open Rcommander after it has been downloaded, select Packages, then Load Package. 

To install Rcommander for Rstudio, you follow the same steps as with R except you first need to make sure you can view your packages. Rstudio doesn't have Packages as a menu option by default so we will go to Options from the Tools drop-down menu. 

Figure 1-11

From the Options menu select Pane Layout from left. Under the Workspace drop-down, make sure the Packages checkbox is selected, as shown below. 

Figure 1-12

Placing a check mark next to Packages creates a small tab in the workspace called Packages as shown in Figure 1-13 below. Now simply find the package you wish to load, which in this case is Rcmdr, then select the checkbox to open the Rcommander window.

Figure 1-13