You are on page 1of 13

An R Programming

Quick Reference
Share and Enjoy

Basic Data Representation


TRUE, FALSE
1, 2.5, 117.333
1.23e20
3+4i
"hello, world"
NA
NULL
NaN
Inf
-Inf
var

logical true and false


simple numbers
scientific notation, 1.23 1020 .
complex numbers
a character string
missing value (in any type of vector)
missing value indicator in lists
not a number
positive infinity
negative infinity
quotation for special variable name (e.g. +, %*%, etc.)

Creating Vectors
c(e1 , . . . , en )
logical(n)
numeric(n)
complex(n)
character(n)

combine into a vector


logical vector of length n (containing falses)
numeric vector of length n (containing zeros)
complex vector of length n (containing zeros)
character vector of length n (containing empty strings)

Creating Lists
list(e1 , . . . , ek )
vector("list", k)

combine as a list
create a list of length k (the elements are all NULL)

Basic Vector and List Properties


length(x)
mode(x)

the number of elements in x


the mode or type of x

Tests for Types


is.logical(x)
is.numeric(x)
is.complex(x)
is.character(x)
is.list(x)
is.vector(x)

true for logical vectors


true for numeric vectors
true for complex vectors
true for character vectors
true for lists
true for both lists and vectors

Tests for Special Values


is.na(x)
is.nan(x)
is.null(x)
is.finite(x)
is.infinite(x)

true for elements which are NA or NaN


true for elements which are NaN
tests whether x is NULL
true for finite elements (i.e. not NA, NaN, Inf or -Inf)
true for elements equal to Inf or -Inf

Explicit Type Coercion


as.logical(x)
as.numeric(x)
as.complex(x)
as.character(x)
as.list(x)
as.vector(x)
unlist(x)

coerces to a logical vector


coerces to a numeric vector
coerces to a complex vector
coerces to a character vector
coerces to a list
coerces to a vector (lists remain lists)
converts a list to a vector

Vector and List Names


c(n1 =e1 ,. . .,nk =ek )
list(n1 =e1 ,. . .,nk =ek )
names(x)
names(x) = v
names(x) = NULL

combine as a named vector


combine as a named list
extract the names of x
(re)set the names of x to v
remove the names from x

Vector Subsetting
x[1:5]
x[-(1:5)]
x[c(TRUE, FALSE)]
x[c("a", "b")]

select elements by index


exclude elements by index
select elements corresponding to TRUE
select elements by name

List Subsetting
x[1:5]
x[-(1:5)]
x[c(TRUE, FALSE)]
x[c("a", "b")]

extract a sublist of the list x


extract a sublist by excluding elements
extract a sublist with logical subscripts
extract a sublist by name

Extracting Elements from Lists


x[[2]]
x[["a"]]
x$a

extract an element of the list x


extract the element with name "a" from x
extract the element with name name "a" from x

Logical Selection
ifelse(cond, yes, no)
replace(x, list, values)
which(v)

conditionally select elements from yes and no


replace selected values in x with those in values.
returns the indices of TRUE values in v

List Manipulation
lapply(X, FUN, ...)
split(x, f)

apply FUN to the elements of X


split x using the factor f

Sequences and Repetition


a:b
seq(n)
seq(a,b)
seq(a,b,by=s)
seq(a,b,length=n)
seq(along=x)
rep(x,n)
rep(x,v)
rep(x,each=n)

sequence from a to b in steps of size 1


same as 1:n
same as a:b
a to b in steps of size s
sequence of length n from a to b
like 1:length(n), but works when x has zero length
x, repeated n times
elements of x with x[i] repeated v[i] times
elements of x, each repreated n times

Sorting and Ordering


sort(x)
sort(x, decreasing=TRUE)
rev(x)
order(x)

sort into ascending order


sort into descending order
reverse the elements in x
get the ordering permutation for x

Basic Arithmetic Operations


x
x
x
x
x
x
x

+ y
- y
* y
/ y
^ y
%% y
%/% y

addition, x plus y
subtraction, x minus y
multiplication, x times y
division, x divided by y
exponentiation, x raised to power y
remainder, x modulo y
integer division, x divided by y, discard fractional part

Rounding
round(x)
round(x,d)
signif(x,d)
floor(x)
ceiling(x)

round to nearest integer


round x to d decimal places
round x to d significant digits
round down to next lowest integer
round up to next highest integer

Common Mathematical Functions


abs(x)
sqrt(x)
exp(x)
log(x)
log10(x)
log2(x)
log(x,base=b)

