R tutorial
R tutorial
This is a tutorial introduction to R for users with no previous background in the platform or the language.
Contents
The environment
In this section we discuss how to download and install the software, how to configure an R session and what work with the R environment includes.
Installation
- Navigate to http://probability.ca/cran/ [1] and follow the link to your computer's operating system.
- Download a precompiled binary (or "build") of the R "framework" to your computer and follow the instructions for installing it. You don't need tools, or GUI versions for now, but do make sure that the program is the correct one for your version of your operating system.
- Launch R.
The program should open a window–the "R console"–and greet you with its input prompt, awaiting your input:
>
The samples here sometimes copy input/output from the console, and sometimes shows the actual commands only. The <
character at the beginning of the line is always just R's input prompt; It is shown here only to illustrate the interactive use of the program and you do not need to type it. If a line starts with [1]
or similar, this is R's output on the console. Often, I type a #
-character into a command line: this marks the following text as a comment which is not executed by R. In principle, commands can be copied by you and pasted into the console, or into a script - obviously, you don't need to copy the comments. In addition, I use syntax highlighting on R-script, to color language keywords, numbers, strings, etc. different from other text. This improves readability but keep in mind that the colours you see on your computer will be different. One more thing about the console: use your keyboard's up-arrow keys to retrieve previous commands, then enter the line with left-arrow to edit it; hit enter to execute the modified line.
User interface
R comes with a GUI[2] to lay out common tasks. For example, there are a number of menu items, many of which are similar to other programs you will have worked with ("File", "Edit", "Format", "Window", "Help" ...). All of these tasks can also be accessed through the command line. In general, GUIs are useful when you are not sure what you want to do or how to go about it; the command line is much more powerful when you have more experience and know your way around in principle. R gives you both options.
Let's begin with a glossary of some terms that R uses and how they relate to your work:
- Help
- Help is available for all commands and for the R command line syntax. As well, help is available to find the names of commands when you are not sure of them.
> help(rnorm) # "help" is a function, arguments to a function are passed in parentheses "()"
> ?rnorm # shorthand for the same thing
> ?binom # what was the name of that again ... ?
No documentation for 'binom' in specified packages and libraries:
you could try '??binom'
> ??binom
> ?Binomial # ... found it in the list of keywords
>
- Working directory
To locate a file i a computer, one has to specify the filename and the directory in which the file is stored; this is sometimes called the path of the file. The "working directory" for R is either the direcory i which the R-program has been installed, or some other directory, as initialized by a startup script. You can execute the command getwd()
to list what the "Working Directory" is currently set to:
> getwd()
[1] "/Users/steipe/R"
It is convenient to put all your R-input and output files into a project specific directory and then define this to be the "Working Directory". Use the setwd()
command for this. setwd()
requires a parameter in its parentheses: a string with the directory path. Strings in R are delimited with "
or '
characters. If the directory does not exist, an Error will be reported. Make sure you have created the directory. On Mac and Unix systems, the usual shorthand notation for relative paths can be used: ~
for the home directory, .
for the current directory, ..
for the parent of the current directory.
> setwd("~") # my home directory
> getwd()
[1] "/Users/steipe"
> setwd("~/../chen") # relative path: home directory, up one level, then down into chen's home directory
> getwd()
[1] "/Users/chen"
> setwd("/Users/steipe/abc/R_samples") # absolute path: specify the entire string
> getwd()
[1] "Users/steipe/abc/R_samples"
- Create a directory for your sample files and use
setwd()
to set the working directory.
The Working Directory functions can also be accessed thorugh the Menu, under Misc.
- Workspace
During an R session, you might define a large number of variables, datastructures, load packages and scripts etc. All of this information is stored in the so-called "Workspace". When you quit R you have the option to save the Workspace; it will then be reloaded in your next session.
> ls() # list the current workspace contents: initally it is empty
character(0)
> a <- 1; b <-2; eps <- 0.0001 #Initialize three variables (multiple commands on one line can be separated with a semicolon";")
> ls() # list the current workspace contents
[1] "a" "b" "eps"
> rm(a) # remove one item. Note: the parameter is not a string, but a variable name
> ls()
[1] "b" "eps"
> rm(list = ls()) # we can use the output of ls() as input to rm() tomremove everything ... see: ?rm for details
> ls() # once again empty
character(0)
Packages
Standard packages included, data available
Files
... Loading and running scripts
Simple commands
Including functions
Scalar datatypes
Definition, change, operations with, functions to work on...
Vectors
Matrices, tables, frames
Subsetting,mselecting and filtering
Data manipulations
Transformation Search
Writing functions
Installing new functions
Numeric output
Graphic output
Notes
- ↑ This is the CRAN mirror site at the University of Toronto, any other mirror site will do. You may access a choice of mirror sites from the R-project homepage.
- ↑ Graphical User Interface
Further reading and resources