!
#
o
o
d
3
0
%
p
'
5i0el%(ood function
5 ,xampe of data import and ana!sis3 1o perform statistica tests, first cop! t#e
data into t#e cip"oard and t#en use t#e foowing commands in R3
data=?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK'
summar!%data=) #lists a summary of the data
attac#%data=' 5to make t#e varia"es defined in data= accessi"e to t#e consoe
t.test%var=,var>' ##!test between data contained in two variables! 6se
the names of the variables inside the object! 'n my case they were named
as var var)!
23/31
Tiessen (2009) Tutorial: Rapid intro to R.
#*xamples 6 *xercises
Make C!inder 2eig#t Mieage 1!pe
9okswagen 9C >::B >E )ma
-ord 9C >:CD :: )ma
,age 9C >DEB :: )ma
(cura 9E :>ED >B Medium
<uick 9E ::>D >: 0arge
C#r!ser 9E :CDB >> Medium
5Cop! t#e a"ove ta"e from word and t#en import data using t#e command
read.ta"e%fie?Kcip"oardK #eader?1' in t#e R consoe as foowing3
CarData?read.ta"e%fie?Kcip"oardK, #eader?1'
5 <oxpots are ver! usefu w#en comparing grouped data. -or exampe, side-"!-
side "oxpots of weig#ts grouped "! ve#ice t!pes are s#own "eow3
attac#%CarData'
"oxpot%2eig#t R1!pe' 5 create "oxpot of 2eig#t grouped "! 1!pe
title(.7eight by Fehicle #ypes.) 5 put tite to t#e grap#
"oxpot%Mieage RC!inder)
title(.2ileage by (ylinder.)
pot%Mieage, 2eig#t) 5 create a x,! scatterpot
title(.2ileage by weight.) 5 put tite to t#e grap#
#*xample of A33I/
Rep Plot Entry En# $ield
1 1 A ,= 3.3
2 4 A ,= 3!.9
1 2 B ,= 49.9
2 5 B ,= 50.3
1 3 % ,= "0.1
2 6 % ,= "2.3
1 1 A ,> 44.4
2 4 A ,> 4.3
1 2 B ,> 22.3
2 5 B ,> 20."
1 3 % ,> "5.4
2 6 % ,> ".!
1 1 A ,: "2.4
2 4 A ,: ".3
1 2 B ,: 92.3
2 5 B ,: 92."
24/31
Tiessen (2009) Tutorial: Rapid intro to R.
1 3 % ,: 90.3
2 6 % ,: 94.
5cop! t#e a"ove data into cip"oard and t#e t!pe t#e foowing commands
dataD?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK' 5 import data
attac#%dataD' 5to make t#e varia"es defined in data= accessi"e to t#e consoe
i"rar!%agricoae' #load and activate library
mode? &22'(9nv, 9ntry, 8ep, ?ield, graph"KbiplotK,number">&BA9)
(
< C
-C -> B > C
-
=
B
=
>
:
*C =
*
C
>
,>
,:
=
>
*C ]
LD.=
=C.@
5-or c#anging t#e range of t#e axes use xim and !im3
mode? &22'(9nv, 9ntry, 8ep, ?ield, xlim"c(-0,0),ylim"c(-1,1),
graph".biplot.,number">&BA9)
5Conditiona averaging
5 use t#e same datata"e as a"ove %dataD'
dataD?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK' 5 import data
dataDQavRep?ave%dataDQGied,dataDQRep' 5 create new coumn wit# averages
among Reps
dataDQav,ntr!?ave%dataDQGied,dataDQ,ntr!' 5 create new coumn wit# average
among ,ntries
#*xample of grain %ield trial data
RE& B'( &')T E*TR$ RA*+E ')*+R), GYF AD ASI PH EH EPO EPP
1 1 1 1 1 ==.BB EB.B ->.B =LD.B @E.B B.D> =.:D
1 1 2 5 1 2 L.EB DL.B =.B =@C.B @@.B B.D= B.@E
1 1 3 2 1 3 A.CE EB.B B.B =ED.B AB.B B.C> =.BB
1 1 4 2" 1 4 =B.@@ E:.B =.B =@>.B =B@.B B.DA =.BL
1 1 5 11 1 5 ==.>: E:.B ->.B >>C.B =>B.B B.DC =.BC
1 2 12 1 ==.:> EC.B =.B >BD.B =B@.B B.D: =.BB
1 2 ! 1 1 ! L.>B EB.B B.B >=B.B =B@.B B.D> B.@=
25/31
Tiessen (2009) Tutorial: Rapid intro to R.
1 2 " 19 1 " =B.B= E=.B :.B >=D.B ==:.B B.D: B.@E
1 2 9 2! 1 9 =>.CE E:.B =.B >=A.B =B@.B B.DB =.BB
1 2 10 2 1 10 =:.=@ EC.B B.B >=D.B ==:.B B.D: =.>C
1 3 11 25 1 11 =B.>: EC.B ->.B >::.B =BL.B B.CE =.BB
1 3 12 9 1 12 ==.EC E:.B -:.B =LB.B LD.B B.CA =.=:
1 3 13 4 1 13 A.BL EB.B =.B >BD.B L>.B B.CB =.CB
1 3 14 1" 1 14 D.:> EE.B B.B >BL.B @>.B B.CC =.BA
1 3 15 13 1 15 L.@E ED.B B.B >=:.B =B:.B B.CL =.BB
1 4 1 3 1 1 @.E= EB.B =.B >=D.B ==:.B B.D: =.BC
1 4 1! 14 1 1! ==.>B EB.B =.B >B:.B @>.B B.CD =.BB
1 4 1" 30 1 1" ==.@L ED.B B.B >B@.B =B>.B B.C@ B.@=
1 4 19 20 1 19 D.@L EC.B =.B >B:.B =B@.B B.DC B.LD
1 4 20 ! 1 20 E.D: EC.B =.B =AC.B @B.B B.D> B.@C
1 5 21 23 2 20 A.DD EC.B =.B =@D.B LL.B B.CD =.=>
1 5 22 10 2 19 ==.:B EC.B =.B >=:.B =BE.B B.DB B.@E
1 5 23 1! 2 1" @.BB E:.B =.B >=L.B =BE.B B.C@ =.BB
1 5 24 " 2 1! L.C= EE.B B.B >BC.B @:.B B.CE B.@E
1 5 25 24 2 1 =B.BE EE.B B.B >:B.B ==D.B B.DB =.BB
1 2 29 2 15 ==.D> ED.B =.B >=B.B ==L.B B.DE =.B@
1 2! 21 2 14 E.L> E=.B =.B >BL.B =BC.B B.DB B.AE
1 2" 1 2 13 =>.@: EB.B =.B >B:.B =BD.B B.D> B.@E
1 29 15 2 12 A.:E E=.B =.B >=>.B ==:.B B.D: B.LA
1 30 22 2 11 @.EE EB.B =.B >BL.B =BA.B B.D= B.@>
2 1 31 2" 2 10 =>.>D EB.B =.B >:@.B =:B.B B.DC =.=E
2 1 32 25 2 9 @.LL E=.B =.B >>B.B =>=.B B.DD B.@E
2 1 33 15 2 " A.@= E>.B C.B >:B.B =>:.B B.D: B.@>
2 1 34 14 2 ! =B.D: DA.B B.B >==.B =BL.B B.D= =.BD
2 1 35 1 2 E.LB DA.B -=.B >=B.B @D.B B.CD B.@D
2 2 3 2 5 =B.:E DL.B B.B >>B.B ==A.B B.D: =.BC
2 2 3! 2! 2 4 =B.@E EB.B =.B >>:.B =>=.B B.DC =.BB
2 2 3" 13 2 3 =:.LE E=.B =.B >B@.B ==:.B B.DC B.@E
2 2 39 10 2 2 =>.DD EB.B =.B >=A.B =B>.B B.CA B.@>
2 2 40 21 2 1 @.L> E=.B ->.B >BA.B @>.B B.CC =.BD
2 3 41 1" 3 1 =>.>@ E=.B =.B >=A.B =B>.B B.CA =.=A
2 3 42 22 3 2 @.>E E>.B B.B >=L.B =>C.B B.DA =.BE
2 3 43 11 3 3 =:.>> EB.B ->.B >=@.B =BC.B B.CA =.=@
2 3 44 3 3 4 E.@B EB.B -=.B >=@.B =BL.B B.C@ B.L:
2 3 45 " 3 5 @.CL EB.B =.B >>D.B =>:.B B.DD =.BB
2 4 4 12 3 =>.:A EB.B B.B >BB.B =B:.B B.D> =.BC
2 4 4! 20 3 ! L.A> D@.B -=.B >>C.B =B:.B B.CE =.BB
2 4 4" 2 3 " A.:B EB.B =.B =@B.B @A.B B.D= B.L=
2 4 49 1! 3 9 A.EC EB.B =.B >=A.B L:.B B.:L =.BB
2 4 50 29 3 10 ==.L> ED.B B.B >BD.B @A.B B.CA =.BD
2 5 51 9 3 11 =B.>: E=.B B.B >BC.B ==D.B B.DE =.=C
2 5 52 19 3 12 L.CE E=.B =.B >B@.B =B=.B B.CL =.BD
2 5 53 ! 3 13 L.=B EB.B =.B =A=.B AC.B B.C: =.BD
2 5 54 5 3 14 E.C: DL.B B.B =L:.B AE.B B.C> =.BB
2 5 55 23 3 15 A.D: EB.B =.B =@D.B @B.B B.CE =.C:
2 5 30 3 1 =B.C@ EE.B B.B =L@.B LE.B B.CE =.>B
2 5! 4 3 1! A.:> EB.B =.B =@C.B @E.B B.C@ =.=B
2/31
Tiessen (2009) Tutorial: Rapid intro to R.
2 5" 24 3 1" D.EB E:.B >.B =@=.B LL.B B.CE B.@:
2 59 2 3 19 =B.@> EB.B =.B >>A.B =BA.B B.CA =.:B
2 0 1 3 20 =B.@E EB.B =.B =@A.B @E.B B.C@ =.BD
5Cop! t#e a"ove ta"e and t#en t!pe t#e foowing commands in t#e R Consoe3
1riaData?read.ta"e%fie?Kcip"oardK, #eader?1'
51#en do exporator! data ana!sis.
pairs%1riaDataOA3=:P, ower.pane?pane.smoot#, upper.pane?pane.cor>,
diag.pane?pane.#ist'
5t#is assumes t#at !ou #ave previous! defined t#e functions pane.#ist and
pane.cor> as presented earier in t#is tutoria.
GG-
DL E> EE
B.BBC>
B.E>
B.B:=
B.=L
=LB >>B
B.B@>
B.B=@
B.=E
B.BB=E
B.CB B.DD
B.=>
B.BBA
E
=
B
=
C
B.BE:
B.BD:
D
L
E
>
E
E
(D
B.BBBCE
B.LA
B.BBB>@
B.@
B.BB=E
B.AE
B.BBC:
B.E>
B.BBBD
B.LE
()I
B.BB>@
B.EL
B.B>A
B.>=
B.B:E
B.=D
-
:
B
>
C
B.BCC
B.==
=
L
B
>
>
B
*7
B.DL
=.>e-=>
B.BL
B.B>L
B.BBD>
B.DL
,7
B.E@
=.Le-=E
A
B
=
B
B
B.B>L
B.>=
B
.
C
B
B
.
D
D
,*8
B.B:D
B.=E
E =B =C -: B > C AB =BB B.L =.= =.C
B
.
L
=
.
=
=
.
C
,**
attac#%1riaData' 5make varia"es accessi"e
"oxpot%GG- R,.1RG' 5 create "oxpot of GG- grouped "! categor! ,.1RG
2!/31
Tiessen (2009) Tutorial: Rapid intro to R.
"oxpot%GG- R,.1RG, notch"#) 5 )ame as a"ove "ut wit# notc# option set to
1R/,. If t#e notc#es of two pots do not overap t#is is ^strong evidence_ t#at
t#e two medians differ
"oxpot%GG- RR,*' 5 create "oxpot of GG- grouped "! categor! R,*
a?aov%GG- R,.1RGDGBHD89=D8&%<9DB$%<8$7) 5 define a mode for t#e anova of
GG-
summar!%a' 5 s#ow summar! of anova
pairwise.t.test%GG-, <0;' 5 make t.tests of GG- grouped "! categor! <0;
51#e pairwise.t.test function is +uite usefu since it makes 1-tests wit# a possi"e
two groups and provides a matrix of * vaues. 1#is aows identif!ing groups
t#at are significant! different from eac# ot#er. /nfortunate!, it seems t#at R
can on! #ande a"out ess t#an >B groups.
50inear modes et !ou find causa reations#ips of t#e dependent varia"es. (
"reeder, for exampe, woud "e interested in modeing grain !ied in
dependence of ot#er tria parameters and secondar! traits. Cop! t#e ta"e wit#
grain !ied data and t#en enter foowing command in R for modeing.
1riaData?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK'
attac#%1riaData' 5make varia"es accesi"e
mode=?m%GG- RR,*N<0;N*081N,.1RGN(DN()IN*7N,7N,*8N,**'
summar!%mode=' 5 s#ow summar! of t#e inear mode
5 t#e m coefficients et !ou know #ow muc# is t#e dependence from t#e ot#er
varia"e, w#ereas t#e p vaues indicate if t#e reations#ips are significant or not.
5Construct a new varia"e caed ),0I.D,X taking into account t#e coefficient of
t#ose varia"es w#ic# were significant in t#e inear mode. In t#is case use GG-,
,** and ()I
1riaDataQ),0I.D,X?GG-N,**Y:.D-()IYB.C 5Define a new varia"e caed
),0I.D,X "ased on a inear com"ination of ot#er varia"es GG-, ,** and
()I. 1#e coefficients given "! t#e inear mode were used as weig#ting factors
for t#is seection index.
"oxpot%GG- R,.1RG'
2"/31
Tiessen (2009) Tutorial: Rapid intro to R.
-
= : D A @ == =: =D =A =@ >= >: >D >A >@
E
L
=
B
=
>
=
C
+$.
5-rom t#e a"ove "oxpot, decide w#ic# is t#e #ig#est !ieding entr!6 Is it entr!
=B,==,=>, =: or >E6 (re t#e differences significant6 2e can pot t#e seection index
to find an answer.
"oxpot%),0I.D,X R,.1RG'
-
= : D A @ == =: =D =A =@ >= >: >D >A >@
=
B
=
>
=
C
=
E
/E'0*1E2
title(3ain45/ele6tion 0nde75) - to put a title to t8e 9rap8
-.ro3 t8e a:o#e :o7plot it 6an :e seen t8at entry 11 is si9ni;i6antly :etter t8an entry 10<
12< 13 and 2. T8is =as not si9ni;i6ant i; only loo>in9 at +$. para3eter (pre#ious
:o7plot). T8is 8i98li98ts t8e use;ulness o; in6ludin9 se6ondary traits into t8e analysis.
29/31
Tiessen (2009) Tutorial: Rapid intro to R.
# /ome interesting codes
51o add an extra coumn containing t#e rank to a ragged arra! indexed "! more
t#an one grouping factors3
rank.ists H-wit#% data=, tapp!% !ied, ist% site?site, !ear?!ear ', rank ' '
51o add an additiona coumn ``rankaa containing t#e rank of t#e ``!iedaa of
t#e different varieties in reation to t#e indices ``!earaa and ``siteaa to t#e data=
o"&ect. I ac#ieved to cacuate t#e ranks "ut I do not manage to merge t#is resut
to t#e origina dataframe.
mode= H- me%mat# R grade, mat#data, random?RgradeSstudentid'
#*$emplo en espa7ol
5/tiice #ep%' para aprender mas de as funciones +ue va a utii$ar para anai$ar
sus datos. *ara importar un arc#ivo, o me&or usar a funcibn fie.c#oose%' para
escoger cua+uier arc#ivo de texto en cua+uier directorio.
data= ? read.ta"e%fie.c#oose%', #eader?1'
data&'read.ta#lefile',clip#oard,- (eader'T- sep',.t," 5importar datos de
cip"oard
summar!%data=' 5 0e da un resumen de as estadsticas "csicas
attri"utes%data=' 5 0e da os Katri"utosK
attri"utes%data='Qnames 5 0e da os nom"res de as varia"es
summar!%data=Qong' 5 tam"ien da e resumen de a varia"e KongK dentro de
o"&ecto data=
5 para acceder una varia"e sin e commando attac# ponga e nom"re de o"&eto
seguid de sim"oo de pesos Q
attac#%data=' 5 (nexa os datos de manera +ue R sepa de a existencia de esas
varia"es sin necesidad de referirnos a o"&eto.
pot%x,!' 5 graficar una varia"e contra otra
#8losar% of fre9uent R functions
? name -does t8e sa3e as 8elp(na3e)
a=aov(var1~var2) #defines a new object a with the results of the
analysis of variance
attach(data1) -to 3a>e t8e #aria:les de;ined in data1 a66essi:le to t8e 6onsole
attach(object) -3a>es t8e ele3ents o; o:?e6t a#aila:le to :e 6alled dire6tly
attributes(data1) -lists t8e atri:utes o; data1: #aria:le and ro= na3es
boxlot(data1) -:o7plot
colot(var1 ~ var2 ! var3)
data1=edit(data"#rame()) - opens a =indo= to de;ine data1 3anually
#ix(data1) - allo=s to edit and ;i7 an e7istin9 o:?e6t
30/31
Tiessen (2009) Tutorial: Rapid intro to R.
data1=read"table(#ile"choose()$header=%) - sele6t ;ile =it8 a popup =indo=
data1=read"table(#ile=&cliboard&) -i3port data ;ro3 6lip:oard. ,or>s only i; t8ere are no e3pty 6ells
data1=read"table(#ile=&cliboard&$ header=%$ se=&'t&) -i3port data ;ro3 6lip:oard. ,or>s e#en i;
e3pty 6ells are present
data1=read"table(#ile=&cliboard&$ header=%$ se=&'t&$ na"strin(s=&&) -i3port data ;ro3 6lip:oard.
,or>s e#en i; e3pty 6ells are present! >ills empty cells with %&!
detach(data1) -to deta68 t8e #aria:les de;ined in data1
hel (name) -pro#ides 8elp a:out 5na3e5
hist(data1) -si3ple 8isto9ra3
ls() -s8o=s t8e #aria:les in t8e =or>spa6e
names(table) -=8at are t8e #aria:les inside t8e ta:le
airs(data1) -splo3 plot
air)ise"t"test(var1$ var2) - 3a>e t.tests o; #ar1 9rouped :y 6ate9ories #ar2. T8e result is a pair=ise
6o3parison 3atri7.
lot(data1) -si3ple s6atter plots
rm(list = ls()) -re3o#es all #aria:les ;ro3 t8e =or> spa6e
rm(variable) -re3o#es t8at #aria:le or o:?e6t
round(cor(data1)$2) -6orrelation 3atri7 rounded to 2 de6i3als
summar* (lm(var1~0+var2*var3)) #summary of the results of a linear model
with interactions
summar* (lm(var1~var2)) #summary of the results of a linear model
summar*(a) #summary of the contents of object a
summar*(aov(var1~var2)) #summary of the analysis of variance
summar*(data1) #lists a summary of the data
t"test(var1$var2) ##!test between two variables! 6se the names of the
variables inside the object! 'n my case they were named as var1, and
var)!
t"test(var1~var2) ##!test within data of variable1 grouped by categories
specified in variable )! <rouping factor of var) must have exactly )
levels!
31/31