Anda di halaman 1dari 31

Tiessen (2009) Tutorial: Rapid intro to R.

Tutorial: Rapid intro to R


Dr. Axel Tiessen Favier
Departamento de Ingeniera Gentica
Cinvestav-Irapuato, Mxico.
www.ira.cinvestav.mx

Install programs
R is a software package especia! suita"e for matrix data ana!sis and grap#ica
representation. R can "e used as a statistica too "ut aso as a programming
anguage itsef, making it ver! fexi"e and #ig#! customi$a"e. Grap#ica toos
make R an idea environment for exporator! data ana!sis and for preparing
pu"ication read! figures %exporta"e as .&pg fies'. ( work is done in command
st!e text functions and t#erefore it is different from ot#er windows st!e programs
%ike )*))' t#at use menus wit# seect and cick options for predefined statistica
procedures. It takes considera"e time to earn to use R, "ut once !ou #ave passed
t#e first "urden, it is +uite convenient to #ande. (s a programming anguage it is
not for "eginners, "ut rat#er for advanced users for w#om t#e statistica functions
of Microsoft ,xce are no onger enoug#. -or exampe if !ou want to do *rincipa
Component (na!sis. In comparison to )() and )*)) w#ic# are ver! expensive
commercia programs for doing statistics, R is free software distri"uted under t#e
G./ and G*0 icense terms. 1#ere are versions of R for /nix, 2indows and
Macintos# at t#e officia CR(. we"site3
#ttp344cran.r-pro&ect.org4
-oow t#e downoad instructions to insta R oca! on !our computer. 2#en
running t#e program !ou wi "e presented wit# t#e R Consoe window.
5 I strong! recommend t#at !ou insta a text editor program ike 1inn-R. 1#is can
"e used to store scripts and send command ines direct! into R. Get t#e editor
for free at t#e we"site3 #ttp344www.sciviews.org41inn-R4
1/31
Tiessen (2009) Tutorial: Rapid intro to R.
#Tutorial: Rapid intro to R
# Dr. Axel Tiessen Favier
# Cinvestav-Irapuato
5 2#en I instaed t#e R program and first saw t#e R consoe, I asked m!sef3 now w#at6 7ow do I
enter m! data6 7ow can I do a simpe 1-test or (.89( wit# t#is program6 ,ven t#oug# t#ere
are man! R tutorias on t#e internet expaining t#e man! functions and features of R, it was
+uite tedious for me to start using R in a meaningfu wa!. I #ad to spend over : weeks reading
instructions and documents to get to a point w#ere I coud use R in a productive wa!. I do use
,xce for a m! "asic cacuations and some ana!sis. I ike ,xce ver! muc#, "ut I am aso
aware of its imitations for more advanced statistica ana!sis, for exampe for doing *C(s, ;-
means custering or genera inear modes.
5 1#erefore, I decided to write t#is tutoria as a s#ort cut introduction to R wit# practica exampes of
statistica ana!sis. M! intention is not to s#ow a t#e features of R, "ut rat#er to ist some
commands and functions t#at I common! use for m! work. I do researc# wit#in t#e fied of
"ioogica sciences, particuar! in pant "ioc#emistr!, meta"oomics, mai$e "reeding and
microarra!s.
5 1#e R consoe #as a prompt in w#ic# !ou can t!pe t#e commands direct!. In t#e foowing I wi
put some ines in "ue. 1#is refers to command code for R. 1#e 5 sign denotes comments t#at
are not interpreted "! t#e R consoe. I use t#em #ere to expain some features of R. Cop! t#e
ines containing "ue text and paste t#em into t#e R consoe. Instead of doing cop!-paste from
2ord to R, !ou can aso open t#is document in 1inn and seect t#e "ue ines and t#en cick on
t#e "utton send ine to R. <! o"serving t#e output !ou wi earn to use t#e R program.
# Introduction to R !asic usage"
5 R uses o#$ects and functions. 1#e o#$ects contain data, w#ie t#e functions
use t#e data inside t#e o"&ects and perform cacuations and give outputs. If !ou
want to see a functions of R t#en t!pe3
#ep%' 5 wi open a window wit# genera #ep info %ist a functions'
#ep%var' 5 specific #ep on t#e function var %variance'
#ep%sd' 5 specific #ep on t#e function sd %standard deviation'
6sd 5 aternative wa! to ca for #ep on t#e function sd
5Most functions in R need an open and cosed "racket for entering varia"es or
attri"utes for t#e respective function. 1#is is needed even t#oug# sometimes t#e
"rackets are eft empt!.
5,ver!t#ing !ou t!pe in R is case sensitive. (s in unix command ine, in R !ou
can recall previousl% t%ped commands using t#e up and down arrow ke!s.
5 Defining and editing o#$ects manuall%
51o work wit# R !ou first need to define o#$ects. (n o"&ect can "e a varia"e, a
vector, a dataframe or a matrix, depending on t#e amount of data and its
structure. C#oose a name for an o"&ect t#at is not a name for a function. )ome
possi"e names for o"&ects are3 a, ", c, x, !, $, data=, data>, matrix=, mode=,
etc.
" ? @ 5to create a varia"e named " wit# on! = vaue. If !ou need entering more
vaues t#en use t#e com"ine function as foows3
x ? c%=,:,>,A,@' 5t#e c function creates o"&ects wit# severa vaues %vector'
! ? c%a?=,a?:,"?>,c?A,c?@,d?>,d?:' 5define ! wit# varia"e names and vaues
2/31
Tiessen (2009) Tutorial: Rapid intro to R.
$?matrix%c%=B,>B,:B,CB,DB,EB',nco?>' 5define matrix wit# > coumns. 9aues are
e+ua! distri"uted into t#e two coumns. 1#e first coumn is fied first and
downwards.
"F xF !F $ 5s#ow t#e contents of o"&ects ", x, ! and $
5 Gou can c#oose severa s!m"os to define o"&ects. Gou can use t#e s!m"o ?,
"ut aso wit# t#e s!m"os of H- or aso -I depending on t#e side in w#ic# !ou
put t#e varia"e. I prefer to use ? "ecause it is on! one ke!"oard strike, "ut for
some o"scure reason most R tutorias use H- .
x ? c%=,:,>,A,@' 5create an o"&ect x wit# D components
x H- c%=,:,>,A,@' 5aternative option to create t#e same o"&ect x
c%=,:,>,A,@' -I x 5 aternative option to create t#e same o"&ect x. .ote t#at t#e
orientation of t#e s!m"o is inverted if t#e varia"e is on t#e rig#t side
5 If !ou are not ver! famiiar wit# command ine st!e, or if !ou are more used to
spreads#eet st!e, a "etter option to define a matrix o"&ect is wit# t#e
edit%data.frame%'' function
data& ' editdata.frame"" # opens a window to define data1 manually
3/31
Tiessen (2009) Tutorial: Rapid intro to R.
5
5(fter !ou enter t#e data and cose t#e window of t#e spreads#eet st!e editor,
!our input is saved into t#e o"&ect data=. <! cicking on t#e coumn a"es !ou
can define if t#e vaues s#oud "e numerica or categorica. Gou can give
names to !our varia"es or eave t#e defaut names %var=, var>, var:, etc'. If
!ou c#ange t#e names of t#e varia"es, use a s#ort and eas! to remem"er
name, so t#at !ou can #ave eas! access to it ater %attac# function'. Do not
use space or strange c(aracters for t(e varia#le names. 1o see w#at it is
stored in t#at matrix, simp! t!pe t#e name of t#e o"&ect. 7ere I used Jdata=K
"ut !ou can define an! ot#er simiar name.
5 T(e first #urden for using R is to enter arge amounts of data into o"&ects. 1#e
manua entr! of data as descri"ed previous! is sometimes neit#er practica nor
convenient. 1#ere are "etter options to enter data from ot#er fies into R. Gou
can import data from t#e windows clip#oard. 1#is works fine for sma and
medium si$e datasets. -or extreme! arge data sets %suc# as microarra!s wit#
over DB t#ousand data ines' !ou can import data from external text files.
5 )ploading data from Clip#oard
5 Rat#er t#an t!ping t#e data manua! wit# t#e edit data.frame function, it is
possi"e to import data from *xcel or +ord via t(e windows clip#oard. -or
doing t#is, open !our exce fie or word document and cop! t#e data ta"e
excuding or incuding row names and coumn #eadings. 1#en go to t#e R
consoe and t!pe t#e command3
data&'read.ta#lefile',clip#oard,- sep',.t," 5for importing wit#out row names
and coumn #eadings. Coumn varia"es wi "e named 9=, 9>, 9:,
consecutive!. If t#e first ce is empt! t#en t#is aows automatic recognition of
row names and #eadings. 1#is command creates t#e o"&ect Jdata=K wit# t#e
contents of t#e windows cip"oard. It transfers t#e data from t#e cip"oard
memor! to t#e R program memor!. 1#is works fine for ,xce, 2ord, *ower*oint
and man! ot#er windows programs. -or me t#is is t#e most convenient wa! to
4/31
Tiessen (2009) Tutorial: Rapid intro to R.
import data into R, since I do not need to create text fies nor create foders or
define working directories.
data&'read.ta#lefile',clip#oard,- (eader'T- sep',.t," 5for forcing import wit#
row names and coumn #eadings. 1#is option works even if t#e first ce is not
empt!.
5,xampe of a ta"e wit# first ce empt!3
GYF AD ASI
A ==.BB EB.B ->.B
A L.EB DL.B =.B
B A.CE EB.B B.B
B =B.@@ E:.B =.B
51a"e wit# first ce fied.
Entry GYF AD ASI
A ==.BB EB.B ->.B
A L.EB DL.B =.B
B A.CE EB.B B.B
B =B.@@ E:.B =.B
5Important notice3 1#e text for t#e #eading names must not incude spaces or
ot#er strange c#aracters. (so, t#e data matrix !ou cop! into t#e cip"oard must
not contain empt! ces. If it does, an error message wi appear in t#e R
consoe3 ,rror en scan%fie, w#at, nmax, sep, dec, +uote, skip, nines, na.strings, 3 a inea = no tiene A
eementos. 1o avoid suc# error messages, do not use names wit# spaces and fi
a empt! ces in ,xce wit# .(.
5,xampe of a ta"e wit#
empt! ces
5Corrected ta"e wit# .( fied
ces
5 If !ou #ave man! empt! ces in exce, t#en additiona options can correct data
recognition and aow t#e import of ta"es wit# empt! ces and spaces. Most
important! is t#e option sep',.t, to avoid importing errors.
data&'read.ta#lefile',clip#oard,- (eader'T- sep',.t,"
data&'read.ta#lefile',clip#oard,- (eader'T- sep',.t,- na.strings',,"
5/(ort note on data import: )ome tutorias recommend !ou to export data from
,xce s#eets into text fies, and t#en import t#e data "ack to R. 1#is is totall%
unnecessar% "ecause !ou can use t#e cip"oard direct! for data import.
.evert#eess, sometimes !ou wi #ave ver! arge amounts of data t#at cannot
fit into t#e cip"oard. -or exampe, w#en doing microarra!s of ten t#ousand
genes. -or t#ese cases, !ou must use t#e read.ta"e function to import !our
text fie as foows3

5/31
Entry G Y AD ASI
A ==.BB EB.B ->.B
A L.EB DL.B =.B
B A.CE B.B
B =B.@@ E:.B =.B
Entry GY AD ASI
A ==.BB EB.B ->.B
A L.EB DL.B =.B
B A.CE .( B.B
B =B.@@ E:.B =.B
Tiessen (2009) Tutorial: Rapid intro to R.
5 )ploading data from text files
51#e R program needs to define a foder to upoad and save fies. 1o c#ange t#e
director! in w#ic# !ou want to work go to t#e menu -I-ie -I C#ange director!
and seect a foder or director! of !our c#oice. Gou can aso do it manua! on
t#e consoe3
setwd%KC34(xe )tatisticsRK' # sets the working directory manually
getwd%' 5dispa!s t#e pat# for t#e working director!
dir%' 5 ists t#e fies in t#e current work director!
5 for importing data from a ta" deimited text fie !ou #ave severa options3
data=?read.ta"e%Kfie.txtK, #eader?1, sep?KMtK, row.names ? ='
5 1#e #eader option 1 or - ets !ou specif! if !our data #as a #eader in t#e first
row. 1#e sep?KMtK option ets !ou define t#e separation s!m"o "etween vaues,
in most cases separated "! ta". 1#e sep?KMtK option wi not work for fies wit#
format csv %comma separated vaues', in t#ose cases use t#e option sep?K,K.
1#e row names option ets !ou specif! names for t#e rows.
5 1#e text fie must "e ocated in t#e defaut director! of R. /se setwd to define t#e
correct director!. ( more convenient wa! to upoad fies is wit# t#e fie.c#oose%'
command. 2it# t#is function !ou do not need to remem"er or t!pe t#e fiename
manua!. Gou can aso "rowse t#e foder wit# windows. -or importing data
from standard ta" deimited text fie use t#e command3
data1=read.table(file.choose(),header=T) 5 seect fie wit# a popup window
5 If !our datafie #as no varia"e names in first row t#en use option #eader?-
Remem"er t#at ever!t#ing !ou t!pe in R is case sensitive. Remem"er t#at !ou
can reca previous commands using t#e up and down arrow ke!s to correct an!
t!ping mistakes.
5 Anal%sis of o#$ects
5If !ou #ave defined an o"&ect x, !ou can do simpe t#ings ike3
x # shows data stored in the object x on screen
sort%x' # list data increasing order
engt#%x' # number of items in x
52it# data o"&ects stored in t#e workspace one can perform man! individua
functions ike sum(x), max(x), min(x), median(x), mean(x), but it is
better to use the summary function that integrates several of them:
summar!%data=' # shows a summary of x (min, max, mean, median,
uartiles)
sd%data=, na!rm " #) #standard deviation of elements in data1! $ption
of %&s removed
/31
Tiessen (2009) Tutorial: Rapid intro to R.
sd%data='4s+rt%engt#%data=)) #gives the standard error of data in data1
var%data=, na!rm " #) #variance value, or covariance matrix of
elements in data1! $ption of %&s removed
# /(ort note on function var(). 'f only given one dimensional vector
object, the result is the total variance of the dataset! 'f the object
is a more dimensional matrix, then var() gives a covariance matrix,
euivalent to the function cov()! 'n a covariance matrix, elements in
diagonal are variances within that variable, and the other elements
are the covariances between different variables! (ovariance is a
measure of how much the variables are independent or correlated! &
covariance matrix can be numerically scaled to a correlation matrix
with the function cov)cor()!
pot(data1) #matrix correlation plots with all elements in data1
pot(data1*):+,) #matrix plots with selected columns )-+ of data1
cor%data=, use " .pairwise!complete!obs.) #correlation coefficient
matrix of all elements in data1
round%cor(data1, use " .pairwise!complete!obs.),)) #correlation matrix
rounded to ) decimals
round%cor(data!frame(data1/var1, data1/var), data1/var0)),))
#correlation matrix rounded to ) decimals with some selected variables
only
round%cor(data1*1:1,),)) #same as previous with columns 1 to 1 selected
5 Defining new o#$ects
5 -unctions t!ped aone s#ow t#e resuts on! on t#e screen. If !ou need to do
furt#er cacuations wit# t#e resuts of a given function, !ou need to use o"&ect?
to define a new o"&ect w#ic# wi store t#e output in t#e workspace memor!. -or
exampe3
matrix1"var(data1) # stores the covariance matrix in new object called
2atrix1
matrix)"cor(data1) # store the correlation coefficient matrix of
elements in data1
matrix13 matrix) #shows the contents of the two newly created objects!
#he semicolon allows separating two functions given in the same line
5( o"&ects are stored in t#e R(M memor! of R. 2#en using R no fie is written to
t#e #ard-disk uness !ou save it specifica!. 2#en !ou +uit R program !ou can
save t#e workspace so t#at a o"&ects created during t#e current session are
avaia"e next time !ou work wit# R.
s%' #to list all objects stored in the current workspace
rm%x' #to remove object x from workspace
matrix= 5 to ist t#e content of t#e Matrix= o"&ect in t#e consoe window
edit%data=' 5 It ets !ou print editions on screen "ut it does not save c#anges in
t#e o"&ect memor!. 1o edit and save c#anges use t#e foowing command3
data=?edit%data=' 5 to re-edit t#e contents of o"&ect data= and saving it
!/31
Tiessen (2009) Tutorial: Rapid intro to R.
fix%data=' 5 a simper command t#at does t#e same as t#e a"ove
5 +or0ing wit( matrices
5 *erform some matrix manipuations ike3
data:?)4data1 # define a new matrix with scalar matrix multiplication
"! factor >
dataC?data=Ndata: # matrix addition
dataD?sove%data=' 5 define a new matrix using t#e inverse matrix of data=
data)"t(data1) # define a new matrix using the transposed matrix from
data1
dataE?data.frame%data=,data:' 5 use data.frame for adding eements to o"&ects
5 ,xtracting t#e component of a matrix invoves one or two indices Orow, coumnP.
data=O>30, #elements of data1 at the second and third columns
$?data1*):0, #define new object with selected columns from data1
data=O>,:P #element of data1 at the second row, third column
data:?read.ta"e%fie?Kcip"oardK, #eader?-, sep?KMtK' 5 import data wit#out
#eader
data:?data.matrix%data:' 5 convert data.frame to a data matrix.
5 ,igenvaues and eigenvectors of a matrix is #anded "! eigen() function3
m>?matrix(c(15,)5,05,15),ncol"))
eigen%m>'
#Attac( and detac(
5 1#e attac# function is used to access t#e varia"es inside an o"&ect. 6se detach
to get rid of those variables!
attac#%data=' 5 aows direct access to t#e varia"es defined inside an o"&ect
hist(var1) # plots an histogram of variable var1 inside attached
object data1
detac#%data=' 5 detac#es varia"es
hist(data1/var1) # plots an histogram of var1 from data1 without
attaching
#Conditional functions
5conditiona recoding of vaues wit#in an o"&ect
data=? x ? rnorm%=BBB' 5create a set of =BBB random vaues
xOx I =.>P ? .( 5 to recode a vaues of x I =.> to .(
data=? data.frame%a ? rnorm%=BBB', "? rnorm%=BBB' , d?rnorm%=BBB', entr!?c%=3=B''
data=QaOdata=Q" I =P ? D 5 to recode a vaues of " I = to D
"/31
Tiessen (2009) Tutorial: Rapid intro to R.
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
9/31
Tiessen (2009) Tutorial: Rapid intro to R.
#Creating grap(s
5 1#ere are man! simpe functions to pot grap#s using the data in the
objects!
pot(data1) #graphical output with plot arrays of elements in
data1! #his graphical feature is particularly impressive since it
plots all variables of the matrix against all variables! 7ith this
feature of 8 you can do exploratory analysis and discover some hidden
correlations! 'f you want to do this in 9xcel, you will need much more
time! (later in the tutorial ' will present a more informative version
of multiple scatterplots)
GG-
DL E> EE =LB >>B B.CB B.DB
E
L
=
>
D
L
E
>
E
E
(D
()I
-
:
-
=
=
:
=
L
B
>
>
B
*7
,7
A
B
@
B
=
>
B
B
.
C
B
B
.
D
B
,*8
E L => -: -= = : AB @B =>B B.L =.= =.C
B
.
L
=
.
=
=
.
C
,**
50ater in t#e tutoria I wi expain #ow to make specia scatterpots.
"oxpot(data1) #graphical output with boxplots of all elements in
data1
attac#%data=' 5to make t#e varia"es defined in data= accesi"e to t#e consoe
var= 5 to s#ow t#e contents of var= %defined inside attac#ed data='
"oxpot%var=, var>' #produces boxplots of var1 and var)
"oxpot%var= R var>) #produces a boxplot of var1 grouped by factor var)
pot(data!frame(var1, var), var0)) #plot arrays of some selected
elements only!
pot(data1*1:1,) #plot only selected columns 1 to 1 of data1
detac#%data=' 5to detac# t#e varia"es defined in data=
data=Qvar= 5 to s#ow t#e contents of var= from data= wit#out t#e attac# function
10/31
Tiessen (2009) Tutorial: Rapid intro to R.
5 If !ou want to get t#e statistics invoved in t#e "oxpots, t#e foowing commands
s#ow t#em3
"?"oxpot%data='F "/stats # gives t#e vaue of t#e ower end of t#e w#isker, t#e
first +uartie %>Dt# percentie', second +uartie %median?DBt# percentie', t#ird
+uartie %ADt# percentie', and t#e upper end of t#e w#isker.
5 plot() is a genera grap#ic command wit# numerous options.
pot%x' 5 pots t#e data x
pot%var1, var)) 5 pots var= in dependence of var>
a"ine%ine%var=,var>'' 5 add a regression ine
pot%!,$, main?K,nter 1ite 7ereK' 5 scatterpot wit# varia"es ! and $
fit?lm(y:;) 5( fitted straig#t ine is s#own in t#e pot "! executing two more
commands
a"ine%fit'
# *xporting grap(s
5Rig#t cicking an!w#ere inside t#e active grap#ics window s#ows a context sensitive menu,
aowing saving t#e pot as metafie %,M-' or postscript format %*)'. 1#e options Copy as
metafile or Copy as bitmap %<M*' puts t#e grap#ics in t#e cip"oard. Gou can t#en paste it in
some appications, e.g., M) 2ord. More grap#ica formats are avaia"e from t#e main menu.
2#ie t#e grap#ic window is active, cick -ieS )ave (s from t#e menu and it ists six fie formats
%metafie, postscript, *D-, *.G, <M*, and T*G at t#ree +uait! eves' in tota so !ou #ave
pent! of c#oices.
52#ic# is t#e "est c#oice for t#e grap#ic format6 In genera metafie format retains grap#ic +uait!
even w#en it is resi$ed in t#e appication. 8n t#e ot#er #and, T*G is a ver! popuar c#oice
"ecause t#e fie si$e is usua! muc# smaer and internet compati"e. ,xcept for rare
circumstances, I woud not recommend "itmap fie format "ecause it is usua! ver! arge and
s#ows ver! poor picture +uait! w#en resi$ed. *ostscript fie format is usefu w#en incuding t#e
11/31
Tiessen (2009) Tutorial: Rapid intro to R.
grap#ic fie in anot#er postscript fie or w#en postscript printer is avaia"e. *icture +uait! does
not deteriorate w#en resi$ed.
5 /se metafie format for M) office appications %e.g. 2ord, *owerpoint'. 2it#in t#e grap#ic
window, rig#t cick t#e mouse "utton and seect cop! as metafie. 1#is wi transfer t#e figure
into t#e windows clipboard! <o to =owerpoint and paste as metafile! >or
publications save graphics in jpeg format!
# *xporting data
write.ta"e%x, fie ? Kdata8ut.txtK, append ? -(0),, co.names ? .(, sep ? K K'
5 wit# t#e a"ove commands a textfie wi "e created in in t#e working director!. 1o
c#ange t#e working director! use t#e foowing
getwd%' 5dispa!s t#e pat# for t#e working director!
dir%' 5 ists t#e fies in t#e current work director!
setwd%KC34(xe )tatisticsRK' # sets the working directory manually
5 open t#e text fies in exce using t#e text import wi$ard.
#1andling of R 2ac0ages
51#ere are dedicated we"pages for specific statistica topics. -or exampe, for t#e
ana!sis of microarra! data wit# R, consut3 #ttp344"ioconductor.org4
5Downoad R packages from diverse we"sites and save t#em in a foder as $ip
fies. 2it#in t#e R program use t#e option insta from oca $ip fies. Insta
packages from R direct! wit# t#ese commands3
insta.packages%KcorrgramK'
insta.packages%KagricoaeK'
insta.packages%KgpotsK'
insta.packages%K/singRK'
insta.packages%KatticeK'
insta.packages%JmaanovaK'
5 8nce !ou #ave downoaded t#e packages, activate t#em wit# t#e i"rar! function.
1#en !ou can use t#e extended functions in t#ose packages.
i"rar!%agricoae' #load and activate the library
i"rar!%corrgram)3
library(gpots)3library(/singR)3library(attice)3library(maanova)
5 *xample of group comparisons agricolae"
data=?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK' 5 import data
attac#%data=' 5to make t#e varia"es defined in data= accessi"e to t#e consoe
i"rar!%agricoae' #load and activate library
mode=?aov(?ield:<en49nv) #define a model for variance analysis
df?df!residual(model1)
M)error?deviance(model1)@df
comparison ? 7)D.test%Gied,Gen,df,2Aerror, group"#869)
"ar.err%comparison,std?1R/,) #for plotting a graph
52ake an additional BAC test with:
12/31
Tiessen (2009) Tutorial: Rapid intro to R.
comparison ? BAC!test(?ield,<en,df,2Aerror, p!adj".bonferroni.,
group">&BA9)
"ar.err%comparison,std?1R/,) #for plotting a graph
5>or adjusting the graph you can modify some options like:
"ar.err%comparison,std?1R/,,!im?c%B,CD',densit!?C,"order?K"ueK'
5&22'A biplots:
mode? &22'(9nv, 9ntry, 8ep, ?ield, graph".biplot.,number">&BA9)
5>or adjusting the graph you can modify some options like:
mode? &22'(9nv, 9ntry, 8ep, ?ield, xlim"c(-1,1),ylim"c(-1,1),
graph".biplot.,number">&BA9)
5 *xample 2airwise.T.test"
data=?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK' 5 import data
attac#%data=' 5to make t#e varia"es defined in data= accessi"e to t#e consoe
pairwise.t.test%9ar=, 9ar>) 5 make t.tests of 9ar= grouped "! categor! 9ar>
5 if !ou want to export t#e data !ou need to use foowing commands3
x?pairwise.t.test%9ar=, 9ar>) 5 create an o"&ect x for exporting
write.ta"e%xQp.vaue, fie ? Kdata8ut*airwise.txtK, append ? -(0),, co.names ?
.(, sep ? K K'
5 a textfie wi "e created in in t#e working director!.
getwd%' 5dispa!s t#e pat# for t#e working director!
dir%' 5 ists t#e fies in t#e current work director!
5 open the text file in excel using the text import wi;ard!
5 &nother option to make multiple comparisons!
mode=?aov%var=Rvar>'
1uke!7)D%mode=, .var1.)
#*xample 2CA anal%sis #ioconductor"
5first !ou need to define t#e source to downoad t#e package
source%K#ttp344"ioconductor.org4"ioc0ite.RK'
"ioc0ite%KpcaMet#odsK' 5downoading needs on! to "e done t#e first time
i"rar!%pcaMet#ods' 5oad t#e i"rar!. .ecessar! to do it ever! time !ou run R
5 -or importing data !ou #ave : options
55 =' oad t#e matrix wit# data. Considering t#at row names are present
data?read.ta"e%fie.c#oose%', #eader?1, sep?KMtK, row.names?='
55 or >' not considereing t#e ID of meta"oites or genes
data?read.ta"e%fie.c#oose%', #eader?1, sep?KMtK'
55 or :' importing data from cip"oard
data?read.ta"e%Kcip"oardK, #eader?1, sep?KMtK'
13/31
Tiessen (2009) Tutorial: Rapid intro to R.
5 depends on t#e t!pe of data, !ou can use3
55 =' varia"es in t#e rows and sampes in t#e coumns
md ? prep%t%data', scae ? KnoneK, center ? 1R/,'
55 or >' varia"es in t#e coumns and sampes in t#e rows
md ? prep%%data', scae ? KnoneK, center ? 1R/,'
55 t#en do t#e ana!sis. In t#is case for t#e first D principa components.
res**C( ? pca%md, met#od ? KppcaK, center ? -(0),, n*cs ? D'
spot%res**C(' 5 to pot t#e resuts
res**C(Uscores 5 to see a t#e principa components %treatment'
res**C(Uoadings 5 to see a t#e oadings
#*xample 2CA anal%sis native R"
data?read.ta"e%Kcip"oardK, #eader?1, sep?KMtK' 5importing data from cip"oard
5 depends on t#e dataframe !ou #ave3
55 =' varia"es in t#e rows and sampes in t#e coumns
md ? prep%t%data', scae ? KnoneK, center ? 1R/,'
55 or >' varia"es in t#e coumns and sampes in t#e rows
md ? prep%%data', scae ? KnoneK, center ? 1R/,'
md?data.frame%mdO,>P,mdO,:P,mdO,CP,mdO,DP,mdO,EP,mdO,AP'
55 (na!sis for > principa components.
pc?princomp%md, scae?1R/,'
oadings%pc'
"ipot%pc'
pc
summar!%pc'
#*xample A33I/ anal%sis agricolae"
dataD?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK' 5 import data
5 data must "e in t#e foowing format3
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
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)
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)
14/31
Tiessen (2009) Tutorial: Rapid intro to R.
#*xample: Conditional Averaging
data=Qvar:?ave%data=Qvar=,data=Qvar>' 5 create new coumn named var=B wit#
averages of var= grouped "! var>
attac#%data='F data=Qvar:?ave%var=,var>' 5 same as a"ove "ut using attac#
function
dataDQav,ntr!?ave%dataDQGied,dataDQ,ntr!' 5 create new coumn wit# average
of Gied grouped "! entries
#Tests for significance
t.test%var=Rvar>' 51.test wit#in data of varia"e= grouped "! categories specified
in varia"e >. Grouping factor of var> must #ave exact! > eves!
# %ote the important difference between the t!test(var1,var)) and
t!test(var1:var)) since in the first case you are comparing all values
of var1 against all values of var)! 'n the second case, you make
subgroups of the values contained in var1 depending in the grouping
levels defined in var)! 'f you have more than ) grouping levels in var),
then it is better to use the pairwise!t!test function!
pairwise.t.test%var=, var>) 5 make t.tests of var= grouped "! categories var>. 1#e
resut is a pairwise comparison matrix. .ote t#e comma in (var1, var))
ave%var=, var>) 5 make averages of var= grouped "! factor categories var>. 1#e
resut is a vector of t#e same engt# as var1!
5#o make an analysis of variance:
a"aov(var1:var)Dvar0Dvar1) #defines a new object a with the results of
the analysis of variance
a #lists the content of the previously created
object a
summar!%a' #lists the summary of the statistical results of object
a
attri"utes(a) #lists all attributes of new object
5#o make a linear model:
mode=?m%var= Rvar>Nvar:NvarC'
summar!%mode=' 5 s#ow summar! of t#e inear mode
attri"utes%mode=' 5 s#ows a attri"utes of t#e new o"&ect
#3a0ing special grap(ics
# 3ultiple plots per page
5Gou can com"ine mutipe pots into one overa grap#, using eit#er t#e
15/31
Tiessen (2009) Tutorial: Rapid intro to R.
par% ' or a!out% ' function. 2it# t#e par% ' function, t#e option mfrow?c%nrows,
ncos' creates a matrix of nrows x ncos pots t#at are fied in "! row.
mfco?c%nrows, ncos' fis in t#e matrix "! coumns.
par%mfrow?c%>,>''
pot%var=,var>, main?K1ite pot=K'
pot%var=,var:, main?K1ite pot>K'
#ist%var=, main?K1ite 7istogramK'
"oxpot%var=, main?K1ite <oxpotK, xa"?Ka"e=K, !a"?Ka"e>K'
5 1#e a!out% ' function #as t#e form a!out%mat' w#ere mat is a matrix o"&ect
specif!ing t#e ocation of t#e . figures to pot.
5 ,xampe of one figure in row = and two figures in row >
a!out%matrix%c%=,=,>,:', >, >, "!row ? 1R/,''
#ist%var='
#ist%var>'
#ist%var:'
5 1istograms
5 7ow many bins should an histrogram containE #his depends on the number
of observations n! 6se the approximation of Aturges k" log)(n)D1
51#is means t#at for =BB o"servations !ou s#oud use around A-L "ins
5 )turges, 7. (. %=@>E'. K1#e c#oice of a cass intervaK. T. (merican )tatistica
(ssociation3 EDVEE.
5 *xample gplots"
data=?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK' 5 import data
attac#%data=' 5to make t#e varia"es defined in data= accessi"e to t#e consoe
i"rar!%gpots' #load extended plot library
potmeans%var=Rvar)) #plot means with error bars
C
B
D
B
E
B
C(RR,R(>
C
8
.
8
C
<I80 I<W I.G I.G(GR 81R8 W-<
n?=@ n?>= n?A n?=> n?=> n?L
5 *xample lattice"
x?read.ta"e%fie?Kcip"oardK, #eader?1, sep?KMtK' 5 import data
attac#%x' 5make varia"es accessi"e
i"rar!%attice' #load library
1/31
Tiessen (2009) Tutorial: Rapid intro to R.
x!pot%var=R var>)
C(RR,R(>
C
8
.
8
C
>B
:B
CB
DB
EB
AB
LB
<I80 I<W I.G I.G(GR 81R8 W-<
dotpot%var=R var>'
barchart(var1: var))
strippot%var=R var>)
"wpot%var=R var>)
C
8
.
8
C
>B
:B
CB
DB
EB
AB
LB
<I80 I<W I.G I.G(GR 81R8 W-<
5 *xample /imple"
insta.packages%K/singRK'
re+uire%/singR'
data=?read.ta"e%Kcip"oardK, #eader?1, sep?KMtK'
attac#%data='
simpe.vioinpot%var= R var>, co?L'
<I80 I<W I.G I.G(GR 81R8 W-<
>
B
C
B
E
B
L
B
par%new?1R/,' 5for adding to an existing grap#. .ext function wi add.
pot%var= R var>, add ? 1R/,' 5 so t#at a new pot is not started
1!/31
Tiessen (2009) Tutorial: Rapid intro to R.
5 *xample ellipse"
data=?read.ta"e%Kcip"oardK, #eader?1, sep?KMtK'
i"rar!%eipse'
potcorr%cor%data=, use ? Kpairwise.compete.o"sK''
<I8W
<I80
I.G0,)
*R8<(<
W/IM
<
I
8
W
<
I
8
0
I
.
G
0
,
)
*
R
8
<
(
<
W
/
I
M
i"rar!%corrgram'
corrgram%data='
#Trellis grap(s
#In order to produce Trellis plots you must load the Lattice" library and
start a trellis aware" device.
library(lattice)
trellis.device()
5Make conditiona pots wit# t#e copot function
copot%var= R var> S var:' 5 pot var= against var>, su"divided in categories var:
1"/31
Tiessen (2009) Tutorial: Rapid intro to R.
.8 )I
:
B
C
B
D
B
E
B
A
B
L
B
.8 )I
*R8*,D
C
8
.
8
C
-
M
Given 3 ),X8
5 It is possi"e to use two conditiona varia"es in t#e form of var:YvarC
copot%var= R var> S var:YvarC' 5 pot var= against var>, su"divided in categories
var: and varC
19/31
Tiessen (2009) Tutorial: Rapid intro to R.
:
B
D
B
A
B
.8 )I
:
B
D
B
A
B
:
B
D
B
A
B
:
B
D
B
A
B
:
B
D
B
A
B
.8 )I
:
B
D
B
A
B
*R8*,D
C
8
.
8
C
-
M
Given 3 ),X8
<
I
8
0
I
<
W
I
.
G
I
.
G
(
G
R
8
1
R
8
W
-
<
G
i
v
e
n

3

C
(
R
R
,
R
(
>
58ptions for t#e copot function3
copot%formua, data, given.vaues, pane ? points, rows, coumns, s#ow.given ?
1R/,, co ? par%KfgK', pc# ? par%Kpc#K', xa" ? paste%KGiven 3K, a.name', !a" ?
paste%KGiven 3K, ".name', num"er ? E, overap ? B.D, ...' co.intervas%x, num"er ? E,
overap ? B.D'
5 formua
5 Add a trendline
5/se t#e functions m%' and a"ine%' to add a trendine to an existing pot.
data=?read.ta"e%Kcip"oardK, #eader?1, sep?KMtK'
attac#%data='
pot%var=, var>'
mode=?m%var=Rvar>'
a"ine%mode=,wd?>' 51#e wd specifies t#e ine widt#
mode= 51o s#ow t#e sope and intercept of t#e trendine
# To add text to a grap(ic
text %x, ...' text.defaut %x, ! ? ./00, a"es ? se+%aong ? x', ad& ? ./00, pos ?
./00, offset ? B.D, vfont ? ./00, cex ? =, co ? ./00, font ? ./00, xpd ? ./00'
mtext%text, side ? :, ine ? B, outer ? -(0),, at ? ./00, ad& ? .(, cex ? .(, co ?
.(, font ? .(, vfont ? ./00'
tite%main?K.ameK'
20/31
Tiessen (2009) Tutorial: Rapid intro to R.
5 4D 2erspective 2lot
B.LC= B.ACE B.EAL B.E>L B.DLA B.DDC B.D>E B.DB>
B.@B@ B.AL@ B.A=B B.ED> B.EBE B.DAB B.DCB B.D=C
B.=C= B.=C= B.=CB B.=CB B.=:@ B.=:@ B.=:L B.=:L
-B.ADA -B.ELA -B.E:C -B.D@> -B.DDL -B.D:B -B.DBD -B.CLC
-B.@D@ -B.L=@ -B.A:B -B.EEA -B.E=@ -B.DLB -B.DCL -B.D>=
-B.>A@ -B.>AE -B.>A> -B.>E@ -B.>EE -B.>E: -B.>EB -B.>DA
B.EDA B.E== B.DA: B.DC: B.D=E B.C@C B.CAC B.CDE
B.@L@ B.L:E B.AC> B.EAE B.E>D B.DLD B.DD: B.D>D
B.C=> B.CB= B.:@B B.:LB B.:A= B.:E: B.:DD B.:CA
-B.DCC -B.D=L -B.C@D -B.CAD -B.CDA -B.CC= -B.C>A -B.C=C
data:?read.ta"e%fie?Kcip"oardK, #eader?-, sep?KMtK' 5 import data wit#out
#eader. It is important t#at it is wit#out #eader in order to convert it to a matrix.
data:?data.matrix%data:' 5 convert ta"e to a data matrix. Important step "ecause
ot#erwise data wi "e treated as a dataframe and not as a datamatrix.
x?c%=3=B' 5 define x varia"e. In t#is case =B vaues
!?c%=3L' 5 define ! varia"e. In t#is case L vaues
par%mfrow?c%>,>'' 5 for potting C grap#s in one page
contour%x, !, data:, co?K"ueK' 5 pot contour
image%x, !, data:' 5 pot image
persp%x, !, data:, co?K"ueK, t#eta ? ->B, p#i ? =D, scae ? 1R/,, axes ? 1R/,'
x
!
d
a
t
a
:
21/31
Tiessen (2009) Tutorial: Rapid intro to R.
5 /pecial /catterplots
55 In order to do specia scatterpots, !ou need first to define extra functions, and
t#en ca t#em from wit#in t#e function pairs.
55 ,xtra function to put #istograms on t#e diagona
pane.#ist ? function%x, ...'
Z
usr ? par%KusrK'F on.exit%par%usr''
par%usr ? c%usrO=3>P, B, =.D' '
# ? #ist%x, pot ? -(0),'
"reaks ? #Q"reaks
n< ? engt#%"reaks'
! ? #QcountsF ! ? !4max%!'
rect%"reaksO-n<P, B, "reaksO-=P, !, co?Kc!anK, ...'
[
55 ,xtra function to put correations R on t#e upper panes
pane.cor ? function%x, !, digits?>, prefix?KR?K, cex.cor'
Z
usr ? par%KusrK'F on.exit%par%usr''
par%usr ? c%B, =, B, =''
r ? a"s%cor%x, !, use?Kpairwise.compete.o"sK''
txt ? format%c%r, B.=>:CDEAL@', digits?digits'O=P
txt ? paste%prefix, txt, sep?KK'
if%missing%cex.cor'' cex.cor ? B.L4strwidt#%txt'
text%B.D, B.D, txt, cex ? cex.cor'
[
55 ,xtra function to put R> vaues instead of simpe R vaues. It aso puts p vaues.
pane.cor> ? function%x, !, digits?>, prefix?KK, cex.cor'
Z
usr ? par%KusrK'F on.exit%par%usr''
par%usr ? c%B, =, B, =''
r ? a"s%cor%x, !, use?Kpairwise.compete.o"sK''
r> ? a"s%cor%x, !, use?Kpairwise.compete.o"sK'Ycor%x,!,
use?Kpairwise.compete.o"sK''
txt ? format%c%r>, B.=>:CDEAL@', digits?digits'O=P
txt ? paste%prefix, txt, sep?KK'
if%missing%cex.cor'' cex.cor ? B.L4strwidt#%txt'
text%B.D, B.D, txt, cex ? cex.cor'
modeo?summar!%m%xR!''
vaor*?signif%modeoQcoefficientsOLP, digits?digits'
text%B.A, B.>, vaor*'
[
55 1o put #istograms in diagona and smoot# ines in ower pane3
22/31
Tiessen (2009) Tutorial: Rapid intro to R.
pairs%/)TudgeRatingsO=3DP, pane?pane.smoot#, cex ? =.D, pc# ? =, "g?Kig#t
"ueK, diag.pane?pane.#ist, cex.a"es ? >, font.a"es?>'
55 1o put correations on t#e upper panes and smoot# ines in ower pane3
pairs%/)TudgeRatingsO=3EP, ower.pane?pane.smoot#, upper.pane?pane.cor'
55 1o put correations R> on t#e upper panes and smoot# ines in ower pane3
pairs%/)TudgeRatingsO=3EP, ower.pane?pane.smoot#, upper.pane?pane.cor>'
55 1o put correations R on t#e upper panes, #istograms in diagona, and smoot#
ines in ower pane3
pairs%/)TudgeRatingsO=3DP, ower.pane?pane.smoot#, upper.pane?pane.cor,
diag.pane?pane.#ist'
55 1o put correations R> on t#e upper panes, #istograms in diagona, and smoot#
ines in ower pane3
pairs%/)TudgeRatingsO=3DP, ower.pane?pane.smoot#, upper.pane?pane.cor>,
diag.pane?pane.#ist'
5 *xercises
5 ,xampe of Grap#3
p H- c%B3=BBB'4=BBB
x H- EY%p\>'Y%%=-p'\>'
pot%p,x,co?KredK,t!pe?KK'
tite%main?K0ike!#ood functionK, !a"?K0ike!#ood3 0%p'K'
grid%'
B.B B.> B.C B.E B.L =.B
B
.
B
B
.
=
B
.
>
B
.
:
p
0
i
k
e

!
#
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

Anda mungkin juga menyukai