Obtaining R

R Basic Cheat Sheet

R is available for Linux, MacOS, and Windows. Software can be downloaded from The Comprehensive R Archive Network (CRAN).

The devtools package makes it easy to build your own R packages, and packages make it easy to share your R code. Supplement this cheat sheet with r-pkgs.had.co.nz, Hadley’s book on package development. R Reference Card by Tom Short, EPRI PEAC, tshort@epri-peac.com 2004-11-07 Granted to the public domain. See www.Rpad.org for the source and latest. R can operate in 2 modes, as an interactive interpreter (the R shell) and as a scripting language, much like Perl or Python. Typically the R shell is used to try things out and the serial commands written in the R language and saved in a file are used to automate operations once the sequence is well-defined and debugged. Rnorm(n,mean,sd) #randomly generate n numbers from a Normal distribution with the specific mean and sd pnorm #find probability (area under curve) of a Normal(10,3^2) distribution to the left #of 8,i.e.


After R is downloaded and installed, simply find and launch R from your Applications folder.

Entering Commands

R is a command line driven program. The user enters commands at the prompt (> by default) and each command is executed one at a time.

R Basics Cheat Sheet Free

The Workspace

The workspace is your current R working environment and includes any user-defined objects (vectors, matrices, data frames, lists, functions). At the end of an R session, the user can save an image of the current workspace that is automatically reloaded the next time R is started.

Graphic User Interfaces

Aside from the built in R console, RStudio is the most popular R code editor, and it interfaces with R for Windows, MacOS, and Linux platforms.

Operators in R

R's binary and logical operators will look very familiar to programmers. Note that binary operators work on vectors and matrices as well as scalars.

Arithmetic Operators include:

^ or ** exponentiation

Logical Operators include:

>greater than
>=greater than or equal to
exactly equal to
!=not equal to

Data Types

R has a wide variety of data types including scalars, vectors (numerical, character, logical), matrices, data frames, and lists.

Creating New Variables

Use the assignment operator <- to create new variables.

# An example of computing the mean with variables
mydata$sum <- mydata$x1 + mydata$x2
mydata$mean <- (mydata$x1 + mydata$x2)/2


Almost everything in R is done through functions. A function is a piece of code written to carry out a specified task; it may accept arguments or parameters (or not) and it may return one or more values (or not!). In R, a function is defined with the construct:

function ( arglist ) {body}

The code in between the curly braces is the body of the function. Note that by using built-in functions, the only thing you need to worry about is how to effectively communicate the correct input arguments (arglist) and manage the return value/s (if any).

Importing Data

Importing data into R is fairly simple. R offers options to import many file types, from CSVs to databases.

For example, this is how to import a CSV into R.


# first row contains variable names, comma is separator
# assign the variable id to row names
# note the / instead of on mswindows systems
mydata <- read.table('c:/mydata.csv', header=TRUE,
sep=',', row.names='id')

Descriptive Statistics

R provides a wide range of functions for obtaining summary statistics. One way to get descriptive statistics is to use the sapply( ) function with a specified summary statistic.


R Basics Cheat Sheet Pdf

Below is how to get the mean with the sapply( ) function:

# get means for variables in data frame mydata
# excluding missing values
sapply(mydata, mean, na.rm=TRUE)

Possible functions used in sapply include mean, sd, var, min, max, median, range, and quantile.

Plotting in R

In R, graphs are typically created interactively. Here is an example:

# Creating a Graph
plot(wt, mpg)
title('Regression of MPG on Weight')

Tyranny download for mac

The plot( ) function opens a graph window and plots weight vs. miles per gallon. The next line of code adds a regression line to this graph. The final line adds a title.


Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library. R comes with a standard set of packages. Others are available for download and installation. Once installed, they have to be loaded into the session to be used.

.libPaths() # get library location
library() # see all packages installed
search() # see packages currently loaded

Getting Help

Once R is installed, there is a comprehensive built-in help system. At the program's command prompt you can use any of the following:

help.start() # general help
help(foo) # help about function foo
?foo # same thing
apropos('foo') # list all functions containing string foo
example(foo) # show an example of function foo

Going Further

If you prefer an online interactive environment to learn R, this free R tutorial by DataCamp is a great way to get started.

vi is one of the most commonly used editor program and included by default with every UNIX and linux system. vi basically operates in 3 modes, namely :

  1. vi mode – the mode vi starts in
  2. command mode – you can be in command mode from the vi mode by pressing the key colon (“:”)
  3. input mode – in this mode user starts the actual editing of the text

Below are some of the most commonly used vi commands in all 3 modes of operation.

vi mode commands

kMove one line upwards
lMove one character to the right
hMove one character to the left
wMove one word to the right
WMove one word to the right past punctuation
bMove one word to the left
BMove one word to the left past punctuation
eMove to the end of the current word
1GMove to the beginning of the file
HMove to the top of the current screen
MMove to the middle of the current screen
LMove to the bottom of the current screen
Ctrl-GMove to the last line in the file
Ctrl-FMove one screen towards the end of the file
Ctrl-DMove 1/2 screen towards the end of the file
Ctrl-BMove one screen towards the beginning of the file
Ctrl-UMove 1/2 screen towards the beginning of the file
Ctrl-LRefresh the screen
5GMove to line 5 of the file (5 can be any line number)
/stringFind text string forward
?stringFind text string backward
nFind forward next string instance after a string search
NFind backward next string instance after a string search
ZZSave the file exit vi
xDelete the character at the cursor
XDelete the character behind the cursor
ddDelete the line the cursor is on
10ddDelete the 10 lines following the cursor
yyYank the current line
pPut the yanked line below the current line
PPut the yanked line above the current line

Command mode commands

:g/X/s//x/gGlobal Search and replace (X=search object x=replace object)
:r fileImport a file into the current file
:34 r fileImport a file into the current file after line 34
:wWrite out the file to save changes
:w fileWrite the file to named file
:wqSave the file exit vi
:w!Force save the file
:q!Quit vi but don’t save changes

Input mode commands

aInsert characters to the right of the cursor
AAppend characters to the current line
iInsert characters to the left of cursor
IInsert characters at the beginning of the current line
oAdd a new line after current line
OInsert a new line above the current line
⇒ ⇒ J201 Fet