R Command Cheat Sheet Page 2

ADVERTISEMENT

Combining vectors to create matrices
2 1
cbind
Column bind
mat1 <- cbind(v1,v2)
3 3
4 5
2 3 4
rbind
Row bind
mat2 <- rbind(v1,v2)
1 3 5
0 0 0
matrix
Create matrix
matrix(0, nrow=2, ncol=3)
0 0 0
as.data.frame
Create dataset from matrix
A<-as.data.frame(mat1)
2
1
3
3
4
5
Utility functions
[ ]
Subscript operator (Vectors)
answer <- v1[3]
4
[,]
Subscript operator (2D)
answer <- mat1[1,1]
2
answer <- mat1[,1]
2,1
answer <- mat1[1,]
2,3,4
3 3
answer <- mat1[-1,]
4 5
[,,]
Subscript operator (3D)
answer <- arr1[2,4,3]
114
length
Length of vector
length(v4)
9
sort
Sort a vector
sort(v4)
1,1,1,3,3,3,5,5,5
order
Indices to sort a vector
order(v4)
1,4,7,2,5,8,3,6,9
Useful for sorting matrices
v4[v4.order]
1,1,1,3,3,3,5,5,5
rev
Reverse order of vector
rev(v3)
10,8,6,4,2
unique
Lists unique objects in vector or matrix unique(v4)
1,3,5
Statistics
max
Maximum of vector or matrix
max(v4)
5
min
Minimum of vector or matrix
min(mat1)
1
pmax
Parallel maximum of vectors/matrices
pmax(v1,v2)
2,3,5
pmin
Parallel minimum of vectors/matrices
pmin(v1,v2)
1,3,4
mean
Calculates mean of vector or matrix
mean(mat1)
3
median
Calculates median of vector or matrix
median(v3)
6
quantile
Calculate quantiles requested
quantile(1:5,probs=c(0,0.25,0.5,0.75,1))
1,2,3,4,5
var
Calculate variance of vector
var(v3)
10
cor
Calculates correlation of 2 vectors
cor(v4,1:9)
0.3162
Distributions
d<dist>(x,<parameters>) density at x
dunif(1.4,min=1,max=3)
0.5
p<dist>(x,<parameters>) CDF evaluated at x
pnorm(1.645,0,1)
0.95
q<dist>(x,<parameters>) inverse cdf
qnorm(0.95,0,1)
1.645
r<dist>(x,<parameters>) generates n random numbers
rbeta(3, shape1=0.5, shape2=1)
0.175083,0.668609,0.009384
<dist>
Distribution
Parameters
Defaults
beta
Beta
shape1, shape2
-,-
cauchy
Cauchy
location, scale
0,1
chisq
Chi-square
df
-
exp
Exponential
-
-
f
F
df1, df2
-,-
gamma
Gamma
shape
-
lnorm
Log-normal
mean, sd (of
0,1
log)
Logis
Logistic
location, scale
0,1
norm
Normal
mean, sd
0,1
stab
Stable
index, skew
-,0
t
Student’s t
df
-

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3