absolute values
square root
exponential functiopn
natural logarithms (base e)
common logarithms (base 10)
base 2 logarithms
base b logarithms

Trigonometric and Hyperbolic Functions


sin(x), cos(x), tan(x)
asin(x), acos(x), atan(x)
atan2(x,y)
sinh(x), cosh(x), tanh(x)
asinh(x), acosh(x), atanh(x)

trigonometric functions
inverse trigonometric functions
arc tangent with two arguments
hyperbolic functions
inverse hyperbolic functions

Combinatorics
choose(n, k)
lchoose(n, k)
factorial(x)
lfactorial(x)

binomial coefficients
log binomial coefficients
factorials
log factorials

Special Mathematical Functions


beta(x,y)
lbeta(x,y)
gamma(x)
lgamma(x)
psigamma(x,deriv=0)
digamma(x)
trigamma(x)

the beta function


the log beta function
the gamma function
the log gamma function
the psigamma function
the digamma function
the trigamma function

Bessel Functions
besselI(x,nu)
besselK(x,nu)
besselJ(x,nu)
besselY(x,nu)

Bessel Functions of the first kind


Bessel Functions of the second kind
modified Bessel Functions of the first kind
modified Bessel Functions of the third kind

Special Floating-Point Values


.Machine$double.xmax
.Machine$double.xmin
.Machine$double.eps

largest floating point value (1.797693 10308 )


smallest floating point value (2.225074 10308 )
machine epsilon (2.220446 1016 )

Basic Summaries
sum(x1 ,x2 ,. . .)
prod(x1 ,x2 ,. . .)
min(x1 ,x2 ,. . .)
max(x1 ,x2 ,. . .)
range(x1 ,x2 ,. . .)

sum of values in arguments


product of values in arguments
minimum of values in arguments
maximum of values in arguments
range (minimum and maximum)

Cumulative Summaries
cumsum(x)
cumprod(x)
cummin(x)
cummax(x)

cumulative sum
cumulative product
cumulative minimum
cumulative maximum

Parallel Summaries
pmin(x1 ,x2 ,. . .)
pmax(x1 ,x2 ,. . .)

parallel minimum
parallel maximum

Logical Summaries
all(x)
any(x)

are all of the elements in x true?


are any of the elements in x true?

Statistical Summaries
mean(x)
sd(x)
var(x)
median(x)
quantile(x)
quantile(x, p)

mean of elements
standard deviation of elements
variance of elements
median of elements
median, quartiles and extremes
specified quantiles

Uniform Distribution
runif(n)
runif(n,a,b)
punif(x,a,b)
qunif(x,a,b)
dunif(x,a,b)

vector of n Uniform[0,1] random numbers


vector of n Uniform[a,b] random numbers
distribution function of Uniform[a,b]
inverse distribution function of Uniform[a,b]
density function of Uniform[a,b]

Binomial Distribution
rbinom(n,size,prob)
pbinom(x,size,prob)
qbinom(x,size,prob)
dbinom(x,size,prob)

a vector of n Binomial(size,prob) random numbers


Binomial(size,prob) distribution function
Binomial(size,prob) inverse distribution function
Binomial(size,prob) density function

Normal Distribution
rnorm(n)
pnorm(x)
qnorm(x)
dnorm(x)
rnorm(n,mean,sd)
pnorm(x,mean,sd)
qnorm(x,mean,sd)
dnorm(x,mean,sd)

a vector of n N(0, 1) random numbers


N(0, 1) distribution function
N(0, 1) inverse distribution function
N(0, 1) density function
a vector of n normal random numbers with given mean and s.d.
normal distribution function with given mean and s.d.
normal inverse distribution function with given mean and s.d.
normal density function with given mean and s.d.

Chi-Squared Distribution
rchisq(n,df)
pchisq(x,df)
qchisq(x,df)
dchisq(x,df)

a vector of n 2 random numbers with degrees of freedom df


2 distribution function with degrees of freedom df
2 inverse distribution function with degrees of freedom df
2 density function with degrees of freedom df

t Distribution
rt(n,df)
pt(x,df)
qt(x,df)
dt(x,df)

a vector of n t random numbers with degrees of freedom df


t distribution function with degrees of freedom df
t inverse distribution function with degrees of freedom df
t density function with degrees of freedom df

F Distribution
rf(n,df1,df2)
pf(x,df1,df2)
qf(x,df1,df2)
df(x,df1,df2)

a vector of n F random numbers with degrees of freedom df1 & df2


