Here is a tutorial I put together to get you start with R quickly. But you need to read more systematic tutorials after you take off. comments/suggestions to jianganghao@gmail.com
0. To make R script executable, put the following at the beginning of the script.
#! /usr/bin/env Rscript
1. Go to the R webpage, under "Package", you can find the descriptions about the existing packages. You can install the packages by
install.packages("packageName")
2. Vectors and Matrices are widely used in R functions.
x <- 1:4 is vector of numbers 1, 2, 3, 4 or similar c(1:4), or seq(1,4,by=1)
c() function is used in R to create a vector
seq() function works with sequences
cbind() and rbind() combines vectors, matrices by columns and rows respectively.
rep(a,k): define an array of k dim with element a.
Example:
x=c(1:5)
y=seq(6,10,by=1)
cbind(x,y)
rbind(x,y)
rep(0.,100) : define a 100 dim array with elements = 0.
3. To define matrix use function matrix()
Example:
a= matrix(0,nrow=2,ncol=3) - 2 x 3-matrix ”a” with all zeros.
There are different approaches to edit matrix:
a[1,2] = 3 (by elements), a[1,] = 3 (by rows)
fix(a) will open Data editor (which available in latest R version) similar, open editor manually Edit−→ Data editor conver t data into matrix using function as.matrix()
4. Matrix operations :
t(a) (transpose ”a”)
a% ∗ %t(a) (matrix multiplication)
a*a (element-wise multiplication)
eigen(a% ∗ %t (a)) (eigen values and vectors)
5. Useful functions:
dim() Retrieve or set the dimension of an object.
To comment a line, use #
names() Functions to get or set the names of an object.
attributes() Function returns the object’s attribute list.
sum() Sum all numbers in the vector
length() Find the length of the vector
round(v,x) Round all elements in v to x decimal places
factor() Conver t a num/char vector into a factor vct.
sort() Sort a vector in ascending orde; sort(x, decreasing = T)
order() sort a vector and return the index of the sorted vector.
numeric(x) Create a zero numeric vector of size x
character(x) Create a blank character vector of size x
as.factor(x), as.matrix(x), as.vector(x), etc.
prod(x), product of array
sample(1:10,3): randomly select 3 number from 1:10 range
6. Basic Data Manipulations:(use the famous crab data as an example)
Type crabs (R is case-sensitive!)to take a look at the full data set:
Species Sex FL RW CL CW BD
1 1 1 8.1 6.7 1.61 1.90 0.70
2 1 1 8.8 7.7 1.81 2.08 0.74
.........
crabs[1:3,] prints first 3 rows of the data;
crabs[,1:2] prints first 2 data columns;
crabs[1,2] prints first obser vation in the second column;
crabs$FL is a vector which refers to the variable FL
crabs$newvar=crabs$Sex + 2*crabs$Species creates new variable
female=crabs[crabs$Sex==1,] Create new data frame contains only female crabs
female=subset(crabs,Sex==1) the same as female=crabs[crabs$Sex==1,]
7. Basic Statistics
Density(starts with d), distribution function (p), quantile function (q)and random generation (r)
for the Beta (beta), for t (t), for normal (norm), for F (f), for binomial (binom), for Chi-square (chisq) distributions.
mean(crabs) (calculates means), sd(crabs) (standard deviations), median(crabs) for all variables in the data set, and mean(crabs$FL) calculates mean for variale FL only.
Function summary(crabs) for numerical variables calculates six summary statistics, and for categorical variables will have categories tabulated.
Functions cov(crabs) and cor(crabs) compute covariance and correlation matrices respectively
sum(); colSums(); rowSums()
8. Function Plot
PLOT is the most generic function for plotting of R objects.
plot(crabs$FL) is the simplest way to plot variable FL
To plot FL using different glyphs for Sex and colors for Species use
plot(crabs$FL,pch=as.character(crabs$Sex),col=as.numeric(crabs$Species))
To add interactively plot legend:
legend(locator(1),c("Male","Female"),pch=c(’1’,’2’),col=4)
plot(crabs$FL,crabs$CL) shows scatter plot (variable CL against variable FL)
abline(v=mean(crabs$FL)) adds vertical (v) (for horizontal(h)) line through the mean of variable FL
abline(lm(crabs$CL ̃crabs$FL),col=4,lty=2) adds a
linear regression line (lty correcponts to line style)
points(x,y,pch='.',col='red') is a function that overplot on an existing plot the points specified by x,y. (important way to do overplot)
hist2d(x,y) : exampel: > hst=hist2d(b$z,b$z_err,nbins=c(100,100),show=F)
>filled.contour( hst$x, hst$y, hst$counts, nlevels=16,col=rainbow(16),xlab='z',ylab='z errors')
9. Specific Plots
par(mfrow=c(2,2)) helps to setup a picture that contains four graphs in 2 rows and 2 columns.
Some Statistical Plots:
pairs(crabs[,-c(1,2)]) creates matrix of scatter plot for all variables (except Species and Sex)
hist(crabs$FL,breaks=100) computes a histogram of FL values
plot(density(crabs$FL)) plots an estimated (Kernel) density
boxplot(crabs$FL) produces a single boxplot
boxplot(crabs$FL~crabs$Sex,col=c(2,5)) produces side-by-side plot for 2 groups
qqnorm(crabs$FL) followed by qqline(crabs$FL) create QQ-plot with corresponding straight line
10. Save plots:
save as jpeg plot:
jpeg('plot.jpg')
make your plot here
dev.off()
save as png:
png('test.png')
make your plot here
dev.off()
11. For loop
for(i in 1:100) {print(i); print(i+1);}
12. Function definition:
test<-function(x,y){ z =- x^2 + y^2; tt=x+y+z;return(tt); }
it will return the result of x+y+z
13. packages handling
RSiteSearch("voronoi", restrict = "functions")
install.packages("tripack") : for voronoi tessellation
MASS: kde2d
14. random number generation:
rnorm: normal
rlnorm: log normal
mvrnorm: multivariate normal ( in MASS package)
15. Greek letters in plot:
example: xlab=expression(theta)
16. Convert forth/back from factor level to string
In a data structure, if letter show up without "", it will be treated as factor levels. This will lead to trouble. You can convert among them by the following:
> directions <- c("North", "East", "South", "South")
> directions.factor <- factor(directions)
> directions.factor
[1] North East South South
Levels: East North South
> as.character(directions.factor)
[1] "North" "East" "South" "South"
> as.numeric(directions.factor)
[1] 2 1 3 3