Difference between revisions of "R programming language"

From Christoph's Personal Wiki
Jump to: navigation, search
(Tutorials)
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''R programming language''' (or just "'''R'''"), sometimes described as "GNU S", is a mathematical language and environment used for statistical analysis and display. It was originally created by Ross Ihaka and Robert Gentleman (hence the name R) at the University of Auckland, New Zealand, and is now steadily developed further by a large community around the world.
+
The '''R programming language''' (or just "'''R'''"), sometimes described as "GNU S", is a mathematical language and environment used for statistical analysis and display.
  
It is based upon [[S programming language|S]], which was developed by John Chambers of Bell Laboratories and described in the paper "Evolution of the S Language" [http://cm.bell-labs.com/stat/doc/96.7.ps]. R is considered by its developers to be an implementation of S, with semantics derived from Scheme. The commercial implementation of S is S-PLUS [http://www.insightful.com/products/splus].
+
R is highly extensible through the use of packages, which are user submitted libraries for specific functions or specific areas of study. A core set of packages are included with the installation of R, with many more available at the comprehensive R archive network, [http://cran.r-project.org/ CRAN]. The [[:Category:Bioinformatics|bioinformatics]] community has seeded a successful effort to use R for the analysis of data from molecular biology laboratories. The [[bioconductor]] project started in the fall of 2001 provides R packages for the analysis of genomic data. e.g. Affymetrix and cDNA microarray object-oriented data handling and analysis tools.
  
R's source code is freely available under the GNU GPL. There are several GUIs for R, including [[RKWard]], [[SciViews-R]] [http://www.sciviews.org/SciViews-R/], and [[Rcmdr]] [http://socserv.socsci.mcmaster.ca/jfox/Misc/Rcmdr/index.html]. Many editors have specialised modes for R, including [[Emacs]] ([[Emacs Speaks Statistics]]), [[jEdit]] [http://community.jedit.org/?q=node/view/2339], [[Kate (text editor)]] [http://www.uni-kiel.de/agrarpol/ahenningsen/app-econ/R.xml.zip], and [http://tinn.solarvoid.com/ Tinn] [http://sourceforge.net/projects/tinn-r], and there is an [http://www.walware.de/goto/statet R plug-in] for the [[Eclipse (software)|Eclipse]] IDE framework.
+
see [[R programming language/Scripts|scripts]]
 
+
R is highly extensible through the use of packages, which are user submitted libraries for specific functions or specific areas of study. A core set of packages are included with the installation of R, with many more available at the comprehensive R archive network, [[CRAN]]. The [[bioinformatics]] community has seeded a successful effort to use R for the analysis of data from [[molecular biology]] laboratories. The [[bioconductor]] project started in the fall of 2001 provides R packages for the analysis of genomic data. e.g. [[Affymetrix]] and [[Complementary DNA|cDNA]] microarray object-oriented data handling and analysis tools.
+
  
 
== Installation ==
 
== Installation ==
Line 24: Line 22:
  
 
Now you are ready to install <tt>R</tt> on SuSE:
 
Now you are ready to install <tt>R</tt> on SuSE:
<pre>
+
./configure
./configure
+
Or,
make
+
./configure --x-includes=/usr/include/X11  # sometimes necessary
make check
+
 
make pdf    # optional
+
make
make info    # optional
+
make check
make install # as superuser ('root')
+
make pdf    # optional
</pre>
+
make info    # optional
 +
make install # as superuser ('root')
  
 
That's it. You are now ready to use <tt>R</tt>
 
That's it. You are now ready to use <tt>R</tt>
Line 40: Line 39:
 
It should not be confused with the R package [http://www.bio.umontreal.ca/Casgrain/en/labo/R/index.html], a collection of programs for multidimensional and spatial analysis available on Macintosh and VAX/VMS systems.  
 
It should not be confused with the R package [http://www.bio.umontreal.ca/Casgrain/en/labo/R/index.html], a collection of programs for multidimensional and spatial analysis available on Macintosh and VAX/VMS systems.  
  
== Basics ==
+
==Basics==
 
+
 
How to get help:
 
How to get help:
* <tt>help.start()</tt> #Opens browser
+
;<code>help.start()</code>:Opens browser
* <tt>help()</tt> #For more on using help
+
;<code>help()</code>:For more on using help
* <tt>help(..)</tt> #For help on ..
+
;<code>help(..)</code>:For help on ..
* <tt>help.search("..")</tt> #To search for ..
+
;<code>help.search("..")</code>:To search for ..
  
 
How to leave again:
 
How to leave again:
* <tt>q()</tt> #Image can be saved to <tt>.RData</tt>
+
;<code>q()</code>:Image can be saved to <tt>.RData</tt>
  
=== Basic R commands ===
+
===Basic R commands===
 
Most arithmetic operators work like you would expect in R:
 
Most arithmetic operators work like you would expect in R:
<pre>
+
4+2 #Prints '6'
> 4 + 2 #Prints ‘6’
+
3*4 #Prints '12'
> 3 * 4 #Prints ‘12’
+
</pre>
+
  
 
Operators have precedence as known from basic algebra:
 
Operators have precedence as known from basic algebra:
<pre>
+
1+2*4   #Prints '9', while
> 1 + 2 * 4 #Prints ‘9’, while
+
(1+2)*4 #Prints '12'
> (1 + 2) * 4 #Prints ‘12’
+
</pre>
+
  
=== Functions ===
+
===Functions===
 
A function call in R looks like this:
 
A function call in R looks like this:
 
* function_name(arguments)
 
* function_name(arguments)
 
* Examples:
 
* Examples:
<pre>
+
cos(pi/3) #Prints '0.5'
> cos(pi/3) #Prints ‘0.5’
+
exp(1)   #Prints '2.718282'
> exp(1) #Prints ‘2.718282’
+
</pre>
+
  
 
A function is identified in R by the parentheses
 
A function is identified in R by the parentheses
* That’s why it’s: help(), and not: help
+
* That's why it's: help(), and not: help
  
=== Variables (objects) in R ===
+
===Variables (objects) in R===
 
To assign a value to a variable (object):
 
To assign a value to a variable (object):
<pre>
+
x<-4   #Assigns 4 to x
> x <- 4 #Assigns 4 to x
+
x=4   #Assigns 4 to x (new)
> x = 4 #Assigns 4 to x (new)
+
x     #Prints '4'
> x #Prints ‘4’
+
y<-x+2 #Assigns 6 to y
> y <- x + 2 #Assigns 6 to y
+
</pre>
+
  
 
Functions for managing variables:
 
Functions for managing variables:
* ls() or objects() lists all existing objects
+
;<code>ls()</code> or <code>objects()</code>:lists all existing objects
* str(x) tells the structure (type) of object ‘x’
+
;<code>str(x)</code>:tells the structure (type) of object 'x'
* rm(x) removes (deletes) the object ‘x’
+
;<code>rm(x)</code>:removes (deletes) the object 'x'
 
+
=== Vectors ===
+
  
 +
===Vectors===
 
A vector in R is like a sequence of elements of the same mode.
 
A vector in R is like a sequence of elements of the same mode.
<pre>
+
x<-1:10           #Creates a vector
> x <- 1:10 #Creates a vector
+
y<-c("a","b","c") #So does this
> y <- c(“a”,“b”,“c”) #So does this
+
</pre>
+
  
 
Handy functions for vectors:
 
Handy functions for vectors:
* c() Concatenates arguments into a vector
+
;<code>c()</code>:Concatenates arguments into a vector
* min() Returns the smallest value in vector
+
;<code>min()</code>:Returns the smallest value in vector
* max() Returns the largest value in vector
+
;<code>max()</code>:Returns the largest value in vector
* mean() Returns the mean of the vector
+
;<code>mean()</code>:Returns the mean of the vector
  
 
Elements in a vector can be accessed individually:
 
Elements in a vector can be accessed individually:
<pre>
+
x[1]     #Prints first element
> x[1] #Prints first element
+
x[1:10]   #Prints first 10 elements
> x[1:10] #Prints first 10 elements
+
x[c(1,3)] #Prints element 1 and 3
> x[c(1,3)] #Prints element 1 and 3
+
</pre>
+
  
 
Most functions expect one vector as argument, rather than individual numbers
 
Most functions expect one vector as argument, rather than individual numbers
<pre>
+
mean(1,2,3)   #Replies '1'
> mean(1,2,3) #Replies ‘1’
+
mean(c(1,2,3)) #Replies '2'
> mean(c(1,2,3)) #Replies ‘2’
+
</pre>
+
  
=== The Recycling Rule ===
+
===The Recycling Rule===
 
The recycling rule is a key concept for vector algebra in R.
 
The recycling rule is a key concept for vector algebra in R.
  
Line 123: Line 106:
  
 
Examples of vectors that are too short:
 
Examples of vectors that are too short:
<pre>
+
x<-c(1,2,3,4)
> x <- c(1,2,3,4)
+
y<-c(1,2) #y is too short
> y <- c(1,2) #y is too short
+
x+y       #Returns '2,4,4,6'
> x + y #Returns ‘2,4,4,6’
+
 
</pre>
 
</pre>
  
=== Data ===
+
===Data===
 +
All simple numerical objects in R function like a long string of numbers. In fact, even the simple: <code>x<-1</code>, can be thought of like a vector with one element.
  
All simple numerical objects in R function like a long string of numbers. In fact, even the simple: x <- 1, can be
+
The functions <code>dim(x)</code> and <code>str(x)</code> returns information on the dimensionality of <code>x</code>.
thought of like a vector with one element.
+
  
The functions dim(x) and str(x) returns information on the dimensionality of x.
+
===Important Objects===
 
+
;<code>vector</code>:A series of numbers
=== Important Objects ===
+
;<code>matrix</code>:Tables of numbers
 
+
;<code>data.frame</code>:More 'powerful' matrix (list of vectors)
* vector – “A series of numbers”
+
;<code>list</code>:Collections of other objects
* matrix – “Tables of numbers”
+
;<code>class</code>:Intelligent(?) lists
* data.frame – “More ‘powerful’ matrix (list of vectors)
+
* list – “Collections of other objects”
+
* class – “Intelligent(?) lists”
+
 
+
=== Data Matrices ===
+
  
 +
===Data Matrices===
 
Matrices are created with the matrix() function.
 
Matrices are created with the matrix() function.
<pre>
+
m<-matrix(1:12,nrow=3)
> m <- matrix(1:12,nrow=3)
+
#This produces something like this:
</pre>
+
– [,1] [,2] [,3] [,4]
 
+
– [1,] 1 4 7 10
This produces something like this:
+
– [2,] 2 5 8 11
<pre>
+
– [3,] 3 6 9 12
– [,1] [,2] [,3] [,4]
+
– [1,] 1 4 7 10
+
– [2,] 2 5 8 11
+
– [3,] 3 6 9 12
+
</pre>
+
  
 
The recycling rule still applies:
 
The recycling rule still applies:
<pre>
+
m<-matrix(c(2,5),nrow=3,ncol=3)
> m <- matrix(c(2,5),nrow=3,ncol=3)
+
#Gives the following matrix:
</pre>
+
– [,1] [,2] [,3]
 
+
– [1,] 2 5 2
Gives the following matrix:
+
– [2,] 5 2 5
<pre>
+
– [3,] 2 5 2
– [,1] [,2] [,3]
+
– [1,] 2 5 2
+
– [2,] 5 2 5
+
– [3,] 2 5 2
+
</pre>
+
 
+
=== Indexing Matrices ===
+
  
 +
===Indexing Matrices===
 
For vectors we could specify one index vector like this:
 
For vectors we could specify one index vector like this:
<pre>
+
x<-c(2,0,1,5)
> x <- c(2,0,1,5)
+
x[c(1,3)] #Returns '2' and '1'
> x[c(1,3)] #Returns ‘2’ and ‘1’
+
</pre>
+
  
 
For matrices we have to specify two vectors:
 
For matrices we have to specify two vectors:
<pre>
+
m<-matrix(1:3,nrow=3,ncol=3)
> m <- matrix(1:3,nrow=3,ncol=3)
+
m[c(1,3),c(1,3)] #Return 2*2 matrix
> m[c(1,3),c(1,3)] #Ret. 2*2 matrix
+
m[1,] #First row as vector
> m[1,] #First row as vector
+
</pre>
+
 
+
=== Beyond two dimensions ===
+
  
 +
===Beyond two dimensions===
 
You can actually assign to dim():
 
You can actually assign to dim():
<pre>
+
x<-1:12
> x <- 1:12
+
dim(x)           #Returns 'NULL'
> dim(x) #Returns ‘NULL’
+
dim(x)<-c(3,4)   #3*4 Matrix
> dim(x) <- c(3,4) #3*4 Matrix
+
dim(x)           #Returns '3 4'
> dim(x) #Returns ‘3 4’
+
dim(x)<-c(2,3,2) #x is now in 3d
> dim(x) <- c(2,3,2) #x is now in 3d
+
dim(x)           #Returns '2 3 2'
> dim(x) #Returns ‘2 3 2’
+
</pre>
+
  
 
But functions like mean() still work:
 
But functions like mean() still work:
<pre>
+
mean(x) #Returns '6.5'
> mean(x) #Returns ‘6.5’
+
</pre>
+
  
=== Graphics and visualisation ===
+
===Graphics and visualisation===
 
+
Visualization is one of R's strong points.
Visualization is one of R’s strong points.
+
  
 
R has many functions for drawing graphs, including:
 
R has many functions for drawing graphs, including:
* hist(x) Draws a histogram of values in x
+
hist(x)   #Draws a histogram of values in x
* plot(x,y) Draws a basic xy plot of x against y
+
plot(x,y) #Draws a basic xy plot of x against y
  
Adding stuff to plots
+
Adding stuff to plots:
* points(x,y) Add point (x,y) to existing graph.
+
points(x,y) #Add point (x,y) to existing graph.
* lines(x,y) Connect points with line.
+
lines(x,y) #Connect points with line.
  
=== Graphical devices ===
+
===Graphical devices===
 +
A graphical device is what 'displays' the graph. It can be a window, it can be the printer.
  
A graphical device is what ‘displays’ the graph. It can be a window, it can be the printer.
+
Functions for plotting "Devices":
 +
X11() #This function allows you to change the size and composition of the plotting window.
 +
par(mfrow=c(x,y)) #Splits a plotting device into x rows and y columns.
 +
dev.print(postscript, file="???.ps") #Use this device to save the plot to a file.
  
Functions for plotting “Devices”:
+
==Packages (add-ons)==
* X11() – This function allows you to change the size and composition of the plotting window.
+
To install packages from the CLI, execute the following:
* par(mfrow=c(x,y)) – Splits a plotting device into x rows and y columns.
+
R CMD INSTALL /path/to/pkg_version.tar.gz
* dev.print(postscript, file=“???.ps”)
+
* Use this device to save the plot to a file.
+
  
=== [[DNA Microarray Analysis (academic course)|DNA Microarray Analysis]] - Example ===
+
==See also==
<pre>
+
*[[Bioconductor]]
## Objects
+
*[http://bio3d.pbwiki.com/ Bio3D]
 +
===Functions===
 +
*[[Heatmap]]
 +
*[[Boxplot]]
  
x <- rnorm(30)
+
===Resources===
 
+
y <- x[x>0]
+
 
+
z <- x
+
z[z<0] <- 0
+
 
+
m <- matrix(x, nrow = 5)
+
str(m)
+
 
+
d.f <- as.data.frame(m)
+
str(d.f)
+
 
+
m[2,2] = "a"
+
d.f[2,2] = "a"
+
str(m)
+
str(d.f)
+
 
+
 
+
## Functions
+
 
+
cube <- function(x) {
+
  z <- x*x*x
+
  return(z)
+
}
+
 
+
fact <- function(x) {
+
z <- 1
+
  for (i in 2:x) {
+
    z <- z * i
+
  }
+
  return(z)
+
}
+
 
+
func <- function(x, y) {
+
  z <- cube(x) - fact(y)
+
  return(z)
+
}
+
 
+
 
+
## Graphics
+
 
+
hist(a <- rnorm(100))
+
 
+
X11()
+
plot(a <- rnorm(100), b <- rnorm(100))
+
points(a[a<0 & b>0], b[a<0 & b>0],col="green")
+
points(a[a>0 & b>0], b[a>0 & b>0],col="red")
+
points(a[a>0 & b<0], b[a>0 & b<0],col="blue")
+
points(a[a<0 & b<0], b[a<0 & b<0],col="yellow")
+
lines(c(-10,10),c(0,0))
+
lines(c(0,0),c(-10,10))
+
</pre>
+
 
+
==See also==
+
 
*[http://www.jstatsoft.org/ Journal of Statistical Software] &mdash; peer-reviewed journal publishing many R related papers
 
*[http://www.jstatsoft.org/ Journal of Statistical Software] &mdash; peer-reviewed journal publishing many R related papers
 
*[http://cran.r-project.org/mirrors.html CRAN] &mdash; Comprehensive R Archive Network for the R programming language.
 
*[http://cran.r-project.org/mirrors.html CRAN] &mdash; Comprehensive R Archive Network for the R programming language.
 +
*[http://spider.stat.umn.edu/R/library/graphics/html/ R graphics] &mdash; a long list of techniques with examples.
 +
===Books===
 +
*[http://www3.imperial.ac.uk/naturalsciences/research/statisticsusingr Statistics: An Introduction using R]
  
 
==External links==
 
==External links==
 
*[http://www.r-project.org/ The R Project for Statistical Computing]
 
*[http://www.r-project.org/ The R Project for Statistical Computing]
 
*[http://www.cran.r-project.org/ The CRAN (Comprehensive R Archive Network) Project]
 
*[http://www.cran.r-project.org/ The CRAN (Comprehensive R Archive Network) Project]
*[http://www.math.montana.edu/Rweb/ Web-based interface to R]
 
 
*[http://www.network-theory.co.uk/R/base/ The R Reference Manual - Base Package] by the R Development Core Team. ISBN 0-9546120-0-0 (vol. 1), ISBN 0-9546120-1-9 (vol. 2)
 
*[http://www.network-theory.co.uk/R/base/ The R Reference Manual - Base Package] by the R Development Core Team. ISBN 0-9546120-0-0 (vol. 1), ISBN 0-9546120-1-9 (vol. 2)
*[http://fawn.unibw-hamburg.de/cgi-bin/Rwiki.pl?RwikiHome The R Wiki] User contributed R documentation and [[how to]] information.
+
===Wikis===
*[http://addictedtor.free.fr/graphiques The R Graph Gallery] shows several examples of graphics generated by R
+
*[http://wiki.r-project.org/rwiki/doku.php R Wiki]
*[http://gentleman.fhcrc.org/ Robert Gentleman's site]
+
*[http://fawn.unibw-hamburg.de/cgi-bin/Rwiki.pl?RwikiHome The R Wiki] User contributed R documentation and how to information.
*[http://www.stat.auckland.ac.nz/~ihaka/ Ross Ihaka's site]
+
*[http://commons.wikimedia.org/wiki/Category:Created_with_R Collection of examples] &mdash; from Wikimedia Commons.
 +
 
 +
===Packages / Resources===
 +
*[http://rpy.sourceforge.net/ RPy]
 
*[http://socserv.socsci.mcmaster.ca/jfox/Misc/Rcmdr/ Rcmdr, an open source GUI for R]
 
*[http://socserv.socsci.mcmaster.ca/jfox/Misc/Rcmdr/ Rcmdr, an open source GUI for R]
 
*[http://www.sciviews.org/_rgui/projects/Editors.html List of IDEs and script editors for R]
 
*[http://www.sciviews.org/_rgui/projects/Editors.html List of IDEs and script editors for R]
 
*[http://sourceforge.net/projects/tinn-r/ Tinn-R, an advanced open source script editor for R under Windows]
 
*[http://sourceforge.net/projects/tinn-r/ Tinn-R, an advanced open source script editor for R under Windows]
 +
*[http://www.math.montana.edu/Rweb/ Web-based interface to R]
 +
===Tutorials===
 +
*[http://www.noitulove.ch/2008/07/03/learning-r-part-i/ Learning R - Part I]
 +
*[http://addictedtor.free.fr/graphiques The R Graph Gallery] shows several examples of graphics generated by R
 +
*[http://gentleman.fhcrc.org/ Robert Gentleman's site]
 +
*[http://www.stat.auckland.ac.nz/~ihaka/ Ross Ihaka's site]
 +
*[http://flowingdata.com/2012/05/15/how-to-visualize-and-compare-distributions/ How to Visualize and Compare Distributions] &mdash; by FlowingData
  
[[Category:Technical and Specialized Skills]]
+
[[Category:R| R programming language]]
[[Category:Graphics software]]
+

Latest revision as of 22:03, 16 June 2012

The R programming language (or just "R"), sometimes described as "GNU S", is a mathematical language and environment used for statistical analysis and display.

R is highly extensible through the use of packages, which are user submitted libraries for specific functions or specific areas of study. A core set of packages are included with the installation of R, with many more available at the comprehensive R archive network, CRAN. The bioinformatics community has seeded a successful effort to use R for the analysis of data from molecular biology laboratories. The bioconductor project started in the fall of 2001 provides R packages for the analysis of genomic data. e.g. Affymetrix and cDNA microarray object-oriented data handling and analysis tools.

see scripts

Installation

Installing R on SuSE 10.1 using the default settings for the rpm or source distribution seems to be a problem. Below are the methods I have used to resolved these problems.

First make sure you have the following installed (check http://www.rpmfind.net for the packages):

compat-g77
compat-gcc
gcc-g77

It also sometimes helps to create a soft link to gfortran like so (changing the directory to suit your needs):

ln -s /usr/bin/g77 /usr/bin/gfortran

Then, and this is important, add the following to your config.site (found in your R source directory):

FPICFLAGS=-g

Now you are ready to install R on SuSE:

./configure

Or,

./configure --x-includes=/usr/include/X11  # sometimes necessary
make
make check
make pdf     # optional
make info    # optional
make install # as superuser ('root')

That's it. You are now ready to use R

Comparison with other programs

Although R is mostly used by statisticians, and other people in need of statistics, it can also be used as a general matrix calculation toolbox in a program such as GNU Octave or its proprietary counterpart, MATLAB.

It should not be confused with the R package [1], a collection of programs for multidimensional and spatial analysis available on Macintosh and VAX/VMS systems.

Basics

How to get help:

help.start()
Opens browser
help()
For more on using help
help(..)
For help on ..
help.search("..")
To search for ..

How to leave again:

q()
Image can be saved to .RData

Basic R commands

Most arithmetic operators work like you would expect in R:

4+2 #Prints '6'
3*4 #Prints '12'

Operators have precedence as known from basic algebra:

1+2*4   #Prints '9', while
(1+2)*4 #Prints '12'

Functions

A function call in R looks like this:

  • function_name(arguments)
  • Examples:
cos(pi/3) #Prints '0.5'
exp(1)    #Prints '2.718282'

A function is identified in R by the parentheses

  • That's why it's: help(), and not: help

Variables (objects) in R

To assign a value to a variable (object):

x<-4   #Assigns 4 to x
x=4    #Assigns 4 to x (new)
x      #Prints '4'
y<-x+2 #Assigns 6 to y

Functions for managing variables:

ls() or objects()
lists all existing objects
str(x)
tells the structure (type) of object 'x'
rm(x)
removes (deletes) the object 'x'

Vectors

A vector in R is like a sequence of elements of the same mode.

x<-1:10           #Creates a vector
y<-c("a","b","c") #So does this

Handy functions for vectors:

c()
Concatenates arguments into a vector
min()
Returns the smallest value in vector
max()
Returns the largest value in vector
mean()
Returns the mean of the vector

Elements in a vector can be accessed individually:

x[1]      #Prints first element
x[1:10]   #Prints first 10 elements
x[c(1,3)] #Prints element 1 and 3

Most functions expect one vector as argument, rather than individual numbers

mean(1,2,3)    #Replies '1'
mean(c(1,2,3)) #Replies '2'

The Recycling Rule

The recycling rule is a key concept for vector algebra in R.

When a vector is too short for a given operation, the elements are recycled and used again.

Examples of vectors that are too short:

x<-c(1,2,3,4)
y<-c(1,2) #y is too short
x+y       #Returns '2,4,4,6'

</pre>

Data

All simple numerical objects in R function like a long string of numbers. In fact, even the simple: x<-1, can be thought of like a vector with one element.

The functions dim(x) and str(x) returns information on the dimensionality of x.

Important Objects

vector
A series of numbers
matrix
Tables of numbers
data.frame
More 'powerful' matrix (list of vectors)
list
Collections of other objects
class
Intelligent(?) lists

Data Matrices

Matrices are created with the matrix() function.

m<-matrix(1:12,nrow=3)
#This produces something like this:
– [,1] [,2] [,3] [,4]
– [1,] 1 4 7 10
– [2,] 2 5 8 11
– [3,] 3 6 9 12

The recycling rule still applies:

m<-matrix(c(2,5),nrow=3,ncol=3)
#Gives the following matrix:
– [,1] [,2] [,3]
– [1,] 2 5 2
– [2,] 5 2 5
– [3,] 2 5 2

Indexing Matrices

For vectors we could specify one index vector like this:

x<-c(2,0,1,5)
x[c(1,3)] #Returns '2' and '1'

For matrices we have to specify two vectors:

m<-matrix(1:3,nrow=3,ncol=3)
m[c(1,3),c(1,3)] #Return 2*2 matrix
m[1,] #First row as vector

Beyond two dimensions

You can actually assign to dim():

x<-1:12
dim(x)           #Returns 'NULL'
dim(x)<-c(3,4)   #3*4 Matrix
dim(x)           #Returns '3 4'
dim(x)<-c(2,3,2) #x is now in 3d
dim(x)           #Returns '2 3 2'

But functions like mean() still work:

mean(x) #Returns '6.5'

Graphics and visualisation

Visualization is one of R's strong points.

R has many functions for drawing graphs, including:

hist(x)   #Draws a histogram of values in x
plot(x,y) #Draws a basic xy plot of x against y

Adding stuff to plots:

points(x,y) #Add point (x,y) to existing graph.
lines(x,y)  #Connect points with line.

Graphical devices

A graphical device is what 'displays' the graph. It can be a window, it can be the printer.

Functions for plotting "Devices":

X11() #This function allows you to change the size and composition of the plotting window.
par(mfrow=c(x,y)) #Splits a plotting device into x rows and y columns.
dev.print(postscript, file="???.ps") #Use this device to save the plot to a file.

Packages (add-ons)

To install packages from the CLI, execute the following:

R CMD INSTALL /path/to/pkg_version.tar.gz

See also

Functions

Resources

Books

External links

Wikis

Packages / Resources

Tutorials