Show Menu
Cheatography

R Commands Cheat Sheet (DRAFT) by

Basic R commands used in lecture.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

R as a Calculator

exp(x)
Expone­ntial
sum(x)
Sum.
log(x)
Natural log.
cumsum(x)
Cumulative Sum.
max(x)
Largest element.
ceil(x)
Round up.
min(x)
Smallest element.
floor(x)
Round down.
mean(x)
Mean.
median(x)
Median.
var(x)
Variance.
quanti­le(x)
Percentage quantiles.
x %% y
Modulo

Univariate Data: I/O

write(­data, "­myd­ata.da­t")
Write data as binary.
scan("m­yda­ta.d­at­")
Read binary data.

Univariate Data: Plotting

plot(data)
Plot quick overview.
barplot(x)
Barplot of absolute freque­ncies.
plot.e­cdf­(data)
Plot ECDF.
hist(data, prob=TRUE)
Histogram of relative freque­ncies.
rug(data)
1D-plot
hist(data, breaks=30)
Specify subdiv­isions of histogram.

Creating Vectors

c(2, 4, 6)
Join elements into a vector
2:6
An integer sequence (end inclus­ive!)
seq(2,3, by=0.5)
Complex sequence (s. np.lin­space)
rep(1:2, 3)
Repeat vector
rep(1:2, 3:4)
Repeat each element

Vecotr Functions

sort(x)
Return x sorted.
rev(x)
Return x reversed.
table(x)
See counts of values.
unique(x)
See unique values.
length(x)
Length of x.

Selecting Vector Elements

By Position
x[4]
The fourth element
x[-4]
All but the fourth.
x[2:4]
Elements two to four
x[-(2:4)]
All elements except 2 to four
x[c(1, 5)]
Elements one and five.
By Value
x[x == 10]
All elements equal to 10
x[x  < 10]
All elements less than 10.
x[x %in% c(1, 2, 5)]
Elements in the given set.
Named Vectors
x['apple']
Element with name 'apple'.

Runs Test of Randomness

rle(x)
Compute the lengths and values of runs of equal values in a vector .
rle(x)­$le­ngths
Vector containing the length of each run.
rle(x)­$values
Vector of the same length as lengths with the corres­ponding values.