1、R for BeginnersChinese Edition 2.0Emmanuel ParadisInstitut des Sciences de lEvolutionUniversite Montpellier IIF-34095 Montpellier cedex 05FranceE-mail: paradisisem.univ-montp2.frCo-translated by: XF Wang, YH Xie, JT Li and GH Ding R for beginners“ B md aRb2002 M7N T2005 M=bEmmanuel Paradisp V4 LATEX
2、qbJrT Chap12: Chap3:m Chap4: + Chap57:Sb =Sv =3I nbv = , i4 v LinbNBiV UIrp 4 s RRb in hYV“ br2006 M4 c 2002, 2005, Emmanuel ParadisPermission is granted to make and distribute copies, either in part or infull and in any language, of this document on any support provided the abovecopyright notice is
3、 included in all copies. Permission is granted to translatethis document, either in part or in full, in any language provided the abovecopyright notice is included.“ “ “ c c c112 Q Q Q32.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 3 # “. . . . . . . . . . . . . . . . . . . . .
4、 52.3L. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 R TTT93.1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2q . . . . . . . . . . . . . . . . . . . . . . . . . 113.3i% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.4 3 . . . . . . . . . . . .
5、. . . . . . . . . . . . . . . . . . 153.4.1?5 . . . . . . . . . . . . . . . . . . . . . . . . . . 153.4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . 183.5 P. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.5.17y. . . . . . . . . . . . . . . . . . . . . . . . . . 193.5.2 . . .
6、. . . . . . . . . . . . . . . . . . . 243.5.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.5.4B /S“d. . . . . . . . . . . . . 273.5.5 . . . . . . . . . . . . . . . . . . . . . . 303.5.6 I . . . . . . . . . . . . . . . . . . . . . . . . . 323.5.7 Btef . . . . . . . . . . . . . . . . 32
7、3.5.8 9 . . . . . . . . . . . . . . . . . . . . . . . . . . 344 Rmmm374.15 m. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374.1.1u 7m !. . . . . . . . . . . . . . . . . . . . . 374.1.2ms. . . . . . . . . . . . . . . . . . . . . . . . . 384.2mf . . . . . . . . . . . . . . . . . . . . .
8、. . . . . . . . . 414.3)m 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . 424.4m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444.5B L . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454.6 gridlattice. . . . . . . . . . . . . . . . . . . . . . . . . . 495 Rddd999s
9、ss565.11ZsBe 0. . . . . . . . . . . . . . . . . . . 565.2 T. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585.3Wf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595.4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626 RIII L L Llll656.1_ . . . . .
10、. . . . . . . . . . . . . . . . . . . . . . . 656.2R. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676.3I F1-f . . . . . . . . . . . . . . . . . . . . . . . . . 687 RMMM111DDD721 m 1RB .1|b RT d bR #%bR B“d9s ?# V U 7b Windows“du 7Rgui.exe ?/ Bt T 7( Lu 7q.)b t V ?$“X 7b Lt = - gBtRT dA
11、1b c 11 = bc n5e1 RT b=| Bts (assign“) T 8“ 3(object) T5 t.Ke1 RdLb2.1 yR BIBtI V ?7 bp L A1 n5R B d7 Ir9i“ 7 ?$71Bt1 n5B T( CFortan, Pascal, .)b=REde4b LB 7lm(y x)VUx1M yQ EBL “bERf 9 | T P | = = ( ,ls()b T f 7 |R51A Uf Bt 8 = b m“s3XT b# f | uY(object)bR H M f #T(objects) Ti9 =ii3M 3|b VYVBt ( 1 )
12、Btf ( for an operator givesthe typical use.Arguments: for a function, details each of its arguments.Details: detailed description.Value: if applicable, the type of object returned by the function or the oper-ator.See Also: other help pages close or similar to the present one.Examples: some examples
13、which can generally be executed without openingthe help with the function example.7 IExampless b7B-%ArgumentsBt 9 dA1bc Bt s Note, ReferencesAuthor(s)b /f help$ =i btry.all.package 8 FALSE, T !TRUE5 V 7 help(“bs“)No documentation for bs in specified packages and libraries:you could try help.search(“
14、bs“) help(“bs“, try.all.packages = TRUE)Help for topic bs is not in any loaded packagebut can be found in the following packages:Package Librarysplines /usr/lib/R/libraryi f /A U1f bs: T P LXu 7“: 7 $ =i H V Ppackage: help(“bs“, package = “splines“)bs package:splines R DocumentationB-Spline Basis fo
15、r Polynomial SplinesDescription:Generate the B-spline basis matrix for a polynomial spline.Html T VYV / f help.start()html T: V P1oM bSee Alsos VYV M1f : b P1oM R9 VYVf help.search LCbZE ? X c31M1 = b help.search(“tree“) : ctree“f bi TBt K n5 Pf help.searchrebuild o(e.g.,help.search(“tree“, rebuild
16、= TRUE)bPf apropos5 ?s 3c31f $ =i apropos(help)1 “help“ “.helpForCall“ “help.search“4 “help.start“83 R TTT3.1 X ARYVBt n5t = Y Q9YV Yb t) Bf1,2,3g |M M V B M 9 V Bs M I tJ T YaII8bA M d9s | M R4“ b/ 9B TBf VC| | % b = b 3 7 (FALSETRUE) 9i i ? V U f Vr T “b VsYYVf modelengthb x mode(x)1 “numeric“ len
17、gth(x)1 1 A mode(A); mode(compar); mode(z)1 “character“1 “logical“1 “complex“ I 9 NA( V) V U v 5 V TV U N N1 2.1e+23R V V U k Inf-InfV U1NaN(d3)V U 3b7 m) 9 x x1 Inf exp(x)1 Inf exp(-x)1 0 x - x1 NaN3 HF |“ T1| V Q|n 3B n“ tf cat A Uwrite.table H (np. 14,f qmethod) H$+ yZ T) b x x1 “Double quotes “
18、delimitate Rs strings.“ cat(x)Double quotes “ delimitate Rs strings.9 6BV U3M ZE|() M f/1Q| | | HA1 x x1 “Double quotes “ delimitate Rs strings.“/V V U Y BBB $_ 3 y0 3F 3 3 3 HW (ts) 3 V 3 f Vr T.10_ BM i 9 Y *“ y0 Bs M F Bk V FB+ k = 2bi F A B B+_ y0 A V otspV U HW cBt- q HW V Vc VB_ 7 5 6Bt-t bt V
19、 Udim1 B22 dim B 2,2 4b3.2qqq q | TR PT “ c b V P 7getwd() (T “ c) s “ c P 7setwd(“C:/data“)setwd(“/home/paradis/R“) M “ cb TBqT “ c 5A 8bR V/ f |i%q ASCII read.table( n)scanread.fwfbR9 V | Tq(Excel, SAS, SPSS, .)SQL o $ic tf bt ?f R) Es | |qKASCII Tbf read.table 7yB |V T 1ZEb B data.datq 7 mydata (
20、NA)colClasses B3_ nrows V |Kv ( )skip | -KV check.names TTRUE5_M Rrfill TTRUE Od M “M5 bAstrip.whitesepX f / TTRUE5 “3M -bblank.lines.skip TTRUE bcomment.charB3 q d3 7h|$ 1 V Pcomment.char = “read.table+My P 8 V+ f /read.csv(file, header = TRUE, sep = “,“, quote=“, dec=“.“,fill = TRUE, .)read.csv2(f
21、ile, header = TRUE, sep = “;“, quote=“, dec=“,“,fill = TRUE, .)read.delim(file, header = TRUE, sep = “t“, quote=“, dec=“.“,fill = TRUE, .)read.delim2(file, header = TRUE, sep = “t“, quote=“, dec=“,“,fill = TRUE, .)f scan1read.table1F 2 uYB - VM 12 mydata 31 NAflushB TTRUE | scan|/B “ qF dF fill TTRU
22、E Od M “M5 bAstrip.whitesepX f / TTRUE5 “3M -bquietB TFALSEscanA UB t3 $ |blank.lines.skip TTRUE bmulti.linewhat B V H FALSE5V U V 8 M Bcomment.char d 7 S3B3 7hs|$ Nf read.fwf V |qBt% z T read.fwf(file, widths, sep=“t“, as.is = FALSE,skip = 0, row.names, col.names, n = -1, .)13“ widths |3 zread.tabl
23、e()Mb TB data.txtqBF U V |t / 7A1.501.2A1.551.3B1.601.4B1.651.5C1.701.6C1.751.7 mydata mydataV1 V2 V31 A 1.50 1.22 A 1.55 1.33 B 1.60 1.44 B 1.65 1.55 C 1.70 1.66 C 1.75 1.73.3iii% f write.table Vq BB B 9V _ .b write.table(x, file = “, append = FALSE, quote = TRUE, sep = “ “,eol = “n“, na = “NA“, de
24、c = “.“, row.names = TRUE,col.names = TRUE, qmethod = c(“escape“, “double“)14x1 fileq 8 H$op append TTRUE5 H “ “Sq V ?Xi |FZ TquoteB _ TTRUE53M y0|“ quote _ 5V|“ *t Sb( f /M $“ quote = FALSE5M c|)sepq3 seol P K3(“nn“V U)naV U 3dec V Ul 3row.namesB % q 1T q3_ col.namesB % q B1T q3_ qmethod quote=TRUE
25、5N 3M |“ ) “escape“ (“e“ 8) “n“9 “d“5 “9XeZE|B q V P 7write(x,file =“data.txt“), x 3 V _ Fb nc(ncol) lq 8 f / Tx 3 5nc=1 nc=5append B TRUE5F H “ *t V ?Xiq FALSE 85 “qXi b1: cBF i V P 7save(x, y, z,file= “xyz.RData“)b V PASCII“TRUE P WeMb R ST bW V Pload(“xyz.RData“)$F =ibf save.image() save(list =ls
26、(all=TRUE),file=“.RData“)BeZ Tb3.4 3 3 3 3.4.1?555 BV130?5 V“ 3 x 1:10-11 0 1 2 3 4 5 6 7 8 9 1:(10-1)151 1 2 3 4 5 6 7 8 9f seq V 3 / L seq(1, 5, 0.5)1 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0B 3V U =V U 3 b9 V“ P seq(length=9, from=1, to=5)1 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 Vf c c(1, 1.5, 2, 2.5, 3,
27、 3.5, 4, 4.5, 5)1 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0TXo Bt 9 V1 P scanf z z1 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0f rep 7yB M_ rep(1, 30)1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1f sequence7yB“ sequence(4:5)1 1 2 3 4 1 2 3 4 5 sequence(c(10,5)1 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5f gl( 3
28、 /Q ) Esy ? 3?5y0 bf E gl(k,n) k Y n Q bNf length 3 labels y0 3b 16 gl(3, 5)1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3Levels: 1 2 3 gl(3, 5, length=30)1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3Levels: 1 2 3 gl(2, 6, label=c(“Male“, “Female“)1 Male Male Male Male Male Male7 Female Female Female Female Female Fem