Professional Documents
Culture Documents
http://www.r-project.org
153
abel,cran,rtist,Xie Yihui,zhangv . . .
1 sunbjt<at>gmail.com
1. R
R Ross Ihaka
Robert Gentleman R Becker, Chambers & Wilks S Sussman Scheme
R S
2. R
R http://cran.r-project.org/mirrors.html UC
Berkeley R LinuxMacOS XWindows Windows
Windows (95 and later) base R-x.x.x-win32.exe
3. R R
5. R
R http://cran.r-project.org/other-docs.
html bbs R
6. R R-intro
R-intro R R
R-intro
7. R
R R
Modern Applied Statistics with SVenables and Ripley
The New S Language: A Programming Environment for Data Analysis and Graphics
Richard A. BeckerJohn M. ChambersAllan R.Wilks
A Handbook of Statistical Analysis Using RBrian S. EverittTorsten Hothorn
Data Analysis and Graphics using RMaindonald and Braun
Introductory Statistics with RDalgaard
8. R
R
R
9. R
100 100
x < rnorm ( 1 0 0 , mean = 5 , sd = 0 . 1 )
mean ( x )
sd ( x )
summary ( x )
demo() R
demo ( g r a p h i c s )
10. R
c i t a t i o n ( package = package )
13.
R ?topic help(topic) topic
help.search(topic)
apropos(tab)
tab
R Site Searchhttp://finzi.psych.upenn.edu/search.
html R RSiteSearch()
14. R
R R CMD Rterm
r maxmems i z e =1Gb
RHOME/bin 2
r g u i maxmems i z e =1Gb
R memory.limit R
R 32Mb 3Gb Windows
2.5.0 R Tab R
Tab
18.
rm()
1
rm( l i s t = l s ( a l l = TRUE) )
19.
options(digits = ) digits 1 22 7options
(prompt) (show.error.messages)
20.
system() shell.exec() :
1
go
to
the
cran
Rterm
cran.r-project.org ) , w a i t = FALSE)
invoke
the
notepad
system ( "notepad" )
s h e l l . e x e c ( "C:/WINDOWS/clock" )
21. Windows R packages
R library library
update.packages() R R update.packages()
22. packages
23. R
Windows XP R
C: \ Documents and S e t t i n g s \ username
R
3
26. library()
R
d e t a c h ( "package:pkg" )
R Package LibraryLibrary
Package Package Library
Windows *.zip
28. Package
3R
UnixMacWindows
search()
1
. p a c k a g e s ( a l l . a v a i l a b l e = TRUE)
R R Package
R
stats
grid
R
graphics
grDevices
utils
datasets
methods
base
29. R
R datasets 100 data()
dim ( data ( ) $ r e s u l t s )
data ( ) $ r e s u l t s [ , 4 ]
30. R
RS class
1
2
3
4
5
6
7
charactor
numeric
integer
logical
complex
list
factor
TRUE=TFALSE=F
a
S
16
RS
Everything in S is an object;
Every object is S has a class.
31. data frame
6
data frame
matrix-like
32.
R
dist
Generic Function
methods() 4
1
summary
It
is
generic
methods ( summary )
summary . lm
you
# maybe
funciton
list
of
want
the
to
S3
know
methods
the
linear
models s
summary
*.tar.gz R-2.5.1.tar.gz
33.
head() tail()
1
head (CO2)
car some
34. R
H lattice |
35. R
1 e10 == 10000000000
2
1 . 2 e4 == 0 . 0 0 0 1 2
4
getAnywhere()
36. R
Excel Excel
Excel
1. Excel csv(Comma Separated Values) read.csv()
2. RODBC odbcConnectExcel() xls
l i b r a r y (RODBC)
z < odbcConnectExcel ( "rexceltest.xls" )
dd < s q l F e t c h ( z , "Sheet1" )
close (z)
R Data Import/Export
3. xlsReadWrite read.xls
38. R
sink()
data (CO2)
2
s i n k ( "CO2.txt" )
CO2
sink ()
go
to
your
work
directory ,
you
will
get
CO2 . t x t
39. R
R capture.output() R
glmout < c a p t u r e . output ( example ( glm ) )
2
glmout [ 1 : 5 ]
Go to LATEX!
40. R
w r i t e . t a b l e ( "clipboard" )
41. (factor)
f
a s . numeric ( a s . c h a r a c t e r ( f ) )
2
or
a s . numeric ( l e v e l s ( f ) ) [ a s . i n t e g e r ( f ) ]
42. R
edit() fix()
1
fix (x)
43. source()
44. R TEX
require ( xtable )
wordpowerpoint
LATEX quantreg latex.table()
45.
R
1
d : \ \R 2 . 4 . 1 \ \ l i b r a r y \\ x g o b i \\ s c r i p t s \\ x g o b i . bat
d : /R 2 . 4 . 1 / l i b r a r y / x g o b i / s c r i p t s / x g o b i . bat
file.choose() Windows
choose.dir() Windows ,
46. R
SQL R SQL
OracleMicrosoft SQL ServerIBM DB2 GNU
General Public License (GPL) MySQL
MySQL RODBC
ODBC R
RMySQL
l i b r a r y (RODBC)
ch < odbcConnect ( "stocksDSN" , u i d = "myuser" ,pwd = "mypassword" )
47.
R NAnot available RS NA 6
x < NA
x > 3
class (x)
x == NA
is.na()
x [ ! i s . na ( x ) ]
48.
a < "print(x)"
class (a)
append()
5
6R
DBI
TRUEFALSENA
10
x < 1 : 5
2
( f o o < c ( x [ 1 ] , 0 , x [ 2 : 5 ] ) )
expected
result
append ( x , 0 , a f t e r = 1 )
50. ()
subset(select = )
1
print (x)
new . x1 < x[ c ( 1 , 4 ) , ]
#row
new . x2 < x [ , c [ 2 , 3 ] ]
#col
new . x1 ; new . x2
subset
i S < i r i s $ S p e c i e s == "setosa"
2
i r i s [ iS , c ( 1 , 3 ) ]
s u b s e t ( i r i s , s e l e c t = c ( S e p a l . Length , P e t a l . Length ) , S p e c i e s == "setosa" )
51.
a2 [ [ 3 , 1 ] ] < 2 > a2 [ [ 8 , 2 ] ]
any ( a1 !=a2 )
a l l ( a1
==
a2 )
any()
a1 !=a2
i n d e n t i c a l ( a1 , a2 )
unique unique
11
x < c ( 9 : 2 0 , 1 : 5 , 3 : 7 , 0 : 8 )
unique ( x )
( xu < x [ ! d u p l i c a t e d ( x ) ] )
#
is
more
efficient
duplicated
53. array
aperm
1
< a r r a y ( 1 : 2 4 , 2 : 4 )
xt < aperm ( x , c ( 2 , 1 , 3 ) )
3
dim ( x )
dim ( xt )
54. list
R NULL
1
or
lst
$ a $ b <
NULL
lst
55. ()
apply()
1
vec =1:20
mat=matrix ( vec , n c o l =4)
vec
cumsum ( vec )
mat
apply ( mat , 2 , cumsum )
(Regular Expression)
*.R source(*.R) #
i f (FALSE) {
something passby
}
57. (data frame)
transform() ?transform
12
58.
pmax() pmin()
1
x < 1 : 1 0
y < r e v ( x )
pmax ( x , y )
pmin ( x , y )
59.
tapply()
n < 1 7 ; f a c < f a c t o r ( r e p ( 1 : 3 , l e n = n ) , l e v e l s = 1 : 5 )
2
table ( fac )
t a p p l y ( 1 : n , f a c , sum )
t a p p l y ( 1 : n , f a c , mean )
##
or
reverse
list
t o < l i s t ( a = 1 , b = 1 , c = 2 , d = 1 )
t a p p l y ( t o , u n l i s t ( t o ) , names )
tapply() meanvarsd
aggregate() tapply() 7
1
a t t a c h ( warpbreaks )
t a p p l y ( breaks , l i s t ( wool , t e n s i o n ) , mean )
from
the
help
a g g r e g a t e ( s t a t e . x77 ,
l i s t ( Region = s t a t e . r e g i o n ,
Cold = s t a t e . x77 [ , "Frost" ] > 1 3 0 ) ,
mean )
60.
sapply(dataframe, is.numeric)
61.
sample()
sample(n)
sample(x)
sample(x, replace = T)
sample(x,n)
sample(x,n, replace = T)
sample(x,n, replace = T ,prob = p)
1, . . . , n
x, length(x) > 1
x n
x n
p x n
13
n < 1000
2
17
rnorm ( 1 0 0 , mean=0, sd =1)
62.
scale
1
only
centering
n < s c a l e ( x , c e n t e r = F)
only
scaling
> fivenum ( c ( 1 , 4 , 6 , 1 7 , 5 0 , 5 1 , 7 0 , 1 0 0 ) )
[1]
5.0
33.5
60.5 100.0
1.0
1.00
25%
5.50
50%
75%
100%
33.50
55.75 100.00
8 14 , 34
1
1
1 + (length(x) 1),
4
4
3
3
1 + (length(x) 1),
4
4
1+
8 summary()
21
7
= 2.75, 1 +
= 6.25
4
4
14
4 + (6 4) 0.75 = 5.5, 51 + (70 51) 0.25 = 55.75
fivenum() NL NU
length(x) lenght(x)
NL , NU
65.
R integrate
1
i n t e g r a t e ( dnorm , 1.96 , 1 . 9 6 )
i n t e g r a t e ( dnorm , I n f , I n f )
## a
slowly convergent
integral
i n t e g r a n d < f u n c t i o n ( x ) {1 / ( ( x+1) s q r t ( x ) ) }
5
i n t e g r a t e ( i n t e g r a n d , l o w e r = 0 , upper = I n f )
66.
x < 1 : 1 0
t (x)
class ( t (x))
t ( t (x)) ;
class (x)
class ( t ( t (x )))
x%%x
X T Y crossprod() 9 outer()
p
sin x2 + y 2
10 p
x2 + y 2
R
1
15
67. R
complex()
x < 1 + 1 i
2
Mod( x )
# x
<
complex (1 ,1)
; Conj ( x )
68.
diag() Z Z
69.
1 2
2
1 1
eigen A =
2 1 2 B = ( 2A ) + E
2 2 1
A < matrix ( c ( 1 ,2 ,2 ,2 , 1 , 2 ,2 , 2 , 1) ,3 ,3)
2
m < s o l v e ( 0 . 5 A) + d i a g ( c ( 1 , 1 , 1 ) )
e i g e n (m)
solve()
1
a%%x = b
x
70.
lower.tri() upper.tri()
1
Rmat
71.
x(1/3) R sqrt() R
"" ( x , 1/ 3 )
R
R
+,-,*,/,,%%,%/%
>, <, >=, <=, ==, ! =
72. ()
16
m < 1000
n < 3000
Cnm =
!n
!m!(n m)
74. R
D()
f 1 < e x p r e s s i o n ( s i n ( x ) x )
2
f 2 < e x p r e s s i o n ( x 2 y + y 2 )
D( f , "x" )
75.
R
20
R-intro Probability distributions
h e l p . s e a r c h ( "distribution" )
0.4
0.3
XN (3, 1)
pnorm
0.1
pnorm ( 5 , 3 , 1 ) pnorm ( 2 , 3 , 1 )
P(2<x<5)
0.8185946
0.0
0.2
Density
P (2 6 X 6 5)
76.
17
uniroot() uniroot
f <f u n c t i o n ( x ) x 3 2 x 1
uniroot ( f , c (0 ,2))
optimize()
f < f u n c t i o n ( x ) x 2 + 2x + 1
o p t i m i z e ( f , c ( 2 ,2))
77. R
R Unix R tolower() toupper() casefold()
c h a r t r ( "iXs" , "why" , x )
c h a r t r ( "a-cX" , "D-Fw" , x )
tolower (x)
toupper ( x )
78. R
paste()
1
f o r ( var i n l e t t e r s [ 1 : 6 ] ) {
x < var
# You
will
get
FOO
a . txt
.. .
help(regex)
80.
substr()
18
s u b s t r ( "abcdef" , 2 , 4 )
s u b s t r i n g ( "abcdef" , 1 : 6 , 1 : 6 )
81.
nchar
nchar ( month . name [ 9 ] )
82.
d2 < c ( "07/02/07" )
D1 + 2
D1 D2
d i f f t i m e (D1 , D2 , u n i t s = "days" )
83. , 22 2007
format()
format ( ( Sys . Date ( ) , format="%A, %d %B %Y" )
84.
19
l a y o u t ( matrix ( c ( 1 , 1 , 1 ,
2, 3, 4,
2 , 3 , 4 ) , nr = 3 , byrow = T) )
h i s t ( rnorm ( 2 5 ) , c o l = "VioletRed" )
5
h i s t ( rnorm ( 2 5 ) , c o l = "VioletRed" )
h i s t ( rnorm ( 2 5 ) , c o l = "VioletRed" )
h i s t ( rnorm ( 2 5 ) , c o l = "VioletRed" )
85.
0 : 25 26
pch
#, %, , |, +, , ., o, O21 : 25
points
bg=
1
11 13 15 17 19 21 23 25
10 12 14 16 18 20 22 24
abline()y h=x v= a=
b=
R High levelLow level Interactive
88.
dfunction(x)
20
x=s e q ( 3 , 3 , . 0 5 )
p l o t ( x , dnorm ( x ) , type="l" )
l i n e s ( x , dt ( x , 1 ) , c o l = "red" )
beta
beta
shape1(),shape2()
binom
size(n),prob(p)
chisq
df
unif
min(a),max(b)
min = 0, max = 1
exp
rate
rate = 1
df1(r1 ),df2(r2 )
gamma
shape(),scale()
hyper
m = N1 , n = N2 , k = n
norm
mean(),sd()
pois
lamda()
df
weibull
shape(),scale()
scale = 1
mean = 0, sd = 1
scale = 1
89.
legendhelp(legend)
1
pie()
bar chartdot chart
91.
stem
stem ( f a i t h f u l $ e r u p t i o n s )
92.
21
GDP (UR)
1
y e a r < 1 9 9 5 : 2 0 0 5
x1 < data . frame ( y e a r , GDP = s o r t ( rnorm ( 1 1 , 1 0 0 0 , 1 0 0 ) ) )
p l o t ( x1 , a x e s = FALSE, type="l" )
a x i s ( 1 , a t = year , l a b e l = y e a r ) ; a x i s ( 2 )
= "red" )
plotrix twoord.plot()
twoord . p l o t ( 2 : 1 0 , s e q ( 3 , 7 , by=0.5)+ rnorm ( 9 ) ,
2
1 : 1 5 , r e v ( 6 0 : 7 4 ) + rnorm ( 1 5 ) , x l a b="Sequence" ,
y l a b="Ascending values" , r y l a b="Descending values" ,
main="Test of twoord.plot" )
93.
grid()
plot (1:3)
2
g r i d (NA, 5 , lwd = 2 )
grid
only
in
y d i r e c t i o n
94.
strwrap
p l o t ( 0 , main = p a s t e ( s t r w r a p ( "This is a really long title that
2
dev.list() dev.cur()
dev.set()dev.off()
96. y
22
? par
see
las
x11
Windows
windows
ps
postscript
pdf
png
EX
png
jpeg
jpeg
bmp
bmp
pictex
xfig
win.metafile
a
LAT
XFIG
emf
Windows
postscript() ps
98.
R stars
1
s t a r s ( s t a t e . x77 [ , c ( 7 , 4 , 6 , 2 , 5 , 3 ) ] , f u l l = FALSE,
key . l o c = c ( 1 0 , 2 ) )
99. R 8
col R 8
palette ()
2
b a r p l o t ( rnorm ( 1 5 , 10 , 3 ) , c o l = 1 : 1 5 )
b a r p l o t ( rnorm ( 1 5 , 10 , 3 ) , c o l = 1 : 1 5 )
p a l e t t e ( rainbow ( 1 5 ) )
p a l e t t e ( "default" )
R 657
23
colors ()
R 502
rgb()
100.
rainbow()
heat.colors()
terrain.colors()
topo.colors()
cm.colors()
gay()grey()
1
x < 1 : 1 0
)
()
()
()
()
()
(
; names ( x ) < l e t t e r s [ 1 : 1 0 ]
b a r p l o t ( x , c o l = gray ( ( 1 : 1 0 ) / 1 0 ) ) ;
101. R RGB
col2rgb()
1
w r i t e . t a b l e ( t ( c o l 2 r g b ( rainbow ( 7 ) ) / 2 5 5 ) , s e p = "," )
102.
Windows savePlot()
windows ( width = , h e i g h t = )
##
##
start
file
drawing
dev . o f f ( )
###
commands
close
the
here
PDF
file
24
103.
two
dimensions
n < 100
3
p l o t ( x , y , type = l )
104.
x < 1 : 5
y < c ( 1 , 3 , 4 , 2 . 5 , 2 )
plot (x , y)
sp < s p l i n e ( x , y , n = 5 0 )
l i n e s ( sp )
105. (lattice)
library ( lattice )
h i s t o g r a m ( h e i g h t | v o i c e . part , data = s i n g e r )
lattice
lattice
xyplot(yx)
Cleveland ()
y x
x y
dotplot(yx)
barchart(yx)
stripplot(yx)
bwplot(yx)
histogram(x)
106.
25
persp() contour() 15
outer()
107.
image() filled.contour()
x < y < s e q ( 10 , 1 0 , l e n g t h =50)
2
f < f u n c t i o n ( x , y ) {
r < s q r t ( x 2 + y 2 )
10 s i n ( r ) / r
}
6
z < o u t e r ( x , y , f )
image ( x , y , z )
f i l l e d . contour ( x , y , z )
108.
R R
x < 1 : 1 0
2
y < r u n i f ( 1 0 )
symbols ( x , y , c i r c l e s = y/2 , i n c h e s = F , bg = x )
109. QQ
apply() R
1
wait
for
changing
r e s u l t s = apply ( t a b l e , 2 , qqnorm )
par ( ask=FALSE)
110.
par()
x < rnorm ( 1 0 0 )
2
hist (x)
op < par ( f i g=c ( . 0 2 , . 5 , . 5 , . 9 8 ) , new=TRUE)
boxplot (x)
111. R
?plotmath LATEX
26
x < 1 : 1 0
2
p l o t ( x , type = "n" )
t e x t ( 6 , 6 , e x p r e s s i o n ( hat ( b e t a ) == (X t X) { . 1 } X t y ) )
t e x t ( 8 , 8 , e x p r e s s i o n ( z [ i ] == s q r t ( x [ i ] 2 + y [ i ] 2 ) ) )
112. bar
barplot() rect()
1
x < 1 : 1 0
; names ( x ) < l e t t e r s [ 1 : 1 0 ]
b < b a r p l o t ( x , c o l = r e v ( h e a t . c o l o r s ( 1 0 ) ) )
3
t e x t ( b , x , l a b e l s = x , pos = 3 )
113.
y
x
( )2 + ( )2 = 1 = x = a sin , y = b cos , 0 < < 2
a
b
1
t < s e q ( 0 , 2 pi , l e n g t h = 1 0 0 )
x < s i n ( t )
a =1
y < 2 c o s ( t )
b =2
p l o t ( x , y , type = l )
114. word R
epspdfpngjpgtiff word
eps
115.
FBasics
skewness ( )
2
kurtosis ()
116.
table() table(x) x table(x,y)
27
prop . t a b l e ( x , 1 )
117.
yi = + xi + i
lm()
lm . s w i s s < lm ( F e r t i l i t y . , data = s w i s s )
lm()
coefficients
residuals
fitted.values
. . . . . .
:
1
names ( lm . s w i s s )
summary() anova()
summary ( lm . s w i s s )
the
same
as
summary . lm ( )
anova ( lm . s w i s s )
118.
update()
summary ( f 0 < lm ( F e r t i l i t y . , data = s w i s s ) )
2
u t i l s : : example ( lm )
s t e p ( lm . D9)
28
quantreg quantregForest
data ( e n g e l )
2
t a u s < c ( . 1 5 , . 2 5 , . 5 0 , . 7 5 , . 9 5 , . 9 9 )
r q s < a s . l i s t ( t a u s )
l i n e s ( l o g 1 0 ( e n g e l $ income ) , f i t t e d ( r q s [ [ i ] ] ) , c o l = i +1) }
8
t.test()
1
x < rnorm ( 1 0 0 )
t . test (x)
122.
K (kmeans() )
x < r b i n d ( matrix ( rnorm ( 1 0 0 , sd = 0 . 3 ) , n c o l = 2 ) ,
segments ( x [ c l $ c l u s t e r = = 1 , ] [ , 1 ] , x [ c l $ c l u s t e r = = 1 , ] [ , 2 ] ,
cl $ centers [1 ,1] , cl $ centers [1 ,2])
segments ( x [ c l $ c l u s t e r = = 2 , ] [ , 1 ] , x [ c l $ c l u s t e r = = 2 , ] [ , 2 ] ,
cl $ centers [2 ,1] , cl $ centers [2 ,2] ,
10
c o l =2)
(hclust() )
n < s e q ( 1 , 5 0 , by = 4 )
2
( x < USArrests [ n , ] )
hc1 < h c l u s t ( d i s t ( x ) ,
print ()
method = "complete" )
hc3 < h c l u s t ( d i s t ( x ) ,
6
method = "ave" )
cutree()
1
c u t r e e ( hc , k = 1 : 3 )
cluster
1
library ( cluster )
c l u s p l o t ( x , pam( x , 2 ) $ c l u s t e r i n g )
123.
stats princomp
( pc . c r < princomp ( USArrests , c o r = TRUE) )
p l o t ( pc . c r , type = "lines"
)
or
or
barplot
screeplot
l o a d i n g s ( pc . c r )
princomp() cor = TRUE
loadings() screeplot()
124.
R factanal()
example ( f a c t a n a l )
125.
126. t
t.test() paired
1
require ( stats )
##
Student s
paired
ttest
30
SPSS
a p r o po s ( "test" )
bartlett.test
chisq.test
fisher.test
kruskal.test
pairwise.t.test
var.test
2
Fisher
Kruskal-Wallis
binom.test
cor.test
friedman.test
mcnemar.test
PP.test
wilcox.test
extra
group
0.7
-0.6
4.6
3.4
Friedman
McNemar
Phillips-Perron
Wilcoxon
127. R
sem
128.
I()
1
lm ( y x + I ( x 2 ) + I ( x 3 ) )
129. ANOVA
aov()
aov(x a)
aov(x a + b)
aov(x a + b +a:b)
aov(x a*b)
130.
0
1
c o n f i n t ( f i t , "wt" )
132.
MASS lm.ridge()
1
data ( l o n g l e y )
not
the
same
as
the
S PLUS
dataset
lm . r i d g e ( y . , l o n g l e y )
p l o t ( lm . r i d g e ( y . , l o n g l e y ,
lambda = s e q ( 0 , 0 . 1 , 0 . 0 0 1 ) ) )
s e l e c t ( lm . r i d g e ( y . , l o n g l e y ,
lambda = s e q ( 0 , 0 . 1 , 0 . 0 0 0 1 ) ) )
133. logistic
logistic 01
glm()
Gaussian
E(y) = x
exp (xT )
1+exp (xT )
T
Binomial
E(y) =
Poission
E(y) = exp (x )
Logistic probit
a Normal
f i t 1 < lm ( formula ,
data )
134.
yi = 0 + 1 xi + 2 x2i + . . . + k xki , i = 1, 2, . . . , n,
k x, x2 , . . . , xk
R poly()
1
( z < p o l y ( 1 : 1 0 , 3 ) )
32
135.
hat(),hatvalues()
136. D-W
car durbin.watson lmtest dwtest
1
h e l p . s e a r c h ( "Durbin-Watson" )
137. Spearman kendall
cor() Person method Kendall Spearman
c o r ( l o n g l e y , method = "spearman" )
Pearson
Kendall
Spearman
Tree-based model
Modern Applied Statistics With S rpart
1
library ( rpart )
set . seed (123)
t e x t ( cpus . rp , d i g i t s = 3 )
139.
t 0 2 q
Xt =
q
X
j tj
p
Xt =
p
X
i Xti + t
ARM A(p, q)
Xt =
p
X
i Xti +
q
X
0
33
j tj
( f i t 1 < arima ( p r e s i d e n t s , c ( 1 , 0 , 0 ) ) )
tsdiag ( f i t 1 )
140. box-cox
MASS
1
boxcox ( )
141. Breusch-Pagan
lmtest bptest() car ncv.test()
142.
qr.solve()
1
qr . s o l v e (X, y )
(X 0 X)1 X 0 y
144.
R cancor()
pop < L i f e C y c l e S a v i n g s [ , 2 : 3 ]
o e c < L i f e C y c l e S a v i n g s [ ,
(2:3)]
c a n c o r ( pop , o e c )
145. R
survival
1
fit
Kaplan Meier
and
plot
it
plot ( f i t )
#
life
table
c b i n d ( f i t $ time , f i t $n . r i s k , f i t $n . event , f i t $ s u r v )
34
146. R
l i b r a r y ( Rpad )
Rpad ( )
enjoy
it
147. R SPSS
Rcmdr
Commander ( )
149. R
R maps mapdata mapdata
1
l i b r a r y ( mapdata )
map( "china" )
map
map( "state" )
maptools
PBSmapping, spatstat, maps, RArcInfo, Stata tmap, WinBUGS, Mondrian
150. Sweave
Sweave TEX S Sweave
TEX S
35
TEX
S
S
Sweave(in R)
LAT X
gc ( )
R R
gc()
152.
10
WinEdt R RWinEdt
wordfile
11
36
Sweave
\documentclass[CJK]{cctart}
\usepackage{verbatim}
\title{Sweave }
\author{}
\date{}
\SweaveOpts{echo=FALSE}
\begin{document}
\maketitle
Sweave \LaTeX{} R
\LaTeX{}
\LaTeX{}
R $<<>>=$ $@$
echofig, R
\LaTeX{} \LaTeX{}
R \LaTeX{}
t
<<echo = TRUE>>=
require(stats)
## Students paired t-test
m <- t.test(extra group, data = sleep, paired = TRUE)
print(m)
@
R
\texttt{sleep}t
$p$-value\Sexpr{format.pval(m$p.value)}
<<echo=TRUE,results=hide>>=
choose(49,6)
@
\Sexpr{choose(49,6)}
word
37
Sweave R \LaTeX{}
\LaTeX{} \footnote{Sweave ps pdf }
Titanic
<<fig=TRUE,echo=FALSE>>=
require(graphics)
mosaicplot(Titanic, main = "Survival on the Titanic")
@
\end{document}
c
Copyright 2008
R and all the Contributors to R FAQ. All rights reserved.
c
R R FAQ 2008
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free
Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with the
Invariant Sections being Contributors, no Front-Cover Texts, and no Back-Cover Texts.
GNU 1.2
Invariant Sections Contributors Front-Cover Texts Back-Cover Texts
38
Symbols
col . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 ESS . . . . . . . . . . . . . . . . . . . . . . . . . 36
dev.copy . . . . . . . . . . . . . . . . . . . . . 25
choose . . . . . . . . . . . . . . . . . . . . . . . 17
citation . . . . . . . . . . . . . . . . . . . . . . . 3
image. . . . . . . . . . . . . . . . . . . . . . . .26
iris . . . . . . . . . . . . . . . . . . . . . . . . . . 21 ncv.test . . . . . . . . . . . . . . . . . . . . . . 34
qqnorm . . . . . . . . . . . . . . . . . . . . . . 26
qr.solve . . . . . . . . . . . . . . . . . . . . . . 34
O
J
jarque.bera.test . . . . . . . . . . . . . . 30
jpeg . . . . . . . . . . . . . . . . . . . . . . . . . 24
K
kmeans . . . . . . . . . . . . . . . . . . . . . . 29
ks.test . . . . . . . . . . . . . . . . . . . . . . . 30
rainbow . . . . . . . . . . . . . . . . . . . . . 24
read.table . . . . . . . . . . . . . . . . . . . . . 8
P
Package . . . . . . . . . . . . . . . . . . . . . . .5
cluster . . . . . . . . . . . . . . . . . . 30
plotrix . . . . . . . . . . . . . . . . . . 22
latex . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Rcmdr . . . . . . . . . . . . . . . . . . 35
latex.table . . . . . . . . . . . . . . . . . . . . 9
Rpad. . . . . . . . . . . . . . . . . . . .35
layout . . . . . . . . . . . . . . . . . . . . . . . 19
rpart . . . . . . . . . . . . . . . . . . . . 33
lda . . . . . . . . . . . . . . . . . . . . . . . . . . 34
sem . . . . . . . . . . . . . . . . . . . . . 31
legend . . . . . . . . . . . . . . . . . . . . . . . 21
stats . . . . . . . . . . . . . . . . . . . . 30
letters . . . . . . . . . . . . . . . . . . . . . . . 24
tseries . . . . . . . . . . . . . . . . . . . 30
read.xls . . . . . . . . . . . . . . . . . . . . . . . 8
rect . . . . . . . . . . . . . . . . . . . . . . . . . .27
Regular Expressions . . . . . . 12, 18
rev . . . . . . . . . . . . . . . . . . . . . . . . . . 13
rgb . . . . . . . . . . . . . . . . . . . . . . . . . . 24
rm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
RMySQL . . . . . . . . . . . . . . . . . . . . 10
rnorm . . . . . . . . . . . . . . . . 17, 22, 25
RODBC . . . . . . . . . . . . . . . . . . . . . 10
rowMeans . . . . . . . . . . . . . . . . . . . 17
Library . . . . . . . . . . . . . . . . . . . . . . . 5 par . . . . . . . . . . . . . . . . . . . 19, 22, 26
RSiteSearch. . . . . . . . . . . . . . . . . . .3
library . . . . . . . . . . . . . . . . . . . . . . . . 5 parplot . . . . . . . . . . . . . . . . . . . . . . 27
RWinEdt . . . . . . . . . . . . . . . . . . . . 36
lines . . . . . . . . . . . . . . . . . . . . . . . . . 25 parse . . . . . . . . . . . . . . . . . . . . . . . . 10
list . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 paste . . . . . . . . . . . . . . . . . . . . . . 4, 18
S
lm . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 pch . . . . . . . . . . . . . . . . . . . . . . . . . . 21
sample. . . . . . . . . . . . . . . . . . . . . . .13
lm.ridge . . . . . . . . . . . . . . . . . . . . . 32 pdf . . . . . . . . . . . . . . . . . . . . . . . . . . 24
save . . . . . . . . . . . . . . . . . . . . . . . . . . 5
lmtest . . . . . . . . . . . . . . . . . . . . . . . 34 persp . . . . . . . . . . . . . . . . . . . . . . . . 26
save.image . . . . . . . . . . . . . . . . . . . . 5
loadings . . . . . . . . . . . . . . . . . . . . . 30 pictex . . . . . . . . . . . . . . . . . . . . . . . 24
savePlot . . . . . . . . . . . . . . . . . . . . . 24
lower.tri . . . . . . . . . . . . . . . . . . . . . 16 pie . . . . . . . . . . . . . . . . . . . . . . . . . . 21
scale . . . . . . . . . . . . . . . . . . . . . . . . . 14
ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 plotmath . . . . . . . . . . . . . . . . . . . . 26
screeplot . . . . . . . . . . . . . . . . . . . . . 30
pmax . . . . . . . . . . . . . . . . . . . . . . . . 13 search . . . . . . . . . . . . . . . . . . . . . . . . 6
M
pmin . . . . . . . . . . . . . . . . . . . . . . . . 13 setwd . . . . . . . . . . . . . . . . . . . . . . . . . 5
mai . . . . . . . . . . . . . . . . . . . . . . . . . . 20
png . . . . . . . . . . . . . . . . . . . . . . . . . . 24 shapiro.test . . . . . . . . . . . . . . . . . . 30
mar . . . . . . . . . . . . . . . . . . . . . . . . . 20
points . . . . . . . . . . . . . . . . . . . . . . . 20 shell.exec . . . . . . . . . . . . . . . . . . . . . 4
matrix . . . . . . . . . . . . . . . . . . . 12, 17
poly . . . . . . . . . . . . . . . . . . . . . . . . . 32 show.error.messages . . . . . . . . . . 4
memory.limit . . . . . . . . . . . . . . . . . 4
postscript. . . . . . . . . . . . . . . . . . . .24 sink . . . . . . . . . . . . . . . . . . . . . . . . 8, 9
merge . . . . . . . . . . . . . . . . . . . . . . . 14
methods . . . . . . . . . . . . . . . . . . . . . . 7
spline . . . . . . . . . . . . . . . . . . . . . . . . 25
W
windows . . . . . . . . . . . . . . . . . . . . . 24
with . . . . . . . . . . . . . . . . . . . . . . . . . 21
X
41