# A short introduction to R

 

# The aim of this note is to give a brief introduction to R. You may copy the commands below from the web-browser and paste them into the command window of R.

# Everything on a line that comes after # is a comment and R disregards this.

 

 

# R as a calculator

 

# You may use R as a calculator. For example:

3+2

3-2

3*2

3/2

3^2

sqrt(2)

exp(2)

log(2)

 

 

#Scalars

 

# You may define scalar variables and use them in computations. For example:

a = 2   # alternatively a<-2

b = 3   # alternatively  b<-3

a+b

a*b

a^b

 

 

#Vectors

 

# You may define vector variables and use them in computations. For example:

x = c(1,2,3,4) 

y =c(2,4,6,8)

x+y

y-x

y/x

y^x

 

# Note that R does the computations elementwise

# (R may also perform vector and matrix algebra)

 

# You may select one or more element(s) of a vector:

x[2]

y[c(1,3)]

 

 

#Sequences

 

# R may create special sequences (stored as vectors):

cc = 1:10

cc

dd = seq(0,20,2)

dd

ee = rep(1,10)

ee

 

#Functions of a vector

 

# R has a number of functions that operate on vectors. For example:

sum(x)

prod(x)

length(x)

 

 

#Descriptive statistics

 

# Make a vector with the data from the lectures on the age of mineral samples:

rock.age=c(249,254,243,268,253,269,287,241,273,306,303,280,260,256,278,344,304,283,310)

 

# Compute mean, median, and standard deviation:

mean(rock.age)

median(rock.age)

sd(rock.age)

 

# The command "summary" gives a summary:

summary(rock.age)

 

 

#Reading data from files and dataframes

 

# We may read data from a file (which may be on the web) into a dataframe. For example:

sigarett=read.table("http://www.uio.no/studier/emner/matnat/math/STK4900/v12/sigarett.dat", header=T)

 

#Look at the data and output a summary of them:

sigarett

summary(sigarett)

 

 

#You may access one of the variables in a dataframe. For example:

sigarett$nicot

 

#(note that it is not sufficient to just write "nicot")

 

#You may attach the dataframe:

attach(sigarett)

 

#Then it suffices to just write "nicot"

 

 

#Some plots

 

#R may produce a number of useful plots. Some examples:

hist(nicot)                     # histogram

boxplot(nicot)               # boxplot

qqnorm(nicot)               # normal probability plot

 

#Multiple plots in one figure:

par(mfrow=c(1,2))       # two plots side by side

plot(co, nicot)               # scatter plot

plot(tar, nicot)

par(mfrow=c(1,1))      # restore the setting with single plots

 

 

# Some statistical methods

 

# t-tests and confidence intervals:

t.test(rock.age)

 

# Linear regression:

fit=lm(nicot~co+tar)

summary(fit)

 

# Note that the summary-command depends on the type of object it is applied to.

# This is typical for the way R operates.

 

 

# Hjelp functions

 

# R has a well developed help system that describes the commands. For example:

help(lm)

 

# If you do not remember the name of a command one may use the "help.search" command.

# For example for linear regression:

help.search("linear regression")

 

 

# Logging off and saving the workspace

 

# You exit R by giving the command:

q()

 

#You then get the question "Save workspace image?".

#If you answer yes, R will store all your variables so that you may resume the next session where the last one ended.

 

 

# Configuring R

 

# If you use R for more than one project, it is useful to create one folder for each project.

# For STK4900, you make a folder called STK4900.

# In this folder you paste a shortcut of the R icon.

# You then right-click on the icon, and write (e.g.) "C:\Documents and Settings\username\My Documents\STK4900" in the "Start in" field.

# This will ensure that all computations for STK4900 will be stored in the folder STK 4900, and hence kept apart from other R-computations.

 

 

# Libraries

 

# A number of libraries (or packages) are available in R.

# Some of these come with the standard implementation of R.

# One such example is the survival-package that may be loaded by:

library(survival)

 

# Other packages may be easily downloaded.

# To download a package, you chose "Install package(s)" from the Packages-menu in R.

# Then you chose a CRAN mirror from a drop-down menu (e.g Norway).

# Finally you chose the package from the next drop-down menu.