An R Programming Quick Reference: Basic Data Representation
An R Programming Quick Reference: Basic Data Representation
Quick Reference
Share and Enjoy
Creating Vectors
c(e1 , . . . , en )
logical(n)
numeric(n)
complex(n)
character(n)
Creating Lists
list(e1 , . . . , ek )
vector("list", k)
combine as a list
create a list of length k (the elements are all NULL)
Vector Subsetting
x[1:5]
x[-(1:5)]
x[c(TRUE, FALSE)]
x[c("a", "b")]
List Subsetting
x[1:5]
x[-(1:5)]
x[c(TRUE, FALSE)]
x[c("a", "b")]
Logical Selection
ifelse(cond, yes, no)
replace(x, list, values)
which(v)
List Manipulation
lapply(X, FUN, ...)
split(x, f)
+ 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)
absolute values
square root
exponential functiopn
natural logarithms (base e)
common logarithms (base 10)
base 2 logarithms
base b logarithms
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
Bessel Functions
besselI(x,nu)
besselK(x,nu)
besselJ(x,nu)
besselY(x,nu)
Basic Summaries
sum(x1 ,x2 ,. . .)
prod(x1 ,x2 ,. . .)
min(x1 ,x2 ,. . .)
max(x1 ,x2 ,. . .)
range(x1 ,x2 ,. . .)
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)
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)
Binomial Distribution
rbinom(n,size,prob)
pbinom(x,size,prob)
qbinom(x,size,prob)
dbinom(x,size,prob)
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)
Chi-Squared Distribution
rchisq(n,df)
pchisq(x,df)
qchisq(x,df)
dchisq(x,df)
t Distribution
rt(n,df)
pt(x,df)
qt(x,df)
dt(x,df)
F Distribution
rf(n,df1,df2)
pf(x,df1,df2)
qf(x,df1,df2)
df(x,df1,df2)
Matrices
matrix(x, nr=r, nc=c)
matrix(x, nr=r, nc=c,
byrow=TRUE)
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)
Matrix Subsets
x[i,j]
x[i,j] = v
x[i,]
x[i,] = v
x[,j]
x[,j] = v
x[i]
x[i] = v
Matrix Diagonals
diag(A)
diag(v)
diag(n)
matrix transpose
matrix product
outer product of vectors
generalised outer product
Linear Equations
solve(A, b)
solve(A, B)
solve(A)
Matrix Decompositions
chol(A)
qr(A)
svd(A)
eigen(A)
Least-Squares Fitting
lsfit(X,y)
Data Frames
data.frame(n1 =x1 ,n2 =x2 ,. . .)
row.names(df)
row.names(df) = v
names(df)
names(df) = v
Matching
match(x, table)
x %in% table
pmatch(x, 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)
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)
format(x)
sprintf(fmt, ...)
cat(...)
print(x)
read.table(file, header=FALSE)
read.csv(file, header=FALSE)
header=true/false
row.names=
col.names=
na.strings="NA"
colClasses=NA
nrows=
write.table(x, file)
write.csv(x, 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)
Low-Level Graphics
plot.new()
plot.window(xlim, ylim, ...)
Options to plot.window
xaxs="i"
yaxs="i"
asp=1
Graphical Parameters
par(. . . )
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)
Language Computation
quote(expr)
substitute(arg)
substitute(expr,subs)
12
Interpolation
approx(x, y,
spline(x, y,
approxfun(x,
splinefun(x,
xout)
xout)
y, xout)
y, xout)
Integration
integrate(x,lower,upper)
13