R Part One

1.1 introduction

R

R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R.

R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ˇ­) and graphical techniques, and is highly extensible. The S language is often the vehicle of choice for research in statistical methodology, and R provides an Open Source route to participation in that activity.

One of R's strengths is the ease with which well-designed publication-quality plots can be produced, including mathematical symbols and formulae where needed. Great care has been taken over the defaults for the minor design choices in graphics, but the user retains full control.

R is available as Free Software under the terms of the Free Software Foundation's GNU General Public License in source code form. It compiles and runs on a wide variety of UNIX platforms and similar systems (including FreeBSD and Linux), Windows and MacOS.

features:

1.2 R download and install

R download

Select a mirror site to download the R precompiled binary distributions according to your operating system.

R install

Follow the instruction in the download page to complete the installation

R running

You can run R normally as a Windows/Mac software, or use it in our server:

% R

1.3 R as a calculator

calculation in R

Note the > at the bottom. Whenever you see this symbol, it means that R is not doing anything and just waiting for your input. It's called the promp. You can input your calculations in the R command line,

Basic arithmetic

> 5+10

> 2*(5+10)/3

> log(2^10)/log(2)

Logical operators

> 10>5

> 10<5

> 10!=5

and

> (10!=5)&(10>5)

or

> (10!=5)|(10<5)

1.4 Objects

R is an object oriented language.Everything in R is an object.

When R does anything, it creates and manipulates objects. R's objects come in different types and flavors. The most basic ones are:

try objects

> x <-1

> x

> mode(x)

> length(x)

> A<-'Gomphotherium'; compar<-TRUE;z<-9+2i

> mode(A);mode(compar);mode(z)

> x<-5/0

> x

try vectors

> x <- c(10.4, 5.6, 3.1, 6.4, 21.7)

> x

> x <- c ( 1 : 10 )

> x

> y <- c ( x , 0 , x )

> V <- x + y + 1

> mean(x)

> sum(x)/length(x)

> X <-c("a","tong","ru","zhang","wen")

Arrays, Matrix and Dataframe

> z<-c(1:1500) ;dim(z) <- c(3,5,100)

> x<-array(1:20,dim=c(4,5))

> i<-array(c(1:3,3:1),dim=c(3,2))

> x[i]

> x[i]<-0

> x[i]

> matrix(data=5,nr=2,nc=2)

> matrix(1:6,2,3)

> x<-1:15

> dim(x)<-c(5,3)

> x<-1:4;n<-10;M<-c(10,35)

> data.frame(x,n)

> data.frame(x,M)

1.5 install packages in R

install packages in R

Packages are the fundamental units of reproducible R code. They include reusable R functions, the documentation that describes how to use them, and sample data. In this section you'll learn how to turn your code into packages that others can easily download and use. Writing a package can seem overwhelming at first. So start with the basics and improve it over time. It doesn't matter if your first version isn't perfect as long as the next version is better.

install the package

> install.packages("MASS")

use the package

> library("MASS")

> ? polr

Summary

R R language
mode
length
c
mean
sum
array
matrix

tongyinbio@hku.hk bbru@hku.hk 13th-Feb 2017