You are on page 1of 6

Basic tutorial for beginners on R

1) Start the program R (in the classroom W036. On your computer you may have
different configuration).
Start - All programs - Teaching - Math - R 2.7.0
o have a folder for R e!ercises inside your "#y $ocuments" folder and therefore
visi%le from all the computers on the campus do the follo&ing.
Open "#y $ocuments".
'reate a ne& folder inside "#y $ocuments"( say "R tutorial"( and open it.
'opy the address. )or e!ample( my &as "**user)S*staff)S*aal10+*&,-*R tutorial".
he follo&ing R command assigns this address as the R &or-ing directory(
setwd("\\\\userS\\sta!!S\\aal"0#\\w2$\\R\ tutorial"%
.ote dou%le %ac-slashes instead of single %ac-slashes and a %ac-slash %efore the
space. his is %ecause %oth the space and the %ac-slash are special sym%ols for R.
he space is a &ord separator and the %ac-slash is a special sym%ol cancelling the
special meaning (if any) of the ne!t sym%ol.
/our address &ill %e different from the one a%ove( %ut hopefully the same for all
the computers on the campus.
'hec- that the &or-ing directory is set correctly(
getwd(%
his setup &ill %e needed to read0&rite files and to save your R1sessions.
,) R help
Open the help &indo& %y typing
help.start(%
"2n 3ntroduction to R" is &hat you &ill need for learning the general concept of
the R1language. #ore information can %e found using "Search 4ngine 5
6ey&ords".
o open a help page on a particular R command type & follo&ed %y the name of
the command. )or e!ample(
&getwd
&setwd
3t is a good idea to read the help page %efore using the command for the first time.
3) Simple assignments
#a-e a vector of length 11 and assign values from 0 to 10
a ' 0("0
a
2ssign list of values to a vector
) ' c("*2.+*+.0*0.0*-".0*2.0%
)
7ere( a and ) are shortcuts to print(a% and print()% &ith default options.
+) Simple operations and plots
ype the follo&ing and analyse &hat is going on
a ' a,"0
a
- ' e-p(a%
-
#ost of the operations in R are vectorised( so is the plot command(
a'2.pi."("00,"00
plot(a*sin(a%%
plot(a*sin(a%*t/pe'0l0%
8lot appears in a separate &indo&. /ou can save it. 'lic- on the &indo& &ith the
plot( then clic- on !ile( sa1e as and then choose one of the formats. 3 find png
format more convenient %ecause of its porta%ility(

9) Reading data
Open a te!t editor and type in some data. his should %e a ta%le( for e!ample:
" 2 2 # +
2 " + 2 #
2 # 2 + "
# + 2 " 2
Save the ta%le as a te!t only file in your "R tutorial" folder. o read the file &ith R(
append an e!tension ".t!t" to the file name. )or e!ample( if the ta%le &as saved as
"ta%1"( then
c ' read.ta)le("ta)".t-t"%
o ma-e sure that everything is fine print or &rite the ta%le to the terminal(
c
write.ta)le(c%
o &rite the ta%le into a te!t only file "ta%,"( define the file name as the second
argument(
write.ta)le(c*"ta)2.t-t"%
'hec- the structure of the o%;ect c(
str(c%
3t is a data.!rame( an o%;ect type( &hich &ill %e most fre<uently used in our
course. here is also a command data.!rame used to generate data.!rame o%;ects
from simpler o%;ects. 7elp on this command &or-s as usual
&data.!rame
he command read.ta)le is not efficient for large data sets. he command scan is
another option meant to %e more efficient. o read more a%out these commands
type:
&read.ta)le
&scan
6) 7andling data sets from R pac-ages.
ype
data(%
to see the list of availa%le data sets. 7elp is availa%le on each of them( for
e!ample(
&chic$wts
$eclare this data set and attach it:
data(chic$wts%
attach(chic$wts%
he last command puts chic$wts in the search path. o see the entire search path
and( in particular( all attached o%;ects( type
search(%
2n attached o%;ect can %e detached(
detach(chic$wts%
search(%
$o not forget to attach chic$wts again(
attach(chic$wts%
o see elements of the attached o%;ect use ls or o)3ects. )or e!ample(
ls("chic$wts"%
or
ls(2%
he argument 2 is the position of an o%;ect in the search path. (=y default( a ne&
o%;ect is attached at the position 2( and the old positions 2( 2( ... are incremented
%y ")
3f data set is attached( its columns can %e accessed directly( &ithout reference to
the data set. )or e!ample( print the elements of chic$wts(
!eed
weight
he command ls &ithout arguments sho&s the glo%al environment( &hich
occupies the position " in the search path and contains o%;ects created %y a user(
ls(%
ls("%
ls(".4lo)al5n1"%
>se rm(% to cleanup the environment. )or e!ample( to remove the o%;ects a and -
created previously( type
rm(a*-%
and see that a and - no longer e!ist
ls(%
?) Specialised plots
he output of the plot command depends on the o%;ect the plot is operating on.
/ou have already dra&n a plot from t&o vector arguments. ry no& the follo&ing(
plot(weight%
3n addition( there are several specialised plotting commands( for e!ample(
)o-plot(weight%
)o-plot(weight6!eed''"casein"7*weight6!eed''"so/)ean"7%

Other useful plot commands are hist( 88plot( 88norm. ry
hist(weight%
88norm(weight%
plot(!eed*weight%

@) Simple statistics
ry the follo&ing commands
a ' weight6!eed''"casein"7
mean(a%
1ar(a%
sd(a)
summar/(weight6!eed''"casein"7%
summar/(chic$wts%
t.test(weight%
t.test(weight6!eed''"casein"7*weight6!eed''"so/)ean"7%
and analyse the results.
A) Random num%er generators and distri%utions
>sing %uild1in random generators you can sample from( for e!ample( uniform
distri%ution(
a ' runi!("000%
hist(a%
normal distri%ution(
a ' rnorm("000%
hist(a%
e!ponential distri%ution(
a ' re-p("000%
hist(a%

Other possi%ilities can %e found in the section "A. 8ro%a%ility distri%utions" of "2n
3ntroduction to R". 2lso( you can search for "$istri%ution" in the "Search 4ngine
5 6ey&ords".
10) Saving R session.
3f you &ant to interrupt your R session and to resume it later( you need to save the
&or-space image. o do so( <uit your R session %y typing
8("/es"%
2lternatively(
8(%
and /es &hen prompted.
o resume the saved session( go to the &or-ing directory (folder)( &hich you have
previously defined &ith the command setwd. $ou%le1clic- on the icon of the
&or-space image. 3f R does not start( Open With B %ro&se B R B R1,.?.0 B %in
Rgui B Open B O6. .e!t time the dou%le1clic- on the icon &ill hopefully &or-.
>se
histor/(%
and
ls(%
to see the latest commands of the previous session and the o%;ects you have
previously created. .ote that data sets are detached.

You might also like