F distribution function with degrees of freedom df1 & df2
F inverse distribution function with degrees of freedom df1 & df2
F density function with degrees of freedom df1 & df2

Matrices
matrix(x, nr=r, nc=c)
matrix(x, nr=r, nc=c,
byrow=TRUE)

create a matrix from x (column major order)


create a matrix from x (row major order)

Matrix Dimensions
nrow(x)
ncol(x)
dim(x)

number of rows in x
number of columns in x
vector coltaining nrow(x) and ncol(x)

Row and Column Indices


row(x)
col(x)

matrix of row indices for matrix x


matrix of column indices for matrix x

Naming Rows and Columns


rownames(x)
rownames(x) = v
colnames(x)
colnames(x) = v
dimnames(x)
dimnames(x) = list(rn,cn)

get the row names of x


set the row names of x to v
get the column names of x
set the column names of x to v
get both row and column names (in a list)
set both row and column names

Binding Rows and Columns


rbind(v1 ,v2 ,. . .)
cbind(v1 ,v2 ,. . .)
rbind(n1 =v1 ,n2 =v2 ,. . .)
cbind(n2 =v1 ,n2 =v2 ,. . .)

assemble a matrix from rows


assemble a matrix from columns
assemble by rows, specifying row names
assemble by columns, specifying column names

Matrix Subsets
x[i,j]
x[i,j] = v
x[i,]
x[i,] = v
x[,j]
x[,j] = v
x[i]
x[i] = v

submatrix, rows and columns specified by i and j


reset a submatrix, rows and columns specified by i and j
submatrix, contains just the rows a specified by i
reset specified rows of a matrix
submatrix, contains just the columns specified by j
reset specified columns of a matrix
subset as a vector
reset elements (treated as a vector operation)

Matrix Diagonals
diag(A)
diag(v)
diag(n)

extract the diagonal of the matrix A


diagonal matrix with elements in the vector v
the nn identity matrix

Applying Summaries over Rows and Columns


apply(X,1,fun)
apply(X,2,fun)

apply fun to the rows of X


apply fun to the columns of X

Basic Matrix Manipulation


t(A)
A %*% B
outer(u, v)
outer(u, v, f)

matrix transpose
matrix product
outer product of vectors
generalised outer product

Linear Equations
solve(A, b)
solve(A, B)
solve(A)

solve a system of linear equations


same, with multiple right-hand sides
invert the square matrix A

Matrix Decompositions
chol(A)
qr(A)
svd(A)
eigen(A)

the Choleski decomposition


the QR decomposition
the singular-value decomposition
eigenvalues and eigenvectors

Least-Squares Fitting
lsfit(X,y)

least-squares fit with carriers X and response y

Factors and Ordered Factors


factor(x)
factor(x,levels=l)
ordered(x)
is.factor(x)
is.ordered(x)
levels(x)
levels(x) = v

create a factor from the values in x


create a factor with the given level set
create an ordered factor with the given level set
true for factors and ordered factors
true for ordered factors
the levels of a factor or ordered factor
reset the levels of a factor or ordered factor

Tabulation and Cross-Tabulation


table(x)
table( f1 , f2 ,. . .)

tabulate the values in x


cross tabulation of factors

Summary over Factor Levels


tapply(x,f,fun)
tapply(x,list( f1 , f2 ,. . .),fun)

apply summary fun to x broken down by f


apply summary fun to x broken down by several factors

Data Frames
data.frame(n1 =x1 ,n2 =x2 ,. . .)
row.names(df)
row.names(df) = v
names(df)
names(df) = v

create a data frame


extract the observation names from a data frame
(re)set the observation names of a data frame
extract the variable names from a data frame
(re)set the variable names of a data frame

Subsetting and Transforming Data Frames


df[i,j]
df[i,j] = dfv
subset(df,subset=i)
subset(df,select=i)
subset(df,subset=i,select=j)
transform(df,n1 =e1 ,n2 =e2 ,. . .)
merge(df1,df2,. . .)

matrix subsetting of a data frame


reset a subset of a data frame
subset of the cases of a data frame
subset of the variables of a data frame
subset of the cases and variables of a data frame
transform variables in a data frame
merge data frames based on common variables

Matching
match(x, table)
x %in% table
pmatch(x, table)

find matches for x in table


binary function version of match
find partial matches for x in table

Set Operations
union(x, y)
intersect(x, y)
setdiff(x, y)
setequal(x, y)

union
intersection
difference
equality

