1、Rn5sR frequently asked questionshttp:/www.r-project.org153sRThis document is generated from LATEXsources compiled with ctex v0.7din a Windows platform . The used packages are CJKalistingsagraphicx and so on. = “; 1R2Z R introR dataR admin9 1 contributed documents 1Capital Of Statistics ) 5b “ Bd9R q
2、4B y MR q T FN V ? VriPb d9ZE yN 5 V ?Wd9 MbR B v8“CRANTask Views V b# A= wa sa a bWd9ard9ZE b7tZEYVMR Packages Z V R Bq h Y fbT F 5 * B1R !9 Bp4 7h hebz37: F MaR pb +!1June 25, 2008abel,cran,rtist,Xie Yihui,zhangv xA -1. R S I $R B“d9s ?# $R PNA not availableV U 1iR SNA“ B 6x 3class (x)# M H ? P1 x
3、 = NA b7 Pf is.na() P1 x ! is .na(x)“ b48. |31M 7$ eval()parse()f b n5 Pparse()f |31Vr T expression7Peval()f Vr T pb1 x a2 8 ,2any(a1!=a2) # a l l ( a1 = a2 )any()f VR B b7 M f 5a1!=a2|R BTRUE * any()f | TRUEb“9 V Pidentical()f b1 indentical (a1 ,a2)T1R M V P1 which(a1!=a2 , arr . ind = TRUE)arr.ind
4、 array indicesiR b52. M t$ Iuniquef buniquef V _ a b111 x 130) ,mean)60. 3$sapply(dataframe, is.numeric)61.BF | $f sample()sample(n) F1;:;nsample(x) F_ x;length(x) 1sample(x, replace = T)Esample(x,n)dbVx |nsample(x,n, replace = T)bVx |nsample(x,n, replace = T ,prob = p) qpbVx |n7 “1d9VC TB“13n fiven
5、um(c(1 ,4 ,6 ,17 ,50 ,51 ,70 ,100)1 1.0 5.0 33.5 60.5 100.03 quantile (c(1 ,4 ,6 ,17 ,50 ,51 ,70 ,100)0% 25% 50% 75% 100%5 1.00 5.50 33.50 55.75 100.00 A/ ls s l summary()f Ps Q V A14; 34s l1+ 14(length(x)1);14s 1+ 34(length(x)1);34s 7flvenum()f QbxE 65. S wLs$R Pintegratef sT 141 integrate (dnorm,
6、1.96, 1.96)integrate (dnorm, Inf , Inf )3 # a slowly c o n v e r g e n t i n t e g r a lintegrand ;=;m !b96.USy 3 b$ m 51 ?par # s e e l a splot (0 ,0 ,xaxt=“n“, type=“n“, ylim=c(0 ,100) , las=1 )3 mtext(“35“, side=2,at=35, line =1, las=1)97.m ! t$Rm ! /+ I?DevicesRm ! x11 X3 gA Uwindows Windows3 gp
7、ostscript ps Tqpdf pdf TqpictexLATEX Pqqpng png Tq!jpeg jpeg Tqbmp bmp TqxflgXFIG Pm Twin.metaflle a emf TqaWindows/r wi Ppostscript()f ypsm T O m T OY b98. S rm$R Pstarsf S rmb1 stars ( state .x77 , c(7 , 4, 6, 2, 5, 3) , full = FALSE,key . loc = c(10 , 2) full = FALSE V U rm s Q rm key.loc V Um b9
8、9. I R ?A U8 $m col P 3 9 H f yR = 8 22palette ()2 barplot (rnorm(15 , 10 , 3) , col = 1:15)palette (rainbow(15)4 barplot (rnorm(15 , 10 , 3) , col = 1:15)palette (“default“)R 657 V P VYV1 colors () Y L t V R ?A U502 b V Pf rgb() i b100. V $)mf Bcol V !bbarplot() m V P F“(colorsets) ! F /+ R Ff rain
9、bow() (xbxbxbxbxbxbxb)heat.colors() (xbxbxbxbxbxbxb)terrain.colors() s a5 (xbxbxbxbxbxbxb)topo.colors() 05 (xbxbxbxbxbxbxb)cm.colors() 0 0, (xbxbxbxbxbxbxb)gay()agrey() (xbxbxbxbxbxbxb)1 x 1:10 ; names(x) letters 1:10barplot (x, col = rev(heat . colors (10)3 barplot (x, col = gray (1:10)/10);101.8“|
10、R RGB $ If col2rgb()1 write . table (t( col2rgb (rainbow (7)/255) ,sep = “,“)102. mvl$Windows / f u 7m3 g3 gvl i PsavePlot()f i 9 V Y51 windows(width = , height = )u 7Blzvl3 g m V Ppdf() ,postscript() , png() ,jpeg() ,pictex() 3“f 231 # s t a r t a PDF f i l epdf(“picture.pdf“, height=4,width=6)3 #
11、your drawing commands h e r edev. off () # c l o s e the PDF f i l etf !mvl V Pdev. copy(device , file=“, height , width) 7b103. E $ VS E E1 # two d i m e n s i o n sn 1003 x cumsum(rnorm(n)y cumsum(rnorm(n)5 plot (x, y, type = l)104. wL$TXStf V Pcurve(expr, from , to, add = T)f bQ P Z wLf spline(x
12、, y , n= ) 1 x 1:5y c(1 ,3 ,4 , 2.5 ,2)3 plot (x, y)sp spline (x, y, n = 50)5 lines (sp)105.(lattice)m Ym I uY$ latticem L= S-plusTrellismR LC V jZEbmM Ym B% T“mZ T M 4b T s Y1|t Y/ 1 m p41 library ( lattice )histogram(height j voice . part , data = singer )latticemf 24latticemf f xyplot(yx)M mdotpl
13、ot(yx) Clevelandm( Fm)barchart(yx) yxHmstripplot(yx)BmxA y V y0bwplot(yx)QLmhistogram(x)Zm106. m$ Ipersp()contour()f b 1i m US Tb I15:outer()f b107.XB B Z I f $ Iimage()fllled.contour()f x y seq(10, 10, length=50)2 f function (x,y)fr sqrt (x2 + y2)4 10sin (r)/rg6 z outer(x , y , f )image(x , y , z )
14、8 filled . contour(x ,y ,z )108. m vlyM 1 $RS meyRm V PM x 1:102 y runif (10)symbols(x,y, circles = y/2 , inches = F,bg = x)109.XB B SQQm$Papply()f T O ?E RA T1 table data . frame(x1 = rnorm(100) ,x2 = rnorm(100 ,1 ,1)par(ask=TRUE) # wait f o r c h a n g i n g3 results = apply(table , 2, qqnorm)par(
15、ask=FALSE)110. BZm FBlQLm$25Zm bF 6lm m B“ P par()x rnorm(100)2 hist (x)op par( fig=c (.02 ,.5 ,.5 ,.98) , new=TRUE)4 boxplot(x)111. RmF T 3$ I?plotmath LATEX?C=Ed bx 1:10 ; plot (x, type = “n“)2 text (3 ,2 , expression (paste(“Temperature (“, degree , “C) in 2003“)text (4 ,4 , expression (bar(x) =
16、sum( frac (x i , n) , i=1, n)4 text (6 ,6 , expression (hat(beta) = (Xt X)f.1g Xt y)text (8 ,8 , expression (z i = sqrt (x i 2 + y i 2)112. Hm A U bar $T barplot()f L )m 7rect()f / */ 09 4 1 x 1:10 ; names(x) letters 1:10b barplot (x, col = rev(heat . colors (10)3 text (b , x , labels = x ,pos = 3)1
17、13. wL$ f TmbUS“/ V P Z(xa)2 +(yb)2 = 1 =) x = asin ;y = bcos ;0 21 t seq (0 ,2pi , length = 100)x sin (t) # a=13 y 2cos(t) # b=2plot (x,y, type = l)114.word PR 3 m$O mrT Kz1 epsapdf7 m pngajpgatifibword V Peps A U zurT pbxId9 115. 9 f $26 1-B9n HWbFBasics4 skewness ()2 kurtosis ()V9 b116. S V$table
18、()f btable(x)x V table(x,y) Vbx with( airquality , table (cut(Temp, quantile (Temp) , Month)2 prop . table (x,1)117. SLB $L Kd9ZE O W Cd9ZE N$ ?Z bKeLB yi = fi+flxi +ifi fl | qblm()f 4 LB9 ZEblm. swiss lm( Fertility . , data = swiss )lm()T BcB V c/coecientsB“ residualsR fltted.values E.V P / 7 V :1
19、names(lm. swiss )summary()anova()sYRB 1ZsVb1 summary(lm. swiss ) # the same as summary . lm ( )anova(lm. swiss )4 | f V IR-introStatistical models in RBbT) v V Pbiglmbiglm()f bf VZ “ B Eb118. $ Iupdate()f 27summary( f0 lm( Fertility . , data = swiss )2 f1 update(f0 ,. . Examination)summary( f1 )119.
20、 PB$R V P9 Bstep()f b 9 AICd9 5 |KlAICd9 rB “b1 utils : example(lm)step(lm.D9)stepf V Pboth,forward,backward“ ZE backward“b F V Iadd1drop1f b120. R LCs B(Quantile Regression) IquantregquantregForestdata( engel )2 taus c(.15 , .25 , .50 , .75 , .95 , .99)rqs as . list (taus)4 for ( i in seq(along = t
21、aus ) frqs i rq(log10(foodexp) log10(income) ,6 tau = taus i , data = engel )lines (log10( engel$income) , fitted (rqs i ) , col = i+1) g8 legend(“bottomright“, paste(“tau = “, taus ) , inset = .04 ,col = 2:( length (taus)+1), lty=1)121. B 98 ( uW9$et.test()f 1 x rnorm(100)t . test (x)122. S s$K ( (
22、kmeans() )x rbind(matrix(rnorm(100 , sd = 0.3) , ncol = 2) ,2 matrix(rnorm(100 , mean = 1, sd = 0.3) , ncol = 2)cl kmeans(x, 2, 20)4 plot (x, col = cl$cluster , pch=3, lwd=1)points ( cl$centers , col = 1:2 , pch = 7, lwd=3)6 segments( x cl$cluster =1 , ,1 , x cl$cluster =1 , ,2 ,28cl$centers 1 ,1 ,
23、cl$centers 1 ,2)8 segments( x cl$cluster =2 , ,1 , x cl$cluster =2 , ,2 ,cl$centers 2 ,1 , cl$centers 2 ,2 ,10 col=2)Q (hclust() )n seq (1 ,50 ,by = 4)2 (x USArrests n ,) # p r i n t ( )hc1 hclust ( dist (x) , method = “complete“)4 hc2 hclust ( dist ( scale (x) ,method = “complete“)hc3 hclust ( dist
24、 (x) , method = “ave“)6 layout(matrix(c(1 ,1 ,2 ,3) ,nrow = 2,byrow = T)plot (hc1 ); plot (hc2 ); plot (hc3)V V ?1s * Pcutree()f 9 p41 cutree (hc , k = 1:3) S cluster1 library ( cluster )clusplot ( x, pam(x, 2)$clustering )123. Sss$statsprincompf b(pc. cr princomp(USArrests , cor = TRUE)2 plot (pc.
25、cr , type = “lines“ # or “ b a r p l o t “) # or s c r e e p l o t4 loadings (pc. cr)princomp() cor = TRUEV U P“M1 TssQ P“xZ bloadings()Ry0bscreeplot() Fmb124.8“Sy0s$R Pfactanal()f v y0sbexample( factanal )125. “ _$1 nZEshapiro.test()ks.test()(Kolmogorov-Smirnov_)jarque.bera.test() (1tseries)b I T _normtestfBasicsM1f b+ $v4 E+_f b29