Up

First steps in R

  1. First steps are meant to familiarize with the command-line interface.

  2. Do not forget R is case-sensitive: "mean", "Mean" and "mEan" are three different names!!!!

  3. R uses the "." in a name as just a character e.g. "Mean.of.X"

Start up the programme R

  1. double click: on the desktop or

  2. search in the list of "all programmes" on the start menu for R or

  3. double click on the RGUI.exe programme ( can be represented be the same icon in the windows explorer) on ??:/program files/R/RW1051/RGUI.exe

Remark that the number 1051 is the version "1.5.1" ; later or earlier versions have higher or lower numbers...

and after a few seconds, the RGUI starts ( R Graphical User Interface ):

1) Commandline approach 

R is normally used as a "command line" programme. After starting R you will get some not so interesting text in blue ending with:

[Previously saved workspace restored]

>    
  

The >  ( in red ) is called the prompt. After the prompt you can type in your "command".

Type in after the prompt "R is not easy" ( the text will appear in red).

> R is not easy    and enter. R does not agree and answers in blue:

Error: syntax error
>

and a new prompt appears. Of course, this a nonsensical example.

2) Recall of executed commands
 

Now, push the "up arrow". You get your previous commandline: > R is not easy

Statisticians like this a lot and will give a thumbs down to any programme not giving easy access to the history of your analysis. By the menu "/File/Save History" one can save the steps at the end of a worksession.

At the start of a new session we can recall the steps by the menu: "/File/Load History".

With the up arrow and the down arrow we can scroll through former command lines. If we push "enter" we execute the line.

3) Functions and data

R uses an an object-oriented approach in which functions are applied to data.

You can copy/paste the commandline input. Omit the ">" in what you copy/paste.  A simple example:

> x = c(45,43,46,48,51,46,50,47,46,45)
> mean(x)                       # the mean
[1] 46.7
> median(x)                     # the median
[1] 46
> max(x)                        # the maximum or largest value
[1] 51
> min(x)                        # the minimum value
[1] 43
 

We make a vector x of some data (types of data see later). On this vector x we execute the functions "mean, median, max and min".

To make commandlines more digestible we can add comments after "#". Comments have no effect on the execution. We can recall every command up to now by the "up and down arrow".

Help on a function (e.g. mean) can be asked by:

> help(mean)
 A window opens with explanation on the function "mean()"

A strength of R for advanced use is that functions can be executed in a mathematical expression.

An example the standard deviation is:

> sqrt(var(x))
[1] 2.406011

We can make our own expression (this is just a simple example for illustration):

> sqrt( sum( (x - mean(x))^2 /(length(x)-1)))
[1] 2.406011

4) Storing in objects

Some software for statistics is easier (SPSS, Statistica etc) but the R (and S-plus) software is favoured by a lot of professional statisticians because of the object oriented approach.

A very simple example, we can execute a commandline and see the result as a consequence:

> mean(x)                       # the mean
[1] 46.7

We could firstly store the result in a object and show it

> Xav<-mean(x)      # the mean is stored in Xav
> Xav

[1] 46.7
 

R allows to put two commands on one line separated with a semicolom ";" .

> Xav<-mean(x)  ;  Xav
[1] 46.7
>
After executing the first line the result is stored in an object. This object can be used for later analysis. In later applications the value and strength will become clear.

The point is just a character.

> Mean.of.x<-mean(x)  ;   Mean.of.x
[1] 46.7
 

A simple logical test to see if the two variables are the same with two equal signs. "==" .

> Mean.of.x == Xav
[1] TRUE

So, both objects are identical.

Part of what could appear on screen ( red is commandline input; blue is reply by R)


Up

10 May 2005 by Guido Wyseure