R FirstSteps

Jump to: navigation, search

Online Demonstration

Open your RStudio software and you should see 3 windows titles "Console" (most likely on your left), a window with two tabs ("Environment" and "History") and a third window that has 5 tabs ("Files", "Plots", "Packages", "Help" and "Viewer"). Before we look at these in detail I want you to open a fresh script file. On the menu choose FILE - NEW FILE - R SCRIPT. You should then see a fourth window. We shall soon get back to this.


Working Directory

The first thing you should do is to set the working directory for your code. choose a folder in which you have any data files you may want to use. In my case it is "T:/ECLR/R/FirstSteps". Note that I am using forward slashes, even in a Windows environment:


It will be best to enter this command into the script file which you created above (and which is empty so far. You should also save this script file (e.g. "Firssteps.R"). Once you saved the script file find the little button called "Source" on the top right corner of the Script file window (R sourcebutton.JPG). Press this or alternatively press "CTRL+SHIFT+s" and R will execute all commands in your script file (right now this is only one, setting the working directory).

Creating Variables

Before we shall import data into our environment we shall learn how to define basic variables. Variables are important as we may use them later to save certain results (like t-statistics or standard errors). For starters we will do some silly stuff just to become familiar with some notation.

As we do this just to become familiar with some basic techniques we may do this in the "Console" window directly. In general you will often try a command directly in the console and once you know it works as you wish you may then copy it into your script file in order to save it into eternity (or at least until you loose your USB stick the next time). So try and type the following into the Console (type everything after the prompt sign ">"):

    > test1 = 4

Once you hit ENTER you will find an entry in the "Environment" tab on the right hand side. This tells you that you have just now created a variable, named test1, that has the value 4. If you have done any form of programming, then this sort of command should look very familiar. However, this isn't the way how "things are done" in R. What you should really do to achieve the same is to use the following assignment command:

    > test1 <- 5

I only used the value 5 so that you can see in the environment tab that the value has now changed to 5. Once you have a variable defined you can use it in the definition of further variables as follows:

    > test2 <- test1 + 2.7

But let's stop this silliness and proceed to some more useful activities.


When working with R you will typically want to perform a number of steps to perform your analysis. Perhaps you will first have to import data, then do some data transformation, run a regression, plot some regression residuals and save this plot so that you can include it into a written report. While in menu-driven software packages you would perform these steps by step after step using the menus of the software, the workflow in R will be somewhat different.

You will first provide R with a set of written instructions to achieve all the above steps. Once they are collectively written down in a file, here called a script file, you click one button and upon that click R will execute all these tasks in one fine swoop. One great aspect of this way of working is that you save this script file and it is a record of everything you did to obtain your results. This is an excellent way to communicate your work to colleagues. Even better, you can go back, make a small change in your instructions and then easily replicate your entire analysis.

We will see an example of such a script as we introduce packages just below. Return to the R Start page .