Basics
q() |
Quit |
<- |
Assign |
m[,2] |
Column 2 of matrix m |
m[,2:5] or m[,c(2,3,4,5)] |
Columns 2-5 |
NA |
Missing data |
require(package_name) |
Load a package |
|
Control flow
for (i1 in vector1) |
Repeat what follows |
if (condition1) ... else ... |
Conditional |
|
Help
help(command) |
Quick help for a command |
help.start() |
Start browser help |
help(package=package_name) |
Help with a package |
|
Arithmetic
%*% |
Matrix multiplication |
%/%, ^, %%, sqrt() |
Integer division, power, modulus,square root |
|
Input and Output
source("file1") |
Run the commands in a file |
read.table("file1") |
Read in data from a file |
scan("file1") |
Read in a file as vector |
write(object,"file1") |
Writes an R object to a file |
write.table(data.frame,"file1") |
Writes a table to a file |
|
Graphics
plot(), barplot(), boxplot(), stem(), hist() |
Basic plots |
matplot() |
Matrix plot |
pairs(matrix1) |
Scatterplots |
qqplot() |
Quantile-quantile plot |
qqnorm(), qqline() |
Fit normal distribution |
|
Managing Variables and Objects
ls() |
List all active objects |
rm(object1) |
Remove an object |
dim(matrix1) |
Dimensions of a matrix |
dimnames(matrix1) |
Names of dimensions of a matrix |
length(vector1) |
Length of vector1 |
1:3 |
Vector (1,2,3) |
c(1,2,3) |
Same vector (1,2,3) |
rep(x,n) |
Repeat vector x, n times |
cbind(a1,b1,c1), rbind(a1,b1,c1) |
binds columns or rows into a matrix |
merge(df1,df2) |
Merge data frames |
cbind(x1,x2) |
Bind vectors x1 and x2 as columns of a matrix |
rbind(x1,x2) |
Bind vectors x1 and x2 as rows of a matrix |
merge(df1,df2) |
Merge data frames |
matrix(vector1,r1,c1) |
Make vector1 into a matrix with r1 rows and c1 columns |
data.frame(v1,v2) |
Make a data frame from vectors v1 and v2 |
as.factor(), as.matrix(), as.vector() |
Conversion |
is.factor(), is.matrix(), is.vector() |
What it is |
t() |
Switch rows and columns |
which(x1==a1) |
Returns indices of x1 where x1==a1 |
|
Statistics
max(), min(), mean(), median(), sum()}, var() |
as named |
summary(data.frame) |
Prints statistics |
rank(), sort() |
as named |
ave(x1,y1) |
Averages of x1 grouped by factor y1 |
by() |
Apply function to data frame by factor |
apply(x1,n1,function1) |
Apply function1 on rows (n1=1) or columns (n1=2) of x1 |
tapply(x1,list1,function1) |
Apply function1 to x1 by list1 |
table() |
Make a table |
tabulate() |
Tabulate a vector |
t.test() |
t-test |
aov(), anova(), lm(), glm() |
Linear and nonlinear models, anova |
chisq.test(matrix1) |
Chi-square test on matrix matrix1 |
cor(x1) |
Correlation |
cor.test(x1,x2) |
Test correlation |
binom.test() |
Sign test |
fisher.test() |
Fisher exact test |
friedman.test() |
Friedman test |
|