Working with R

Disclaimer

The information in this guide is collected, and presented to you, to the best of my knowledge and my ability. However, it is certainly not without errors. Also, some things that work fine on my computer (and my operation system, which is Microsoft Windows) may not work on yours. Hence, this guide is offered to you without any warranty .

Basics

R is mainly a command driven software. Most other statistics software nowadays allows users to work with the help of a menu, that is by clicking and pointing. While R also has a menu, this serves mainly for opening or saving files, installing libraries and similar stuff. The basic way of doing statistical analysis is to write commands that are submitted to R.

These commands may be written, and submitted immediately, one by one at the command prompt. But a much better way is to collect your commands in a file, which in the case of R is called a "script". R has its own editor, which may be invoked via "File" (German: "Datei") from the menu. Here you may either open an existing script of start a new one.

Here you can see a "typical" screen with the R console (where output is shown, but which may also be used to submit commands from the prompt), a script (front window) and an image produced by a short series of commands:

R screen with console, script and image

There are also integrated development enviroments like RStudio or R Commander. RStudio, for instance, regularly offers a fourth window that will keep you informed about the objects that are currently available, and it can make working with R easier in a number of other ways. But I will not go into details here.

Libraries

Loading

The procedures offered by R are organized in "libraries" or packages. Some standard packages are loaded whenever R is started. Procedures that are not part and parcel of this "core" of standard procedures have to be loaded via

library(name-of-desired-library)

before they can be used.

Note that you can find out which packages are available (that is, already installed) on your computer. Just type

row.names(installed.packages())

If, however, you want to know which packages are currently loaded, just type

search()

Installing

In addition, many user-written libraries are not even part of the standard distribution of R. These must be downloaded to your computer before they can be used.

In principle, this should be very easy. From the R console, just click on "Packages" and select "Install Package(s)". If you do this the first time during a session, you are asked to pick a server from which to download the desired packages. Now, a list of available packages is shown (this list is tremendously long), and upon clicking on a package this is downloaded as a zip file to your computer and unpacked.

Alternatively, you may just submit the following command:

install.packages("nameOfPackage")

Before the download, you will be prompted to select a server, as described in the previous paragraph.

Sometimes problems arise. It may happen that, for whatever reasons, a selected file cannot be downloaded (it has happened to me, but I'm certainly not the only one). However, it is normally easy to find the pertinent zip file and download it with a web browser. Such a file can be made available by selecting "Install package(s) from local files".

Keeping R up-to-date

I actually updated R only once, so I have little experience. However, it is perhaps not as simple as in the case of other (commercial) packages.

For one thing, you cannot be sure that the libraries you have installed will be avaialable, or will be easily found by by R, after you have installed a new version. So I think it never hurts if you stick to the following rule:

Keep an R script where you collect information about the libraries you have installed.

More specifically, this script in my case is a collection of install commands. In other words, it looks like this:

install.packages("coda")
install.packages("MCMCpack")
...

Now ... how to update R?

Probably there are many ways, but one possibility I found helpful is the installr package. Once you have installed and loaded this package, simply type

updateR()

As I understood the description, all my packages should be available (and updated, if desired) after the updating procedure, but perhaps I made a mistake during the process. Be it as it may, by running my "installation" script described earlier, things turned out fine.

Useful commands to start your work with

Working directory

You can define your working directory via

setwd("Name-of-working-directory")

R will use the working directory to look for files, but also to save output (such as graphs) you may produce. Remember to separate directories and subdirectories either by slashes or by two backslashes.

If you have got lost in your work, you may find out what your current working directory is by typing getwd().

Help

To be honest, for beginners the R help system is not of much use. Still, it's good to know how to access the help system.

If you know which specific R procedure or function you are seeking help for, the command is (very obviously)

help(helpTopic)

or

?helpTopic

This will look for an R element named "help-topic". Note that you may also look for help on the data files that are part of many R packages. (Actually, this may be the most helpful help you're likely to get.) Type help(BJsales) to get information about the BJsales data set.

If, however, you are looking more generally for help on a certain topic, say, univariate statistics, you may try

??helpTopic

This will look for the occurrence of "helpTopic" in the help files. In other words, it will retrieve instancens in which "helpTopic" is not an R function (or annother object on which help is available), but rather simply appears somewhere in a help file. Note that "helpTopic" must be a single word. Thus, you may write ??univariate but not ??univariate statistics.

Vignettes and demos

Some procedures or packages are accompanied by vignettes, i.e., PDF documents that provide more details plus examples. They are usually more helpful than "help".

vignette()

will show all available vignettes.

vignette("rq")

will open the vignette named rq (this one is about quantile regression).

There are also some demos available. Use demo() to find out more (note particularly the hint to the command that will produce a list of all demos of available statistical packages.

© W. Ludwig-Mayerhofer, R Guide | Last update: 19 Jun 2018