String Handling
nchar(x)
paste(..., sep = " ", collapse = NULL)
strsplit(x, split)
grep(pattern, x)
grep(pattern, x, value = TRUE)
grepl(pattern, x)
sub(pattern, replacement, x)
gsub(pattern, replacement, x)
substr(x, start, stop)
substring(x, first, last)

compute string length


paste strings together
split x on pattern split
return subscripts of matching elements
return matching elements
do elements match (logical)
replace pattern with given replacement
globally replace
extract substrings
extract substrings

Reading Lines
read a line of input
read n lines from the specified file
read all lines from the specified file

readline(prompt="")
readLines(file, n)
readLines(file)

Reading Vectors and Lists


read a vector or list from a file

scan(file, what = numeric())

Formatting and Printing


format a vector in a common format
formatted printing of R objects
concatenate and print vectors
print an R object

format(x)
sprintf(fmt, ...)
cat(...)
print(x)

Reading Data Frames


read a data frame from a file
read a data frame from a csv file

read.table(file, header=FALSE)
read.csv(file, header=FALSE)

Options for read.table and read.csv


does first line contain variable names?
row names specification
variable names specification
entries indicating NA values
the types associated with columns
the number of rows to be read

header=true/false
row.names=
col.names=
na.strings="NA"
colClasses=NA
nrows=

Writing Data Frames


write a data frame to a file
write a data frame to a csv file

write.table(x, file)
write.csv(x, file)

Writing Data Frames


write a data frame to a file
write a data frame to a csv file

write.table(x, file)
write.csv(x, file)

10

High-Level Graphics
plot(x, y)
plot(x, y, type = "l")
plot(x, y, type = "n")

scatter plot
line plot
empty plot

Adding to Plots
abline(a, b)
abline(h = yvals)
abline(v = xvals)
points(x, y)
lines(x, y)
segments(x0, y0, x1, y1)
arrows(x0, y0, x1, y1, code)
rect(x0, y0, x1, y1)
polygon(x, y)

line in intercept/slope form


horizontal lines
vertical lines
add points
add connected polyline
add disconnected line segments
add arrows
add rectangles
a polygon(s)

Low-Level Graphics
plot.new()
plot.window(xlim, ylim, ...)

start a new plot/figure/panel


set up plot coordinates

Options to plot.window
xaxs="i"
yaxs="i"
asp=1

dont expand x range by 8%


dont expand y range by 8%
equal-scale x and y axes

Graphical Parameters
par(. . . )

set/get graphical parameters

Useful Graphical Parameters


mfrow = c(m,n)
mfcol = c(m,n)
mar=c(m1 ,m2 ,m3 ,m4 )
mai=c(m1 ,m2 ,m3 ,m4 )
cex=m
bg=col

set up an m by n array of figures, filled by row


set up an m by n array of figures, filled by column
set the plot margins (in lines)
set the plot margins (in inches)
set the basic font magnification to m
set the device background to col

Layouts
layout(mat,heights,widths)
layout.show(n)
lcm(x)

set up a layout
show layout elements (up to n)
size specification in cm

11

Compound Expressions
{ expr1 , . . . , exprn }

compound expressions

Alternation
if (cond) expr1 else expr1
if (cond) expr

conditional execution
conditional execution, no alternative

Iteration
for (var in vector) expr
while (cond) expr
repeat expr
continue
break

for loops
while loops
infinite repetition
jump to end of enclosing loop
break out of enclosing loop

Function Definition
function(args) expr
var
var=expr
return(expr)
missing(a)

function definition
function argument with no default
function argument with default value
return the given value from a function
true if argument a was not supplied

Error Handling
stop(message)
warning(message)
on.exit(expr)

terminate a computation with an error message


issue a warning message
save an expression for execution on function return

Language Computation
quote(expr)
substitute(arg)
substitute(expr,subs)

returns the expression expr unevaluated


returns the expression passed as argument arg
make the specified substitutions in the given expression

12

Interpolation
approx(x, y,
spline(x, y,
approxfun(x,
splinefun(x,

xout)
xout)
y, xout)
y, xout)

linear interpolation at xout using x and y


spline interpolation at xout using x and y
interpolating linear function for x and y
interpolating spline for x and y

Root-Finding and Optimisation


polyroot(coef)
uniroot(f,interval)
optimize(f,interval)
optim(x,f)
nlm(f,x)
nlminb(x,f)

roots of polynomial with coefficients in coef


find a root of the function f in the given interval
find an extreme of the function f in the given interval
find an extreme of the function f starting at the point x
an alternative to optim
optimization subject to constraints

Integration
integrate(x,lower,upper)

integrate the function f from lower to upper

13

You might also like