Getting started with R

On this page you’ll learn some of the basics:

  • Getting started with R.
  • How to do basic maths and calculations.
  • How to store results.

Getting started with R – The basics

Once you have installed R and run the program you will see an opening window and a message along these lines:

R : Copyright 2006, The R Foundation for Statistical Computing
Version 2.3.1 (2006-06-01)
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type ‘license()’ or ‘licence()’ for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type ‘contributors()’ for more information and
‘citation()’ on how to cite R or R packages in publications.

Type ‘demo()’ for some demos, ‘help()’ for on-line help, or
‘help.start()’ for an HTML browser interface to help.
Type ‘q()’ to quit R.

[Previously saved workspace restored]

>

The > is the “prompt”, this is the point where you type in commands (or paste them in from somewhere else). The window you see is part of the GUI and some operations are possible from the menus (including quit). You will generally be asked if you wish to save the workspace. R stores a list of commands and any data sets that are loaded. It can be pretty useful to say “yes” and to save the workspace. The command history is available by using the up and down arrows. You can easily scroll back through previous commands and edit them if needed. You can copy items from previous commands or in fact from any window on the screen and paste them into the current command line. You can also use the left and right arrow keys to move through the current command.

Simple maths

You can think of R as like a big calculator. You type in some maths and get the results.

> 1 + 2 * 3
[1] 7

Note that the calculations are carried out in the “standard” manner, with multiplications and divisions before additions or subtractions. Anything in brackets (parentheses) is computed before things outside:

> (1 + 2) * 3
[1] 9

Your result is displayed immediately. Often you will want to store the results to use later, so you need to assign a name to the result.

> result1 = 12 + 17 / 2
> result1
[1] 20.5

Notice that you do not see the result right away, you must type the name to view it. The result can be used for more calculations:

> result2 = result1 * 4 / 7
> result2
[1] 11.71429

Object names

You can use all the “regular” letters of the alphabet a-z (or A-Z) as well as numbers 0-9. R is case sensitive so the following names are different!

result1
Result1
RESULT1

You also use the full stop (period) and underscore characters. You must start a name with a letter and not a number.

Assignment

In the examples here I used = to “define” a result. You will often see something like this <- which looks a bit like an arrow. This is the “preferred” way to assign names to results and you’ll see this in the R help entries and examples.

> name1 = 17 * 41 / 94
> name2 <- 17 * 41 / 94
> name1
[1] 7.414894
> name2
[1] 7.414894
Comments are closed.