Part I: Introductory Materials: Introduction To R
Part I: Introductory Materials: Introduction To R
Introduction to R
> v = rnorm(256)
> A = as.matrix (v,16,16)
> summary(A)
> library (fields)
> image.plot (A)
>…
> dyn.load( “foo.so”)
> .C( “foobar” )
> dyn.unload( “foo.so” )
2
Why R?
Scripting
(R, MATLAB, IDL)
Object Oriented
(C++, Java)
Functional languages
(C, Fortran)
Assembly
4
Features of R
7
Variables and assignment
To create a matrix:
# matrix() command to create matrix A with rows and cols
A=matrix(c(54,49,49,41,26,43,49,50,58,71),nrow=5,ncol=2))
B=matrix(1,nrow=4,ncol=4)
15
Exploring the iris data (cont.)
• Plot Petal.Length vs. Petal.Width:
– plot (iris[ , 3], iris[ , 4]);
– example(plot)
• Exercise: create a plot similar to this figure:
• Large data sets are better loaded through the file input interface in R
• Reading a table of data can be done using the read.table() command:
• a <- read.table(“a.txt”)
• The values are read into R as an object of type data frame (a sort of
matrix in which different columns can have different types). Various
options can specify reading or discarding of headers and other
metadata.
• A more primitive but universal file-reading function exists, called
scan()
• b = scan(“input.dat”);
• scan() returns a vector of the data read
Programming in R
• The following slides assume a basic understanding of
programming concepts
Additional resources
• Beginning R: An Introduction to Statistical Programming by Larry
Pace
• Introduction to R webpage on APSnet:
http://www.apsnet.org/edcenter/advanced/topics/ecologyandepidemiologyinr
/introductiontor/Pages/default.aspx
• The R Inferno:
http://www.burns-stat.com/pages/Tutor/R_inferno.pdf
18
Conditional statements
• Step 1: Download R
– mkdir for RHOME; cd $RHOME
– wget http://cran.cnr.berkeley.edu/src/base/R-2/R-2.9.1.tar.gz
• Step 2: Install R
– tar –zxvf R-2.9.1.tar.g
– ./configure --prefix=<RHOME> --enable-R-shlib
– make
– make install
• Step 3: Run R
– Update env. variables in $HOME/.bash_profile:
• export PATH=<RHOME>/bin:$PATH
• export R_HOME=<RHOME>
– R
24
Useful R links
• R Home: http://www.r-project.org/
• R’s CRAN package distribution: http://cran.cnr.berkeley.edu/
• Introduction to R manual:
http://cran.cnr.berkeley.edu/doc/manuals/R-intro.pdf
• Writing R extensions:
http://cran.cnr.berkeley.edu/doc/manuals/R-exts.pdf
• Other R documentation:
http://cran.cnr.berkeley.edu/manuals.html
25