You are on page 1of 76

R for Beginners

Emmanuel
n
E >;L#G IBK?.# ,KLB)._ ,;6?:>(-;. 1.$K.6$2_ 5K()?. =LLF._ 96?.)6?$; .?:$;_
(B?: BL#7G.:6(#_ 96L#t$(?: 4?#L6)_ L#) %L>;?.B <(: 3(6 >;.?6 $(OO.#>: L#)
:B77.:>?(#: (# .L6K?.6 F.6:?(#: (3 >;?: )($BO.#>g E LO LK:( 76L>.3BK >( LKK >;.
O.OP.6: (3 >;. < 1.F.K(-O.#> ,(6. D.LO 3(6 >;.?6 $(#:?).6LPK. .u(6>: ?#
).F.K(-?#7 < L#) L#?OL>?#7 >;. )?:$B::?(# K?:> j6;.K-sg D;L#G: LK:( >( >;.
< B:.6: J;(:. 2B.:>?(#: (6
6 Programming with R in pratice 64
[go (
<
2 A few concepts before starting

0#$. < ?: ?#:>LKK.) (# "(B6 $(O-B>.6_ >;. :(3>JL6. ?: .N.$B>.) P" KLB#$;?#7
>;. $(66.:-(#)?#7 .N.$B>LPK.g D;.
D;. )?7?> o J?>;?# P6L$G.>: ?#)?$L>.: >;L> >;. )?:-KL" :>L6>: L> >;. ]6:>
.K.O.#> (3 ng D;?: $(OOL#) ?: L# ?O-K?$?> B:. (3 >;. 3B#$>?(# print L#) >;.
LP(F. .NLO-K.
)?:-KL
D;. L::?7#O.#> J?KK P. (O?>>.) ?# >;. .NLO-K.: ?3 #(> #.$.::L6" 3(6 B#d
).6:>L#)?#7g
).6 #.$LO
D;. 3B#$>?(# ls K?:>: :?O-K" >;. (P f ddC Vcg[r^r l D) T?#(#WDC SVgorV^ l D) TPW.
2.3 The on-line help

D;. (#dK?#. ;.K- (3 < 7?F.: F.6" B:.3BK ?#3(6OL>?(# (# ;(J >( B:. >;. 3B#$>?(#:g
A.K- ?: LFL?KLPK. )?6.$>K" 3(6 L 7?F.# 3B#$>?(#_ 3(6 ?#:>L#$.h

> ?lm

J?KK )?:-KL"_ J?>;?# <_ >;. ;.K- -L7. 3(6 >;. 3B#$>?(# lm() Tlinear modelWg D;.
Arithmetic
$(OOL#): help(lm)package:base
L#) help("lm") ;LF. >;. :LO. R Documentation
.u.$>g D;. KL:> (#. OB:>
> help("*")
P. B:.) >( L$$.:: ;.K- J?>; #(#d$(#
F.#>?(#LK $;L6L$>.6:h
Error: syntax error
> ?*
> help("bs")
No documentation for bs in specified packages and libraries:
you could try help.search("bs")
> help("bs", try.all.packages = TRUE)
Help for topic bs is not in any loaded package but
can be found in the following packages:

Package Library
splines /usr/lib/R/library

+(>. >;L> ?# >;?: $L:. >;. ;.K- -L7. (3 >;. 3B#$>?(# bs ?: #(> )?:-KL".)g
D;. B:.6 $L# )?:-KL" ;.K- -L7.: 36(O L GL7. #(> K(L).) ?# O.O(6" B:?#7
>;. (->?(# packageh

> help("bs", package = "splines")


bs package:splines R Documentation

B-Spline Basis for Polynomial Splines

Description:

Generate the B-spline basis matrix for a polynomial spline.


...

D;. ;.K- ?# ;>OK 3(6OL> T6.L)_ .g7g_ J?>; +.>:$L-.W ?: $LKK.)


3 Data with R

3.1 Objects

Q. ;LF. :..# >;L> < J(6G: J?>; (PC.$>: J;?$; L6._ (3 $(B6:._ $;L6L$>.6?.) P"
>;.?6 #LO.: L#) >;.?6 $(#>.#>_ PB> LK:( P" attributes J;?$; :-.$?3" >;. G?#) (3
)L>L 6.-6.:.#>.) P" L# (PC.$>g E# (6).6 >( B#).6:>L#) >;. B:.3BK#.:: (3 >;.:.
L>>6?PB>.:_ $(#:?).6 L FL6?LPK. >;L> >LG.: >;. FLKB. o_ r_ (6 Sh :B$; L FL6?LPK.
$(BK) P. L# ?#>.7.6 FL6?LPK. T3(6
> x <- 5/0
> x
[1] Inf
> exp(x)
[1] Inf
> exp(-x)
[1] 0
> x - x
[1] NaN

! FLKB. (3 O(). $;L6L$>.6 ?: ?#-B> J?>; )(BPK. 2B(>.: "g E> ?: -(::?PK.
>( ?#$KB). >;?: KL>>.6 $;L6L$>.6 ?# >;. FLKB. ?3 ?> 3(KK(J: L PL$G:KL:; \E> SF>(
PDC;L6L$>.6
XgoVfJX l ^gXLKB.PL$
D( J6?>. ?# L :?O-K.6 JL" L# (PC.$> ?# L ]K._ >;. $(OOL#) write(x,
file="data.txt") $L# P. B:.)_ J;.6. x ?: >;. #LO. (3 >;. (PC.$> TJ;?$;
$L# P. L F.$>(6_ L OL>6?N_ (6 L# L66L"Wg D;.6.
E> ?:>.6
?: LK:(d^SgX[looSogV
-(::?PK._ ?3 (#.
^S[gSr
JL#>:
DO[cE>?VDD)SD)
>( .#>.6 :(O. )L>L
TJWDC
(# >;.
:$LgSco3i<DC
G oogVfco l Dr[gXfD) TGWDC J?>;cgX
9?#LKK"_ expand.grid() $6.L>.: L )L>L 36LO. J?>; LKK $(OP?#L>?(#: (3 F.$d
>(6: (6 3L$>(6: 7?F.# L: L67BO.#>:h

> expand.grid(h=c(60,80), w=c(100,


>;. $BOBKL>?F. -6(PLP?K?>" ).#:?>" Tpfunc (x, ...)W_ L#) >;. FLKB. (3 2BL#>?K.
Tqfunc (p, ...)_ J?>; l < p < oWg D;. KL:> >J( :.6?.: (3 3B#$>?(#: $L# P.
B:.) >( ]#) $6?>?$LK FLKB.: (6 P dFLKB.: (3 :>L>?:>?$LK >.:>:g 9(6 ?#:>L#$._ >;.
$6?>?$LK FLKB.: 3(6 L >J(d>L?K.) >.:> 3(KK(J?#7 L #(6OLK )?:>6?PB>?(# L> >;. Xm
>;6.:;(K) L6.h

> qnorm(0.025)
[1] -1.959964
> qnorm(0.975)
[1] 1.959964

9(6 >;. (#.d>L?K.) F.6:gV^[S lSog[oVX l D) T(3WDC orgSSco l D) T>;.WDC ocgcl^S l D) T:LO.WDC r^gof[S l
Factor. ! 3L$>(6 ?#$KB).: #(> (#K" >;. FLKB.: (3 >;. $(66.:-(#)?#7 $L>.7(6?$LK
FL6?LPK._ PB> LK:( >;. )?u.6.#> -(::?PK. K.F.K: (3 >;L> FL6?LPK. T.F.# ?3 >;."
L6. #(> -cS[.#> ?# >;. )L>LWg D;. 3B#$>?(# factor L 3L$>(6 J?>;
>;. 3(KK(
D;. (->?(# byrow ?#)?$L>.: J;.>;.6 >;. FLKB.: 7?F.# P" data OB:> ]KK
:B$$.::?F.K" >;. $(KBO#: T>;.
>?(#: J;?$; $;L6L$>.6?. >;. :.6?.:g D;. (->?(#:_ J?>; >;. ).3LBK> FLKB.:_
L6.h

ts(data = NA, start = 1, end = numeric(0), frequency = 1,


deltat = 1, ts.eps = getOption("ts.eps"), class, names)

data L F.$>(6 (6 L OL>6?N


start >;. >?O. (3 >;. ]6:> (PDC F.?>;.6 L #BOP.6_ (6 L
F.$>(6 (3 >J( ?#>.7.6: T:.. >;. .NLO-K.: P.K(JW
.#)
end >;. >?O. (3 >;. KL:> (PDC F:-.$?].) ?# >;. :LO.
-L$GL7.: base L#) utils_ fc (3 :B$; 3B#$>?(#:_ :( J. J?KK #(> 7( ?# >;. )..-.:>
).>L?K: ;.6.g
D;. 6.:BK> (3 L $(#F.6:?(# ).-.#): (PF?(B:K" (3 >;. L>>6?PB>.: (3 >;. $(#d
F.6>.) (PC.$>g =.#6LKK"_ $(#F.6:?(# 3(KK(J: ?#>B?>?F. 6BK.:g 9(6 >;. $(#F.6:?(#
(3 O(
D( $(#F.6> L 3L$>(6 (3 O(). #BO.6?$ ?#>( L #BO.6?$ F.$>(6 PB> G..-?#7 >;.
K.F.K: L: >;." L6. (6?7?#LKK" :-.$?].)_ (#. OB:> ]6:> $(#F.6> ?#>( $;L6L$>.6_
>;.# ?#>( #BO.6?$g

> as.numeric(as.character(fac))
[1] 1 10

D;?: -6($.)B6. ?: l D) To^co


J?KK_ lP"D) TFWDC XgScflf l D) T.6"WDC ofglV^r l D) TB:.3BKWDC Sog[oVX l D)
ors
: $(KBO# L: L 3L$>(6g
cr l D) TFWDC XgScflf l D) T.WDC cg^Vfl^ l D) T:..#WDC rSg^o[V l D) T-6.F?(B:K"WDC Xrgrolf l D) T>;L>WDC rSg^o[S l D) T>;.6.W
> x <- 0.5
> 0 < x < 1
[1] FALSE

D;. $(O-L6?:(# (-.6L>(6: (-.6L>. (# each .K.O.#> (3 >;. >J( (PC.$>:


P.?#7 $(O-L6.) T6.$"$K?#7 >;. FLKB.: (3 >;. :;(6>.:> (#. ?3 #.$.::L6"W_ L#)
>;B: 6.>B6#: L# (PC.$> (3 >;. :LO. :?.g D( $(O-L6. jJ;(KK"s
> x[3]
[1] 3
> x[3] <- 20
> x
[1] 1 2 20 4 5

D;. ?#).N ?>:.K3 $L# P. L F.$>(6 (3 O(). #BO.6?$h

> i <- c(1, 3)


> x[i]
[1] 1 20

E3 x ?: L OL>6?N (6 L )L>L
36LO._ > TVWDCocgfr^r l D) TFWDC XgoVfo l D) TLKB.WDC rSgo^go l D) T(3WDC
(7?$LK ?#).N?#7 $L# LK:( P. B:.) J?>; )L>L 36LO.:_ PB> J?>; $LB>?(# :?#$.
)?u.6.#> $(KBO#: (3 >;. )L>L )6LO.J?>;"6
> $(KBO(6
fl^ l D)
cXcD)
T.WDC
TJ?>;).:gdSVVgr[
[gV[fl^ l D) T.WDC
DO^SgX[lo
^gflcgScflD)
DTJ?>;9[gV[flo
T(3WDC orgSl
[[2]]
[1] "c" "d"

9(6 L66L":_ >;. #LO.: (3 >;. )?O.#:?(#: $L# P. L$$.::.) J?>; dimnamesh

> A <- array(1:8, dim = c(2, 2, 2))


> A
, , 1

[,1] [,2]
[1,] 1 3
[2,] 2 45.7963 0 Td (1)4

, , 2

[,1] [,2]
[1,] 5 45.7963 0 Td (1)7
[2,] 6 45.7963 0 Td (1)8

> dimnames(A) <- list(c("a", "b"), c("c", "d"), c("e", "f"))


> A
, , e

c d
a 1 3
b 2 4

, , f

c d
a 5 7
b 6 8

E3 >;. .K.O.#>: (3 L# (PC.$> VXg^ogVS[V l D) T;LWDC oogoScr l D) TFWDC XgScflf l D) T.WDC fgolflc l D) T#LO.:_WDC S
3.5.6 The data editor
E> ?: -(::?PK. >( B:. L 76L-;?$LK :-6.L):;..>dK?G. .)?>(6 >( .)?> L R)L>LY (PC.$>g
9(6 .NLO-K._ ?3 X ?: L OL>6?N_ >;. $(OOL#) data.entry(X) J?KK (-.# L 76L-;?$
.)?>(6 L#) (#. J?KK P. LPK. >( O()?3" :(O.
+(>. >;L> < 6.>B6#.) L JL6#?#7 O.::L7. L#) #(> L# .66(6 O.::L7._ >;B:
>;. (-.6L>?(# ;L: P..# -.63(6O.)g E3 J. JL#> >( L)) T(6 OBK>?-K"W >;. :LO.
FLKB. >( LKK >;. .K.O.#>: (3 L F.$>(6h

> x <- 1:4


> a <- 10
8zzz
> z <- a * x
log(x, base) computes the logarithm of x with base base
scaleTf ) if x is a matrix, cenlogarithmdata; 0 Td5.540ith
[1,] 1 1 2 2
[2,] 1 1 2 2

D;. (-.6L>(6 3(6 >;. -6()B$> (3 >J( OL>6?$.: ?: j%*%sg 9(6 .NLO-K._ $(#d
:?).6?#7 >;. >J( OL>6?$.: m1 L#) m2 LP(F.h

> rbind(m1,
m2) %*% cbind(m1,
m2)
[,1] [,2] [,3] [,4]
[1,] 2 2 4 4
[2,] 2 2 4 4
[3,] 4 4 8 8
[4,] 4 4 8 8
> cbind(m1,
m2) %*% rbind(m1,
m2)

10 10
10 10

. >6L#:-(:?>?(# (3 L OL>6?N ?: )(#. J?>;


< ;L: LK:( :(O. :-.$?LK 3B#$>?(#: 3(6 OL>6?N $(O-B>L>?(#g Q. $L# $?>.
;.6. solve 3(6 ?#F.6>?#7 L OL>6?N_ qr 3(6 ).$(O-(:?>?(#_ eigen 3(6 $(O-B>?#7
.?7.#FLKB.: L#) .?7.#F.$>(6:_ L#) svd 3(6 :?#7BKL6 FLKB. ).$(O-(:?>?(#g

SX
4 Graphics with R

< (u.6: L 6.OL6GLPK. FL6?.>" (3 76L-;?$:g D( 7.> L# ?).L_ (#. $L# >"-.
demo(graphics) (6 demo(persp)g E> ?: #(> -(::?PK. >( ).>L?K ;.6. >;. -(:d
S(i<o[V O^^r l D) TLof l D) TS(i<o[V 3D) T#(>WDC rlgrV^r< D) T-WfC oog^Scr ?#D
X11 X11 pdf
2 3 4
D;. ]7B6.: )?:-KL".) L6. >;. ).F?$. #BOP.6: J;?$; OB:> P. B:.)
> layout(matrix(1:4, 2, 2))

E> ?: (3 $(B6:. -(::?PK. >( $6.L>. >;?: OL>6?N -6.F?(B:K" LKK(J?#7 >( P.>>.6
F?:BLK?. ;(J >;. ).F?$.
)?F?).)h

> mat <- matrix(1:4, 2, 2)


> mat
[,1] [,2]
[1,] 1 3
[2,] 2 4
> layout(mat)
4.2 Graphical functions

A.6. ?: L# (F.6F?.J (3 >;. ;?7;dK.F.K 76L-;?$LK 3B#$>?(#: ?# <g

plot(x) plot of the values of x (on the


symbols(x, y, ...) draws, at the coordinates given by x and y, symy
rect(x1, y1, x2, draws a rectangle which left, right, bottom, and top limits are
y2) x1, x2, y1, and y2, respectively
polygon(x, y)
4.4 Graphical parameters

E# L))?>?(# >( K(JdK.F.K -K(>>?#7 $(OOL#):_ >;. -6.:.#>L>?(# (3 76L-;?$: $L#


P. ?O-6(F.) J?>; 76L-;?$LK -L6LO.>.6:g D;." $L# P. B:.) .?>;.6 L: (->?(#:
(3 76L-;?$ 3B#$>?(#: TPB> ?> )(.: #(> J(6G 3(6 LKKW_ (6 J?>; >;. 3B#$>?(# par >(
How to customize a plot with R

2 1 0 1 2

Ten random values


lat

long
Petal.Length
o o
o o o o
o o o
o o o o
o o o
o o o
o
o
o

Petal.Width
8
7 8

Sepal.Length 6
oo o
oo o o o
oo
o o
5 o ooo
o
ooooo
oo
oo o o
o oo o o
5 6 oo
oo
Sepal
Length

Scatter Plot Matrix


5 Statistical analyses with R

5F.# O(6. >;L#


> aov.spray <- aov(sqrt(InsectSprays[, 1]) ~ InsectSprays[, 2])

D;. ]6:> :"#>LN ?: >( P. -6.3.66.) :?#$. ?> ?: $K.L6.6g


D;. 6.:BK>: L6. #(> )?:-KL".) :?#$. >;." L6. L::?7#.) >( L# (P
1.52.53.5
Partial for spray

3 2 1 0 1 2
0
1
2
3

spray
4
5
6
>?(#: J;?$; $L# P. B:.) >( .N>6L$>
> str(summary(aov.spray))
List of 1
$ :Classes anova and data.frame: 2 obs. of 5 variables:
..$ Df : num [1:2] 5 66
..$ Sum Sq : num [1:2] 88.4 26.1
..$ Mean Sq: num [1:2] 17.688 0.395
..$ F value: num [1:2] 44.8 NA
..$ Pr(>F) : num [1:2] 0 NA
- attr(*, "class")= chr [1:2] "summary.Td (num)Tjn avar Td (2)j 34.
>;?: KL>>.6 (PC.$>_ PB> ?# :(O. $L:.: L 3B6>;.6 L67BO.#> ?: #.$.::L6" K?G. 3(6
predict (6 updateg

add1 tests successively all the terms that can be added to a model
drop1 aom successively all the terms that can be removed from a model
tests
step selects a model with AIC (calls add1 and

757.elect12.4701 0 Td (del)Tj 14.87sq 4.8 0 0 -110.4 4752 Td 6872.28 cm644 /IM true /W 1 /H 1
6 Programming with R in pratice

+(J >;L> J. ;LF. )(#. L# (F.6F?.J (3 <s: 3B#$>?(#LK?>?.:_ K.> B: 6.>B6# >( >;.
KL#7BL7. L#) -6(76LOO?#7g Q. J?KK :.. L 3.6F?.Jrlgll^S l D) T:?O-K.WDC SVgrXVX l D) T?).L:WDC r^g[[X
(Intercept) x
31.683 5.377

[[2]]

Call:
FUN(formula = X[[2]])

Coefficients:
(Intercept) poly(x, 2)1 poly(x, 2)2
4.9330 1 mf5.o330
K?G. L#(>;.6 -6(76LOg E3 >;. B:.6 JL#>: :(O. 3B#$>?(#: >( P. K(L).) .L$;
>?O. J;.# < :>L6>:_ >;." $L# P. :LF
D6" ?> "(B6:.K3 J?>;h

> layout(matrix(1:3, 3, 1))


> ricker(0.1, 1); title("r = 1")
> ricker(0.1, 2); title("r = 2")
> ricker(0.1, 3); title("r = 3")

^l
7 Literature on R

Manuals. @.F.6LK OL#BLK: L6. )?:>6?PB>.) J?>; < ?# < A0%5i)($iOL#BLKih

An Introduction to R Z<d?#>6(g-)3b_
R Instal
withnto
R

You might also like