Some information how to get R running
Installation:
Homepage (Download and manuals)
Emacs speaks statistics; optional interface for R-programming
How to get help:
Outside of R: R: Quick start
# First Examples in R
# by Charlotte Laufkoetter
# comments are marked with a "#" at the beginning of the line.
# The content of a script can be loaded into the command line with "Control + R" in die Kommandozeile
# geladen werden.
# A semicolon at the end of an order/a line is not necessary, but may remain.
# Variables are assigned with "<-" :
# Scalars:
Var1 <- 13
dt<-5.48
yr<-1
# Vectors:
# One possibility is to list the elemente individually:
Vector1 <- c(3, 4.6, 3, 7.81)
# Here, we generate a vector that contains the elements 1, ..., 10000:
c14grnd <- c(1:10000)
time <- 1:10000 #In this case the c() may be left out.
#It is also possible the other way round:
c14wood <- 10000:1
# Addressing a certain vector element:
c14grnd[3]<-5.7;
Vektor1 <- 1: 10000
Vektor1[1:100] <- 4 # Vector 1 is now 10000 elements long. The first
# 100 fields get the number 4.
# Basic calculating operations are, as usual, +, -, *, / and ^ for potency:
Var1 <- 2^4 + 3 * 9
# Furthermore, the usual functions are available: log, exp,
# sin, cos, tan, sqrt..
Var2 <- sqrt(9)
# max(x) and min(x) return the largest/smallest element of a vectors,
# length(x) returns the number of the elements of x, sum(x) calculates the
# sum of the elements of x, prod(x) analogously the product of the elements of x.
Vektor2 <- c(1, 2, 3, 4, 5)
y <- max(Vektor2)
# y should now contain the value 5
# Loops
# a for-loop:
for (i in 1:10000)
{
c14wood[i] <- 13
time[i]<-i;
}
# a while-loop:
Index <- 4
Enumerator <- 2
while ( Index < 7)
{
Index <- Index + 1
Enumerator = Enumerator * 2
}
# The variable can be given in the command line as follows:
Enumerator
# Graphics:
# The command"plot" generates a new window with a coordinate system,
# if a plot is still open, it is now closed.
# The commandl "lines" inserts new curves into an existing plot.
# plot(x,y) plots y against x.
# plot(x) plots the value of the vector element against its index
# additional parameters:
# log="x" or log="y" causes a logarithmic
# scale at the x- resp. y-axis.
# type= determines the type plot:
# type = "p" gives single dots
# type = "l" gives lines
# type = "b" gives in dots, connected by lines
# ...
# xlab="string" labels the x-axis, analogously ylab="string"
# main = "string" gives the title of the plot
# sub = "string" gives the sub-title below the x-axis in thin face
# with col="red" you may determine the colour
# Example for a simple differential equation incl. output:
Var1 <- 1
Var2 <- 1
Var3 <- 5
Vektor1 <- 1:10000
Vektor2 <- 1:10000
Time <- 1:10000
for (i in 1:10000)
{
Var1 <- Var1 + dt * (4 * Var3 - Var2) #This is a randomly chosen term
Vektor1[i] <- Var1
Vektor2[i] <- Var1 + i^2
Time[i] <- i
}
plot(time, Vektor1, type="l", main="Ueberschrift", sub="Some text may be inserted here", ylab="label y-axis, xlab="X-axis")
lines(Vektor2,col="blue")
# To transform the plot into a postscript:
# Under Windows, simply click on the plot with the right mouse button
# and "als postscript speichern" chose,
# or generally: dev.copy2eps("file=name")
# Data input from a file:
# If the data are available in the following form in ASCII format:
# The first line contains names for all variables,
# all other lines contain a line number as the first element
# and then the values for each variable.
# e.g.:
# Price Bottom Area Room
#01 53 333.2 947.4 4
#02 65 084.9 843 438
#...
# This format can be loaded with read.table("file name").
# If the first line with the names of the variables is cancelled,
# read.table("file name", header=TRUE) can be used.
# na.strings = NA inserts NA (=not available) in case data are not available.
# For Data in netcdf format
Example plot
Comparison with matlab-commands
Brief overview: Matlab order - corresponding R order:
Matlab : R
% (comment) : #
= : <-
P = [p5 p4 p3 p2 p1 p0]; : P <- c(p5, p4, p3, p2, p1, p0)
+, -, *, /, ^ : remains the same
roots(p) : Polyroot(p)
Real(p) : Re(p)
For i=1:100 … end : For (i in 1:100) { …}
While (condition) … end : While (condition) { … }
great link to help archives, manuals and other help pages
examples of fancy R-graphs including source code
Inside of R:
help(command) e.g. help(print): Help page for a command of a loaded package
help(package=package) e.g. help(package=ncdf): Shows the documentation for a specific
help(package=package,command) e.g. help(package=clim.pact,EOF): Help page for a function of a specific package
help.search(“keyword”) e.g. help.search(“spectrum”) : Searches the help files for a specific word.
Installation and use of packages:
Packages are user written libraries which extent the functionality of R
You do not have to download the packages there; it is better to let R let do the job:
Installation of packages in R:
- R command line: install.packages("package_name") e.g. install.packages(“ncdf”)
- In R-enviroment: Menu: install packages
To use the packages:
library(package) e.g. library(ncdf)
Important packages:
ncdf : library to read and write ncdf files. (ncdf binaries, if not installed already on the machine, have to be installed)
easy tutorial
clim.pact: climate statistics library from Rasmus E. Benestad,
chron: more useful time and date functions
fields: curve and function fitting with an emphasis on spatial data (kriging etc.)
extRemes: extreme value statistics for climate data
both are documented
gmt: interface to GMT, unfortunately very sparse… I’m working in the moment on extending the functionality


