Introductory remarks

The aim of this document is to provide basic information to help you start working with R. Further documents will be available to introduce specific analysis and other tasks. Note that the statistical functions used in this introduction are only meant to explain how R works and are purely illustrative. You can find some general information on R here. R powerful programming language and environment for statistics with a huge library of packages; these documents provide just a short introduction for courses using R for data exploration and visualization. More information is never far away (on-line help, internet) and many print on R are available.

This document describes the standard user interface for R (command windows). Many other users interfaces/environments, namely graphical user interfaces (GUI) are available, some are developments projects, others are mature and used/supported by large user communities or established companies. Noteworthy user interfaces are:

Important:

Introduction

The screen shot is from the Microsoft windows version; on Mac or Linux the presentation is somewhat different. On Macs the menu system shows different items in different places. You can also a different user interface when you use for instance JGR or other GUIs (GUI=Graphical User Interface) may add to and change the menu system. But no matter what system or add-on you are using, the R functions that are presented here will work in the console window. When you launch R, you will this screen. RGUI, the standard R user interface, is a simple interface to the R language, with some menus and toolbars, as well as a number of windows; when you start R, the Console window is displayed. The available menus (and toolbars) are limited to general housekeeping tasks; for analytical task you will have to type R commands (functions), e.g.. to produce a histogram of a variable named urb you will have to type hist(urb) at the command prompt (>). in the console window:

Vocabulary: hist(urb); hist() is a function producing a histogram, and urb is an object (in this case a particular column of a data matrix), a variable.

Note also that everything that is available in the menu is also available through functions you type in.

Data

Before performing any analysis, your need some data, i.e. the above hist(urb) command needs urb to be available.

You can find the example data in a workspace called "world". Access it by typing (in the console window):

load(url("http://www.unige.ch/ses/sococ/cl/edat/world.Rdata"))

Hint: Use copy-Paste to paste the command into the console window. (Do not bother about details, just do it!).

Basics

For you now type hist(urb) you will get an error message: (Error: object 'urb' not found), as urb is not an object, but just a column in the data frame labelled urb. How can we access the column containing urbanization?

The easiest ways is to use the attach function.

In fact there are several ways of accessing that data, let us have a look at the various possibilities (Beginners: this is a bit complicated, but later you will need this information); for the moment just take note and come back later when you have some more experience with R.)

Information about rows and columns in data frames:

Some simple commands (examples)
median(urb) Computes the median of urb and displays it
med=median(urb) Stores the result into med
mean(log(urb)) Computes and displays the mean of the logarithms of urb.
xx=mean(urb)-median(urb) Stores the difference between the median and mean into xx
24+5*22 Evaluates the expression and displays the result.
stem(urb) Produces a stem and leaf plot.
R menus

As already said, the menus and toolbars provide some general functions, mainly for setting up R and access data. Below you find a short descriptions of the menu items that might be useful in the context of this learning site. Please note that all menu items, correspond to a particular function that can also be typed in using the R command line.

File menu

Your are most likely to use Load workspace... (choose a workspace to load (read) and Save workspace... (saving the current workspace to disk to be reused later). Note that Load workspace corresponds to the load() function we have met earlier when we used load(url("http://www.unige.ch/ses/sococ/cl/edat/world.Rdata")) to access the world data frame. Note that the menu Load workspace is limited to access a file on your local disk.

menus

These two menus contain mostly self-explanatory items, like the Copy-Cut-Paste operations, setting options and preferences, as well as control the display of tool and status bars.

R packages

R implements de S programming language for statistics, and most of the functions you are using are written in that language; if for instance you type stem(urb) you execute a function named stem() (By the way: if you just type stem without any arguments you will display the program code for the function).

Functions are stored in packages; many packages are preinstalled and activated when you install and run R. Some packages need to be loaded (activated) before using them. Given the extensible nature of R a large community programs in R and distributes the result you usually will find in some repository on the internet; these libraries need to be downloaded and activated before use.

The menu lets you perform a number of tasks related to libraries, namely:

Given the architecture of R, you should be aware that libraries you install my modify or completely replace other functions that are currently active, so do not be astonished, when some functions provide different looking results after you have loaded an additional library.

Note that a library of package is a collection of objects, containing of course functions, but also data and documentation.

Graphical windows

This screenshot shows two windows.

  1. The console window used to type in command lines and to receive textual output, as well as error messages.
  2. Graphical functions produce additional windows. In our example the hist(urb) command line has produce the graph. Note that each graphical function produces, by default, a separate window.

You can see, by inspecting the console windows, the commands that have been used.

  1. help(hist) has opened your default browser with the documentation on the hist function (not visible on the screenshot)
  2. hist(world) has produced an error message, because world is a data frame, and the hist function only accepts a single variable (vector); typing hist(urb) produced as well an error as urb could not be found, it works however after typingattach(world) and then produces a histogram shown in a graphic windows.

Whenever a graphics window is active (selected) the File menu offers a series of options to save the graphic to a file for further use.

Related documents