R:
, ,
, .
2010
1
1.1 . . . . .
1.2
1.3 . . . .
1.4 R
1.5 . . . .
1.6 . . . . . .
. . . . .
R
. . . . .
. . . . .
. . . . .
. . . . .
. .
.
. .
. .
. .
. .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2 R
2.1 . . . . . .
2.1.1 . . . . . . . . . . . . . . . . .
2.1.2 . . . . . . .
2.1.3 . . . . . . . . . . . . . . .
2.1.4 , . . .
2.2 . . . . . . . . . . . . . . . . . . . . .
2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.4 ,
2.4.1 R . . . . . . . . . . . . . . .
2.4.2 R . . . . . . . . . .
2.4.3 R .
2.4.4 R . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. .
. .
. .
. .
. .
. .
. .
R
. .
. .
. .
. .
3 .
3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 . . . . . . . . . . . . . . . . . . . . . .
3.2.1 . . . . . . . . . . . . . . . . . .
3.2.2 . . . . . . . . . . . . . . .
3.2.3 . . . . . . . . . . . . .
3.2.4 . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
R
. .
. .
. .
. .
. .
. .
.
.
.
.
.
.
5
5
5
6
6
7
8
.
.
.
.
.
.
.
.
.
.
.
.
9
9
9
10
13
14
15
20
21
21
28
30
31
.
.
.
.
.
.
33
33
33
33
35
40
41
4 .
R
4.1 R . . . . . . . . . . . . . . . . . . .
4.1.1 if . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.1.2 ifelse . . . . . . . . . . . . . . . . . . . . . . . .
4.1.3 for . . . . . . . . . . . . . . . . . . . . . . . . . .
4.1.4 while . . . . . . . . . . . . . . . . . . . . . . . .
4.1.5 repeat, break next . . . . . . . . . . . . . .
4.1.6 switch . . . . . . . . . . . . . . . . . . . . . . . .
4.2 R . . . . . . . . . . . . . . . . . . . . . . .
4.2.1 R . . . . . . . . .
4.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.3 , . . . . . . . . . . . . . . . . . . . . . .
4.2.4 R . . . . . . . . . . . .
4.2.5 R . . . . . . . . . . . . . . . . . .
4.2.6 apply(), sapply() lapply() . . . . . . . . . . .
4.2.7 R
. . . . . . . . . . . . . . . . . .
5 R
5.1 . . . . . . . . . . . . . . . . . . . . . .
5.2 . . . . . . . . . . . . . . . . . . . . . . . . .
5.2.1 . . . . . . . . .
5.2.2 . . . . . . . .
5.2.3 . . . . . . . . . . . . . . .
5.2.4 . . . . . . . . . . . . . .
5.2.5 . . . . . .
5.2.6 . . . . . . . . . . . . . .
5.2.7 which(), duplicated() unique()
5.3 . . . . . . . . . . . . . . . . . . . . . . . .
5.3.1 . . . . . . . . . . . . . . .
5.3.2 . . . . . . . . . . .
5.3.3 . . . . . . . . . . . .
5.4 . . . . . . . . . . . . . . . .
5.5 . . . . . . . . . . . . . . . . . . . . . . . . .
5.6 . . . . . . . . . . . . . . . . . .
5.6.1 factor() . . . . . . . . . . . . . .
5.6.2 table() . . . . . . . . . . . . . .
5.7 . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
43
43
43
45
46
47
47
48
49
49
51
52
53
55
56
60
62
62
62
62
66
69
76
77
78
82
84
84
89
96
97
99
106
106
111
114
6 R
6.1 R . . . . . . . . . . . . . . . . . . . . . . . .
6.1.1 scan( ) . . . . . . . . . . . . . . . . . . . .
6.1.2 read.table() read.csv() . . . . . . . . .
6.2 R . . . . . . . . . . . . . . . . . . . . . . .
6.2.1 write() . . . . . . . . . . . . . . . . . . . .
6.2.2 cat() . . . . . . . . . . . . . . . . . . . . .
6.2.3 write.table(), write.csv() write.csv()
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
117
117
117
119
122
122
122
123
7 R
127
7.1 . . . . . . . . . . . . . . . . . . . . . . 127
7.1.1 par() . . . . . . . . . . . . . . . . . . . . . . . . . 129
7.1.2 plot() . . . . . . . . . . . . . . . . . . . . . . . . 132
7.1.3 . . . . . . . . . . . . . . . 137
7.1.4 contour() . . . . . . . . . . . . . . . . . . . . . . 140
7.1.5 curve() . . . . . . . . . . . . . . . . . . . . . . . 144
7.1.6 R . . . . . . . . . . . . . . . . . . . . . . . 145
7.2 . . . . . . . . . . . . . . . . . . . . . . . 152
7.2.1
abline(), lines(), arrows(), points(), polygon(), rect(),
segments(), symbols() . . . . . . . . . . . . . . . . . . . . 153
7.2.2 axis(), grid() box() 165
7.2.3 expression(),
text(), legend(), mtext() title() . . . . . . . . . . . . . 170
8 . .
182
8.1 182
8.1.1 uniroot . . . . . . . . . . . . . . . . . . . . . . . 182
8.1.2 uniroot.all . . . . . . . . . . . . . . . . . . . . . 184
8.1.3 multiroot . . . . . . . . . . . . . . . . . . . . . . 185
8.2 . . 186
8.2.1
. . . . . . . . . 186
8.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
1.1
34
,
,
R.
1.2
R
, GNU, .
R , . , ,
. R
:
. R ,
, .
R, R,
, ,
, , . R, Boeing.
1.3
R S-PLUS,
S.
S 1976 AT&T Labs. S FORTRAN GCOS. 1980 UNIX.
S. 1988
, S-PLUS.
R.
1993 (Robert Gentleman Ross
Ihaka, Statistics Department, Auckland University) R. S,
S-PLUS , , , .
, () S-PLUS R. ,
, R (SAS, Stata, SYSTAT, SPSS). R
,
( 2010
2142).
R CRAN (Comprehensive R Archive
Network http://cran.r-project.org).
1.4
R.
:
R
(), http://www.rproject.org;
Windows, MacOS X, Linux;
R
, ;
6
;
,
, ;
(
);
;
;
;
R.
:
R , ,
;
(
);
R ( ), .
1.5
:
http://www.r-project.org/ R;
http://cran.gis-lab.info/ http://www.cran.r-project.org/ CRAN;
http://journal.r-project.org/ R;
https://stat.ethz.ch/mailman/r-help/ R-help;
http://wiki.linuxformat.ru/index.php/LXF100-101:R
R ;
7
http://herba.msu.ru/shipunov/software/r/rplus1.htm ;
http://www.sciviews.org/Tinn-R/
R;
http://ru.wikipedia.org/wiki/R_Commande
R;
http://cran.r-project.org/doc/contrib R
;
http://finzi.psych.upenn.edu/nmz.html R;
http://www.statmethods.net/index.html ;
http://pj.freefaculty.org/R/Rtips.html R;
http://www.uic.unn.ru/ zny/ml//Labs/
R.
1.6
3, 4 5
- . . .
2
R
2.1
2.1.1
R .
http://cran.gis-lab.info/ ,
. ( ) R R-2.12.1 16 2010 1 .
. :
( R-2.12.1), exe- (exe- base
37 ).
R Windows2 .
exe-. .
. (
, ).
. ( ). .
1
, . ,
( ), ,
. , R-2.9.2 R-2.7.2
2
, ,
.
, . ( R R-2.10.1 Program
Files)
. pdf-
(Manual).
65 .
( )
.
(
R .Rdata).
. .
2.1.2
R, , . ,
R Console. R
R version 2.12.1 (2010-12-16)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
R -- , .
.
license() .
R -- , .
contributors()
citation() R
.
demo() , help() --
, help.start() -- .
q(), R.
10
[ ]
>
(), ,
.
,
().
( ) ,
>
Enter, .
, ;.
( ) .
Enter,
>
+
# . ,
, .
( ).
,
, .
R ()
, 3 , , () a
() . , R , .. .
() R , (.) (_). ,
, 4 . ?
, .
R =, <- ( : < -) , ->
, , .
3
, , ,
4
11
1. .
> aa<-5;aa
[1] 5
> 6->bb;bb
[1] 6
> c=7;c
[1] 7
> cc<-dd<-8;cc
[1] 8
> dd
[1] 8
>
aa 5, .
bb 6
. c 7.
, , dd,
8.
, [1].
, R ( )
. 1,
[1].
> bb->cc<-7
, cc
bb, 7.
> cc=7
> cc<-5;cc
[1] 5
.
, .
> (z=6)
[1] 6
, ,
.
12
> {z=5;
+ c=4;z
+ c}
[1] 4
{} .
R , 5 (,
c() t()), . , R , , t(),
:
> t
function (x)
UseMethod("t")
<environment: namespace:base>
. base
.
, ,
. , Enter,
. , ,
,
. , .
2.1.3
R. ,
. , ,
.
, .
.
.
:
, (
Ctrl+N).
(Ctrl+O)
5
13
(Ctrl+S). , ,
,
(, ; , ),
( .R
R, S .q,
.ssc .S, , ,
.txt). ,
.
, , .
:
(Ctrl+R).
.
2.1.4
:
1.
> q()
, . , ,
. R
. R .
2. . .
.
, , .
14
R
, :
.RData .Rhistory. ,
. .
. .txt .
2.2
help() help.search().
, ,
( ) help(_). . :
help("_") help(_). ?_.
.
2. ls() .
> help(ls)
:
ls {base}
R Documentation
List Objects
Description
ls and objects return a vector of character strings giving the names of the
objects in the specified environment. When invoked with no argument at the
top level prompt, ls shows what data sets and functions a user has defined.
When invoked with no argument inside a function, ls returns the names of the
functions local variables. This is useful in conjunction with browser.
Usage
ls(name, pos = -1, envir = as.environment(pos),
all.names = FALSE, pattern)
objects(name, pos= -1, envir = as.environment(pos), all.names = FALSE, pattern)
Arguments
name
15
envir
all.names
pattern
Details
The name argument can specify the environment from which object names are taken
in one of several forms: as an integer (the position in the search list); as
the character string name of an element in the search list; or as an explicit
environment (including using sys.frame to access the currently active function
calls). By default, the environment of the call to ls or objects is used. The
pos and envir arguments are an alternative way to specify an environment, but
are primarily there for back compatibility.
Note that the order of the resulting strings is locale dependent, see Sys.getlocale.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language.
Wadsworth & Brooks/Cole.
See Also
glob2rx for converting wildcard patterns to regular expressions.
ls.str for a long listing based on str. apropos (or find) for finding objects in
the whole search path; grep for more details on regular expressions; class,
methods, etc., for object-oriented programming.
Examples
.Ob <- 1
ls(pattern = "O")
ls(pattern= " O", all.names = TRUE)
16
myfunc()
# define a local variable inside myfunc
myfunc <- function() {y <- 1; ls()}
myfunc()
# shows "y"
:
,
;
Description , ,
;
Usage ;
Arguments ,
;
Details ;
References ;
See Also ;
Examples .
( , , ),
help.search("")6 .
, .
3. > help.search("norm")
:
Help files with alias or concept or title matching norm using
regular expression matching:
boot::norm.ci
MASS::bandwidth.nrd
17
Reference Distribution
Simulate from a Multivariate Normal
Distribution
Matrix::ddenseMatrix-class
Virtual Class "ddenseMatrix" of Numeric
Dense Matrices
Matrix::dgeMatrix-class
Class "dgeMatrix" of Dense Numeric (S4
Class) Matrices
Matrix::diagonalMatrix-class
Class "diagonalMatrix" of Diagonal
Matrices
Matrix::dspMatrix-class
Symmetric Dense Numeric Matrices
Matrix::dtpMatrix-class
Packed triangular dense matrices
Matrix::%*%,dtrMatrix,dtrMatrix-method
Triangular, dense, numeric matrices
Matrix::ldenseMatrix-class
Virtual Class "ldenseMatrix" of Dense
Logical Matrices
Matrix::ndenseMatrix-class
Virtual Class "ndenseMatrix" of Dense
Logical Matrices
Matrix::norm
Matrix Norms
Matrix::sparseMatrix-class
Virtual Class "sparseMatrix" -- Mother of
Sparse Matrices
nlme::qqnorm.gls
Normal Plot of Residuals from a gls Object
nlme::qqnorm.lm
Normal Plot of Residuals or Random Effects
from an lme Object
nnet::nnet
Fit Neural Networks
stats::Lognormal
The Log Normal Distribution
stats::Normal
The Normal Distribution
stats::qqnorm
Quantile-Quantile Plots
stats::shapiro.test
Shapiro-Wilk Normality Test
utils::normalizePath
Express File Paths in Canonical Form
MASS::mvrnorm
help.search() apropos("")
18
, .
4. apropos("").
> apropos("norm")
[1] "dlnorm"
"dnorm"
[5] "pnorm"
"qlnorm"
[9] "qqnorm.default" "rlnorm"
"normalizePath"
"qnorm"
"rnorm"
"plnorm"
"qqnorm"
11 , norm.
help.start() HTML- .
,
:
. ,
R R.
R Windows ,
R Windows.
( PDF) 7 R:
(100 ) ;
, (1719 ); / (34 ); R-
; ( ) 138 ; R (51
); R.
R () , ,
( help(_)).
.
help.search().
search.r-project.org r-project.org.
7
19
apropos("").
R R.
CRAN
- CRAN.
:
ls() objects() ( , );
rm(_) .
example(_) ( ) .
history(n) , n .
getwd() ( )
> getwd()
[1] "C:/Users/1/Documents"
setwd("___") (
).
dir() .
source(_.R) R-
.R
sink(_.)
; sink() .
2.3
R :
20
1. (). CRAN ,
. ,
() .
.
.
library(_).
2. - R CRAN, http://cran.gislab.info/web/packages/ ( ,
).
. .
zip-.
, , .
library(_).
, R .
2.4
2.4.1
,
R
R
( ) R :
1. numeric , .
. ( integer) ( double real).
2. complex .
3. logical , : FALSE
(F)8 TRUE (T) .
4. character (
(), ()).
8
R ()
21
5. raw ( ,
)
: raw < logical < integer < real < complex < character.
Numeric
numeric numeric(n),
n . n.
> x=numeric(5)
> x
[1] 0 0 0 0 0
numeric 5.
integer double , , integer(n) double(n),
is.numeric(_)
(is.double(_),
is.integer(_))
numeric (double integer).
.
> x=3.7
5. x y double integer .
> x=double(1)
> x=5
> y=integer(1)
> y=7
> is.integer(x)
[1] FALSE
> is.double(x)
[1] TRUE
> is.integer(y)
[1] FALSE
> is.double(y)
[1] TRUE
> is.numeric(x)
[1] TRUE
> is.numeric(y)
[1] TRUE
22
> y=integer(1)
> is.integer(y)
[1] TRUE
. x double
5. is.integer(x)
integer. FALSE. double
numeric, TRUE. ..,
, x . y (y=integer(1)) 7.
integer, double.
? . , R . ,
as.integer(_).
6. x double, 3 as.integer().
> x=double(1)
> x=5
> is.double(x)
[1] TRUE
> x=as.integer(x)
> is.double(x)
[1] FALSE
> is.integer(x)
[1] TRUE
, x .
Complex
. complex(length.out = 0, real = numeric(), imaginary =
numeric(), modulus = 1, argument = 0).
> z=complex(5)
> z
[1] 0+0i 0+0i 0+0i 0+0i 0+0i
23
9 .
length.out = 5. length.out ( ), real imaginary
( - ).
> z=complex(5,c(1:5),c(6:10));z
[1] 1+ 6i 2+ 7i 3+ 8i 4+ 9i 5+10i
R , , .
modulus argument 10 .
> z=complex(5,modulus=c(1:5),argument=c(6:10));z
[1] 0.9601703-0.2794155i 1.5078045+1.3139732i -0.4365001+2.9680747i
[4] -3.6445210+1.6484739i -4.1953576-2.7201056i
,
:
> x=4
> y=5
> z=x+1i*y;z
[1] 4+5i
1i i = 1.
is.complex(_).
> x=5+1i+10
> is.complex(x)
[1] TRUE
> is.numeric(x)
[1] FALSE
numeric ,
numeric.
complex as.complex(_),
.
> x=3.7
> y=as.complex(x);y
[1] 3.7+0i
9
10
5.2 5.
z: z = x + iy, x = cos(), y = sin()
24
as.numeric(_),
as.double(_).
> z=2.3-1i*6
> x=as.numeric(z)
Warning message:
> y=as.double(z)
Warning message:
> x
[1] 2.3
> y
[1] 2.3
, .
3.2.4 3.
Logical
, .. logical. : TRUE ()
FALSE ()11 , logical(n), n
.
> x=logical(4)
> x
[1] FALSE FALSE FALSE FALSE
, logical(5) , FALSE.
is.logical(_).
> x=1;y=F
> is.logical(x)
[1] FALSE
> is.logical(y)
[1] TRUE
11
T F
25
as.logical(_). .
> z=as.logical(x)
> z
[1] TRUE
x 1,
. 1 TRUE,
0 FALSE. (
as.numeric(), as.double() as.integer()), F
0, T 1.
> x=F;y=T
> z1=as.numeric(x);z1
[1] 0
> (z2=as.double(y))
[1] 1
> (z3=as.integer(x))
[1] 0
3.2.1 3 , 5.2.4 5
.
Character
,
character, . .
character(n),
n.
> x=character(1);x
[1] ""
is.character().
> (x=a)
[1] "a"
> (y="a")
[1] "a"
> is.character(x)
26
[1] TRUE
> is.character(y)
[1] TRUE
( ). , .
> x=a;x
[1] "a"
> y= ;y
[1] " "
.
as.character(_).
> x=F;y=2.4
> (z1=as.character(x))
[1] "FALSE"
> (z2=as.character(y))
[1] "2.4"
.
> x=1
> y=as.numeric(x);y
[1] 1
> x=b
> y=as.numeric(x);y
NA
[1] NA
, , .
( ), NA
( ).
.
> x="F"
> (y1=as.logical(x))
[1] FALSE
> x=T
> (y1=as.logical(x))
[1] TRUE
27
T F TRUE
FALSE ( TRUE
FALSE).
5.2.2
5.
( )
mode(_).
> x=TRUE
> (y1=as.logical(x))
[1] TRUE
> mode(x)
[1] "character"
> mode(y1)
[1] "logical"
:
> x=F
> mode(x)=numeric;x
[1] 0
> mode(x)=character;x
[1] "0"
> mode(x)=complex;x
[1] 0+0i
> mode(x)=logical;x
[1] FALSE
> mode(x)=double;x
[1] 0
2.4.2
R :
Inf : ( + Inf) (
-Inf);
NA (Not Available);
NaN (Not a Number);
NULL .
28
.
.
Inf a0 , a <>
0.
> x=5/0;x
[1] Inf
> y=log(0);y
[1] -Inf
()
is.finite() ( is.infinite()):
> x=5;y=log(0)
> is.finite(x)
[1] TRUE
> is.infinite(y)
[1] TRUE
NaN , , ( ):
> x=0/0;x
[1] NaN
> y=Inf-Inf;y
[1] NaN
> y1=log(-2);y1
[1] NaN
> y2=Inf/Inf
> y2=Inf/Inf;y2
[1] NaN
> y3=cos(Inf);y3
[1] NaN
is.nan(_) ,
NaN:
> x=Inf-Inf
> is.nan(x)
[1] TRUE
NA ,
( ). NaN. ,
NA, is.na(_).
29
> x=NaN
> y=NA
> is.na(y)
[1] TRUE
> is.na(x)
[1] TRUE
NULL () .
(), .
as.null(_), NULL is.null(_).
> x=7;y=as.null(x)
> is.null(x)
[1] FALSE
> is.null(y)
[1] TRUE
R
NA.
2.4.3
12 .
( ), 1 ( ), a b
a:b.
> x=2:10;x
[1] 2 3 4
> y=10:2;y
[1] 10 9 8
9 10
seq(). :
seq(from, to) a:b.
12
5.2.3 5
30
> x=seq(from=-5,to=5);x
[1] -5 -4 -3 -2 -1 0 1
> x=seq(-5,5);x
[1] -5 -4 -3 -2 -1 0 1
0.0
0.5
1.0
1.5
2.0
0.0
0.5
1.0
1.5
2.0
( , by)
seq(from, to, length.out= ) , , . by =
((to from)/(length.out 1)).
x=seq(-5,5,length.out=10);x
[1] -5.0000000 -3.8888889 -2.7777778 -1.6666667 -0.5555556
[7] 1.6666667 2.7777778 3.8888889 5.0000000
0.5555556
seq() 1 c.
> x=seq(6);x
[1] 1 2 3 4 5 6
> y=seq(-4);y
[1] 1 0 -1 -2 -3 -4
( ,
) 5.2.3 5.
2.4.4
R. ,
5.
R :
31
( vector) , . ,
.
(matrix) , .
(array) ,
.
( factor) , .
( list) ,
.
( data.frame) , R.
32
3
. R
3.1
: ; ;
.
3.2
3.2.1
, , .
3.1:
R
!x
x&y
x && y
x|y
x || y
xor(x, y)
x y
x y
x y , TRUE, FALSE
, .
33
3.2:
>
>=
<
<= y
==
!=
,
. .
> a<b
[1] TRUE
> >=
[1] FALSE
> !=
[1] TRUE
> >
[1] FALSE
> <
[1] TRUE
> >=
[1] TRUE
,
.
> x=10000; y=a; x>y
[1] FALSE
> x=10000;y=a;y>x
[1] TRUE
:
> x=F;y=T
> x<y
[1] TRUE
, . ,
.
NA NaN NA.
34
> x=10;y=NA
> x>y
[1] NA
> x=NA;y=NaN
> x>=y
[1] NA
3.2.2
R .
( ).
e, .
> x=0.235;x
[1] 0.235
> x=.235;x
[1] 0.235
> x=23.5e-2;x
[1] 0.235
> x=235E-3;x
[1] 0.235
> x=0.00235e2;x
[1] 0.235
.
R :
+ (), (), (), / (), ( ), %/% ( ), %% ( ). , .. ,
, .
.
Enter :
> 2*(7-9)^8+6/3
[1] 514
514.
, , ,
:
35
> +(2,3)
[1] 5
> /(9,2)
[1] 4.5
.
:
exp(x) ex ;
exp1m(x) |x| 1 ex 1.
R :
log(x) logb(x) x;
log10(x) ;
log2(x) 2;
log1p(x) ln (1 + x) |x| 1 ( x 1
);
log(x, base=) logb(x, base=) .
> x=exp(2);x
[1] 7.389056
> log(x)
[1] 2
> logb(x)
[1] 2
> log10(100)
[1] 2
> log(8)
[1] 2.079442
> log1p(0.01)
[1] 0.00995033
> log1p(-0.99999)
[1] -11.51293
> log(125,base=5)
[1] 3
> logb(256,base=16)
[1] 2
36
.
ceiling(x) , x (
+1);
floor(x) , x ( 1);
trunc(x) ( 0);
round(x) ;
round(x, dig) round(x), dig ,
;
signif(x, digits = 6) (digits)
(.. , ), digits
6.
> x=1.5555
> ceiling(x)
[1] 2
> floor(x)
[1] 1
> trunc(x)
[1] 1
> y=-1.5
> ceiling(y)
[1] -1
> floor(y)
[1] -2
> trunc(y)
[1] -1
> x=1/33;x
[1] 0.03030303
> round(x)
[1] 0
> round(x,4)
[1] 0.0303
> signif(x,5)
[1] 0.030303
37
abs(x) x, ( ), .
> x=-5; y=F;
> abs(x)
[1] 5
> abs(y)
[1] 0
> z=T;abs(z)
[1] 1
> z=5-1i*2
> abs(z)
[1] 5.385165
sqrt(x) x.
> x=4;sqrt(x)
[1] 2
> y=-4;sqrt(y)
[1] NaN
In sqrt(y) : NaN
x, x
:
> y=as.complex(y); sqrt(y)
[1] 0+2i
: - , , -, -,
.
gamma(x) - (x), (x) =
tx1 et dt, x -
, .
lgamma(x) .
38
digamma(x) .
trigamma(x) .
psigamma(x, deriv) deriv- -
( -) ( deriv=0, ..
psigamma(x)=digamma(x)).
> x=-8.9
> gamma(x)
[1] -3.507258e-05
> x=2.15
> gamma(x)
[1] 1.072997
> digamma(x)
[1] 0.5152385
> psigamma(x)
[1] 0.5152385
> trigamma(x)
[1] 0.5894157
beta(a, b) - B(a, b)
a b,
Z1
(a)(b)
B(a, b) = ta1 (1 t)b1 dt =
,
(a + b)
0
a b .
lbeta(a, b) - B(a, b).
> a=2;b=3
> beta(a,b)
[1] 0.08333333
> lbeta(a,b)
[1] -2.484907
choose(n, k) Cnk n k, n
, k 1 .
1
k < 0 , k
39
lchoose(n, k) Cnk .
factorial(x) x, x 0.
lfactorial(x) x.
> n=5.4;k=3;x=4.6;x1=4
> choose(n,k)
[1] 13.464
> choose(round(n),k)
[1] 10
> factorial(x)
[1] 61.55392
> factorial(x1)
[1] 24
3.2.3
R :
sin(x) sin (x), x ( /6);
cos(x) cos (x);
tan(x) tg(x).
:
asin(x) arcsin(x);
acos(x) arccos(x);
atan(x) arctg(x).
> sin(pi/2)
[1] 1
pi . , , (). , atan2(y,x)
Ox (x; y).
:
cosh(x) x;
acosh(x) x;
40
sinh(x) x;
asinh(x) x;
tanh(x) x;
atanh(x) x.
3.2.4
R ,
, , , :
> x=5+1i*5
> y=4-1i*5
> x+y
[1] 9+0i
> x-y
[1] 1+10i
> x*y
[1] 45-5i
> x/y
[1] -0.121951+1.097561i
> x^y
[1] 118961-44137.3i
:
Re(z) z;
Im(z) z;
p
Mod(z) = x2 + y 2 z = x + iy;
Arg(z) z = x + iy, x = cos ,
y = sin ;
Conj(z) z .
41
> x=2+1i*2;y=2-1i*3
> Re(x)
[1] 2
> Im(y)
[1] -3
> Mod(x)
[1] 2.828427
> Arg(x)
[1] 0.7853982
> Conj(x)
[1] 2-2i
> cos(x)
[1] -1.565626-3.297895i
2 .
5.2.3 5
42
4
.
R
, R ,
,
.
4.1
,
, .
4.1.1
if
if, R :
( ) (
).
if
if()
(<, >, >=, <=, ==, ! =),
.
TRUE, .
43
,
. ,
{} (
)1 .
(NULL).
> x=5;y=4
> if(x>y) {z=x+y;z}
[1] 9
z.
> x=5;y=4
> if(x<y) w=x+y
> w
: w
, ,
w .
7. , x y (10). : x y,
x/y. ,
.
?
> x=1:10; y=10;1
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y
[1] 10 9 8 7 6 5 4 3 2 1
> if (x<y) {x/y}
[1] 0.1000000 0.2222222 0.3750000 0.5714286
[7] 1.7500000 2.6666667 4.5000000 10.0000000
0.8333333
1.2000000
, .
,
In if (x != y) { :
> 1,
, .
1
44
if
if() 1 else 2
, 1 ( ), 2 ( )2 .
> x=5;y=4
> if(x<y) {x+y} else {x-y}
[1] 1
4.1.2
ifelse
In sqrt(x) : NaN
2
. 9
45
NaN . ,
.
> sqrt(ifelse(x >= 0, x, NA))
[1] 2.449490 2.236068 2.000000 1.732051 1.414214 1.000000 0.000000
[8]
NA
NA
NA NA
:
>
>
>
>
x=rep(c(1,2,3),3)
x=matrix(x,3,3)
z=cos(ifelse(1<x,x*pi,x*pi/2))
z
[,1]
[,2]
[,3]
[1,] 6.123032e-17 6.123032e-17 6.123032e-17
[2,] 1.000000e+00 1.000000e+00 1.000000e+00
[3,] -1.000000e+00 -1.000000e+00 -1.000000e+00
4.1.3
for
for( in )
. , ( ).
9. 7.
> x=1:10; y=10:1
> x
[1] 1 2 3 4 5 6 7 8 9 10
> y
[1] 10 9 8 7 6 5 4 3 2 1
> w=vector(length=10,mode=numeric)
> for(i in 1:10)
+ { if (x[i]<y[i]) {w[i]=x[i]/y[i]}
+ else {w[i]=x[i]*y[i]}
+ }
> w
[1] 0.1000000 0.2222222 0.3750000 0.5714286
[7] 28.0000000 24.0000000 18.0000000 10.0000000
46
0.8333333 30.0000000
, ,
w x/y,
w = xy. , w,
.
. x y 7,
w .
> for(i in 1:10)
+ { if (x[i]<y[i]) {w=x[i]/y[i]}
+ else {w=x[i]*y[i]}
+ }
> w
[1] 10
w ,
.
- , , , .
c=integer(0)
for (i in c) {m=i}
m
: m
4.1.4
while
.
while()
,
FALSE, .
> x=-10
> while (x<0) {z=x; x=x+1}
> z
[1] -1
4.1.5
repeat
repeat
47
, ( ). break,
.
10. repeat,
if break . repeat t.
> t=-10
> repeat{
+ if (t>0) break
+ f=log(abs(t))
+ t=t+1}
> f
[1] -Inf
t 0 break
f = ln |t|, f = ln 0 = .
next
break . break
, ,
.
4.1.6
switch
switch( , )
.
:
( 1 ),
;
(), .
48
, , switch
NULL.
switch ,
, .
11. x 5, i cos , e,
log2 (4), lg10 (0.01), , , ,
TRUE.
> x=numeric(5)
> for (i in 1:5)
+ { x[i]=switch(i,cos(pi),exp(1),log2(4),log10(0.01),TRUE)}
> x
[1] -1.000000 2.718282 2.000000 -2.000000 1.000000
4.2
4.2.1
R
R
R . :
function() { }
:
function , R ,
;
(
), . : (.. , , x y), = (, x=TRUE),
. . . .
( )
( {}), ,
. ( ;).
.
49
(1, 2, . . . ).
1, 2, . . . ,
, .
12. ,
x y.
> norm = function(x,y) sqrt(x%*%y)
> norm(1:4,2:5)
[,1]
[1,] 6.324555
= , . ,
, ,
. , f un,
:
> fun = function(arg1, arg2, arg3, arg4)
{
#
}
fun , :
> ans = fun(d, f, 20, TRUE)
> ans = fun(d, f, arg4 = TRUE, arg3 = 20)
> ans = fun(arg1 = d, arg3 = 20, arg2 = f, arg4 = TRUE)
.
.
. , f un
, .
> fun(d, f, arg4 = FALSE)
.
, , .
, . :
,
(.. , );
,
;
(
..).
,
.
4.2.2
. . .
.
. . . , ,
.
13. , , .
fun= function (...) {
data = list(...)
n =length(data)
maxs = numeric(n)
51
mins = numeric(n)
means<- numeric(n)
for (i in 1:n) {
maxs[i] = max(data[[i]])
mins[i]<-min(data[[i]])
means[i]<-mean(data[[i]])
}
print(maxs)
print(mins)
print(means)
invisible(NULL)
}
,
x=rnorm(100)
y=rnorm(200)
z=rnorm(300)
, .
>fun(x,y,z)
[1] 2.220520 2.429910 3.541140
[1] -2.185287 -2.436616 -3.001431
[1] -0.13654894 -0.03310537 0.07264300
,
, .
4.2.3
:
, .
( ) , (
function).
, ,
.
52
,
, .
,
> f = function(x)
{
y = 2*x
print(x)
print(y)
print(z)
}
x , y , z .
. ,
. , .
.
4.2.4
:
> = function() {
return()
}
return() () (
, ).
14. return() .
ff=function(x){
y=sum(x)
z=cumprod(x)
}
y=ff(1:10); y
53
z, .
[1]
1
2
[9] 362880 3628800
24
120
720
5040
40320
.
ff1=function(x){
y=sum(x); z=cumprod(x)
return(y)}
ff1(1:10)
[1] 55
return()
y=ff(1:10); y, .
, return().
,
? , return ,
. ,
x
y.
15. x y.
x<-c(1,9,2,8,3,7)
y<-c(9,2,8,3,7,2)
:
minmax=function (a,b) {
c1=max(a)
c2=max(b)
d1=min(a,b)
d2=min(b)
answer<-list(c1,c2,d1,d2)
names(answer)[[1]]<-" x"
names(answer)[[2]]<-" y"
names(answer)[[3]]<-" x"
names(answer)[[4]]<-" y"
return(answer) }
54
minmax(x,y)
$ x
[1] 9
$ y
[1] 9
$ x
[1] 1
$ y
[1] 2
4.2.5
,
, .
<< :
arg1 <<-
16. 3 .
y=5;z=6
ff=function(x){
y=sum(x); z<<- sqrt(y)
return(y)}
ff(1:10)
y
z
> ff(1:10)
[1] 55
y z:
> y
[1] 5
> z
[1] 7.416198
3
>,
55
y, .
y . z, , ,
.
4.2.6
apply(), sapply() lapply() ,
.
(function(x,y){ z <- 2*x^2 + y^2; x+y+z })(0:7, 1)
[1]
2
5 12 23 38 57 80 107
, .
apply()
apply() , ( ).
:
apply(X, , , ...)
:
;
, : 1 , 2
, (1,2) ( ,
);
, +, - .., .
.
17. X.
56
> (X=matrix(1:25,nrow=5))
[,1] [,2] [,3] [,4] [,5]
[1,]
1
6
11
16
21
[2,]
2
7
12
17
22
[3,]
3
8
13
18
23
[4,]
4
9
14
19
24
[5,]
5
10
15
20
25
apply(X,1,sum)
[1] 55 60 65 70 75
> apply(X,2,sum)
[1] 15 40 65 90 115
, .
.
apply(X,1,sqrt)
[,1]
[,2]
[1,] 1.000000 1.414214
[2,] 2.449490 2.645751
[3,] 3.316625 3.464102
[4,] 4.000000 4.123106
[5,] 4.582576 4.690416
[,3]
1.732051
2.828427
3.605551
4.242641
4.795832
[,4]
2.000000
3.000000
3.741657
4.358899
4.898979
[,5]
2.236068
3.162278
3.872983
4.472136
5.000000
.
:
> apply(X,1,function(x) x^2-x)
[,1] [,2] [,3] [,4] [,5]
[1,]
0
2
6
12
20
[2,]
30
42
56
72
90
[3,] 110 132 156 182 210
[4,] 240 272 306 342 380
[5,] 420 462 506 552 600
: , ..
57
sapply()
sapply() apply(), . ,
(
4.2.7).
sapply(X, , ..., simplify = TRUE, USE.NAMES = TRUE)
, ;
X ;
simplify :
( TRUE );
USE.NAMES : TRUE ( ) ,
.
sapply() ,
.
18. ,
.
x <- list(a = 1:10, beta = exp(-3:3), logic = c(TRUE,FALSE,FALSE,TRUE))
x
$a
[1] 1 2 3 4 5 6 7 8 9 10
$beta
[1] 0.04978707 0.13533528 0.36787944 1.00000000 2.71828183
[6] 7.38905610 20.08553692
$logic
[1] TRUE FALSE FALSE TRUE
sapply() .
> sapply(x, quantile)
a
beta logic
0\%
1.00 0.04978707
0.0
25\%
3.25 0.25160736
0.0
50\%
5.50 1.00000000
0.5
75\%
7.75 5.05366896
1.0
100\% 10.00 20.08553692
1.0
58
lapply()
lapply(X, , ...)
sapply(), :
sapply(X, , ..., simplify = FALSE, USE.NAMES = FALSE)
lapply() ,
( , X
numeric).
19. ,
.
a=c("a","b","c","d")
b=c(1,2,3,4,4,3,2,1)
c=c(T,T,F)
X=list(a,b,c)
,
.
class(X)
[1] "list"
X
[[1]]
[1] "a" "b" "c" "d"
[[2]]
[1] 1 2 3 4 4 3 2 1
[[3]]
[1] TRUE TRUE FALSE
lapply()
> lapply(X,length)
[[1]]
[1] 4
[[2]]
[1] 8
[[3]]
[1] 3
59
> lapply(X,mean)
[[1]]
[1] NA
[[2]]
[1] 2.5
[[3]]
[1] 0.6666667
In mean.default(X[[1L]], ...) :
: NA
,
.
4.2.7
, x (x!), .
if for:
fac1=function(x){
f=1
if (x<2) return(1)
for (i in 2:x) f = f*i
f}
:
> sapply(0:5,fac1)
[1]
1
1
2
6
24 120
while:
fac2=function(x) {
f = 1
t = x
while(t>1) {
f = f*t
t = t-1 }
return(f) }
60
while,
(t ) (t =
t 1). 0
5,
> sapply(0:5,fac2)
[1]
1
1
2
6
24 120
, repeat:
fac3=function(x) {
f = 1
t = x
repeat {
if (t<2) break
f = f*t
t = t-1 }
return(f) }
repeat
break.
:
> sapply(0:5,fac3)
[1]
1
1
2
6
24 120
, . cumprod()4 .
fac4=function(x) max(cumprod(1:x))
max() , ,
(0! = 1). fac4 .
sapply(0:5,fac4)
[1] 1 1 2 6 24 120
. 5.2.3 5
61
5
R
5.1
(, ) :
( vector) , . ,
.
(matrix) , .
(array) ,
.
(factor) ,
, .
(list) , ; ,
.
(data.frame) , R.
5.2
5.2.1
R :
62
(, , , ) , , ;
.
vector(mode = " ", )
( numeric, logical, complex, character), , - .
> vector(numeric,10)
[1] 0 0 0 0 0 0 0 0 0 0
> vector(complex,10)
[1] 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i 0+0i
> vector(logical,10)
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> vector(character,10)
[1] "" "" "" "" "" "" "" "" "" ""
(
FALSE, ).
vector(10)
.
vector() numeric(), logical(),
complex(), character(), 2.4.1 2.
.
c().
.
> x=c(3,5,-2,4);x
[1] 3 5 -2 4
> y=c(T,F,T,T);y
[1] TRUE FALSE TRUE TRUE
> z=c(a,b,ab,abc);z
[1] "a"
"b"
"ab" "abc"
63
c() ,
:
; , ; .
> x=c(2,3,-2,T,F,T);x
[1] 2 3 -2 1 0 1
> y=c(4,-6,2.8,T,a,F,3,abc);y
[1] "4"
"-6"
"2.8"
"TRUE"
"a"
"FALSE" "3"
"abc"
c() :
> x=c(1,-3.2,2);x
[1] 1.0 -3.2 2.0
> y=c(-0.6,pi,Inf,5);y
[1] -0.600000 3.141593
Inf
> z=c(2,x,-1,y);z
[1] 2.000000 1.000000 -3.200000
[8]
Inf 5.000000
5.000000
2.000000 -1.000000 -0.600000
3.141593
, , . scan()1 .
20. y scan().
> y=scan()
Enter.
1:
(), .
,
1: 2 34
Enter
3: 13 5 6 7
7:
Enter .
.
1
,
6.
64
Read 6 items
,
:
> y
[1]
2 34 13
, scan()
.
: is.vector()
as.vector(). ,
.
21. y,
, z ( 5.6 ).
> y=c(T, F, 1, -3, 2,1+1i*2);y
[1] 1+0i 0+0i 1+0i -3+0i 2+0i 1+2i
> z=factor(y)
> z=factor(y);z
[1] 1+0i 0+0i 1+0i -3+0i 2+0i 1+2i
Levels: -3+0i 0+0i 1+0i 1+2i 2+0i
.
> is.vector(y)
[1] TRUE
> is.vector(z)
[1] FALSE
as.vector(), .
22. 21 .
> is.vector(z)
[1] FALSE
> w=as.vector(z)
, w
> is.vector(w)
[1] TRUE
>w
[1] "1+0i"
"0+0i"
"1+0i"
"-3+0i" "2+0i"
"1+2i"
, ,
( 5.6).
65
5.2.2
2.4.1 R
,
character().
y=character(10);y
[1] "" "" "" "" "" "" "" "" "" ""
> for (i in 1:length(y)) y[i]=letters[i]
> y
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
, .
> x=a;x
[1] "a"
> y= ;y
[1] " "
c().
> _=c(,,,)
> _
[1] "" "" "" ""
character(n), n.
> character(10)
[1] "" "" "" "" "" "" "" "" "" ""
(.
5.2.6 ) (. 4).
, ,
letters LETTERS.
> letters
[1] "a" "b"
[20] "t" "u"
> LETTERS
[1] "A" "B"
[20] "T" "U"
"c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
"v" "w" "x" "y" "z"
"C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
"V" "W" "X" "Y" "Z"
66
paste()
() paste(). , , .
paste("", "", "", "")
[1] " "
paste() , . , ,
.
> paste(c(,,,),c(- ),sep=)
[1] "- " "- " "- " "- "
, :
> paste("x", 1:5)
> [1] "x 1" "x 2" "x 3" "x 4" "x 5"
:
> paste("x", 1:5, sep = "")
> [1] "x1" "x2" "x3" "x4" "x5"
substr(x, start, stop) ()
. :
x ;
start x, ;
stop x, .
23. .
phrase=
q=character(26)
for (i in 1:26) q[i]=substr(phrase,1,i)
q
67
> q
[1]
[3]
[5]
[7]
[9]
[11]
[13]
[15]
[17]
[19]
[21]
[23]
[25]
""
""
""
" "
" "
" "
" "
" "
" "
" "
" "
" "
" "
""
""
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
strsplit(x, split=character(0)) x .
phrase=
strsplit(phrase,split=character(0))
[[1]]
[1] "" "" "" "" "" " " "" "" "" "" "" " " "" " " "" "" "" "" ""
[20] "" " " "" "" "" "" ""
split (),
.
strsplit(phrase,split= )
[[1]]
[1] "" "" ""
"" ""
.
, . nchar().
> nchar(phrase)
[1] 26
> nchar(q)
[1] 1 2 3 4
[26] 26
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
68
5.2.3
5.2.1 R
c().
c() .
() . (. . ) R 1.
, c() , .
,
> c(c(1, 2, 3, 4, 5), 6, c(7, 8))
[1] 1 2 3 4 5 6 7 8
, , -:-. , 1:5.
seq(),
. ,
:
> seq(0, 1, by = 0.1)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
:
> seq(0, 1, len = 11)
[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
, , .
rep(v, k) , k v,
k :
> rep(c(1, 2), 3)
[1] 1 2 1 2 1 2
, k = (k1 , k2 , . . . , kn ) , v k1 , k2 , . . . , kn .
> x=rep(c(1,2,3,4,5),c(1,2,3,4,5));x
[1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
, ,
R 2 .
Inf, NaN, NA. ,
2
69
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3 4 5
> x1*2
[1] 20 -6 12 4 -8 NA 2 4 6 8 10
> x1/4
[1] 2.50 -0.75 1.50 0.50 -1.00
NA
70
0.25
0.50
0.75
1.00
1.25
> x1+5
[1] 15 2 11
> x1-2
[1] 8 -5 4
1 NA
9 10
0 -6 NA -1
.
> x = c(0, pi/2, pi)
> sin(x)
[1] 0.000000e+00 1.000000e+00 1.224606e-16
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3 4 5
> sqrt(x1)
[1] 3.162278
NaN 2.449490 1.414214 NaN
[9] 1.732051 2.000000 2.236068
In sqrt(x1) : NaN
NA
1.000000
1.414214
R , :
length() .
> x=c(1:5,NA,NaN,6:10);x
[1]
1
2
3
4
5
> length(x)
[1] 12
NA NaN
10
NA NaN
71
10
> max(x)
[1] NaN
> min(x,na.rm=T)
[1] 1
> max(x,na.rm=T)
[1] 10
pmax() pmin() () . , () , . .. pmax(x, y) =
(max(x1 , y1 ), max(x2 , y) , . . .), pmin(x, y) = (min(x1 , y1 ), min(x2 , y) , . . .). - , .
> x=rpois(10,1);x
[1] 2 0 1 3 1 1 1 1 0 2
> y=rpois(6,1);y
[1] 0 4 2 1 0 0
> pmax(x,y)
[1] 2 4 2 3 1 1 1 4 2 2
> pmin(x,y)
[1] 0 0 1 1 0 0 0 1 0 1
NA (NaN),
NA (NaN).
NA (NaN),
na.rm=TRUE,
> x=c(NA, NaN,NaN)
> y=c(NaN,NA,NaN)
> pmin(x,y,na.rm=T)
[1] NaN NA NaN
> pmax(x,y,na.rm=T)
[1] NaN NA NaN
mean()3 .
, NA (NaN),
NA (NaN). , mean(x,na.rm = TRUE).
3
72
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3
> mean(x1)
[1] NA
> mean(x1,na.rm=T)
[1] 2.6
range() , ().
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3
> range(x1)
[1] NA NA
> range(x1,na.rm=T)
[1] -4 10
sum() . ,
NA (NaN), NA
(NaN). ,
sum(x,na.rm = TRUE).
> x=c(1:5,NA,6:10)
> sum(x)
[1] NA
> sum(x,na.rm=T)
[1] 55
prod() . NA (NaN), NA (NaN).
NA (NaN) , prod(x,na.rm=T).
> x=c(1:5,NA,NaN,6:10);x
[1]
1
2
3
4
5
> prod(x)
[1] NA
> prod(x,na.rm=T)
[1] 3628800
>
NA NaN
10
sort() , , , ( ),
sort(x,decreasing=T) ( sort(x,dec=T)). NA (NaN) .
73
x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3 4
> sort(x1)
[1] -4 -3 1 2 2 3 4 5 6 10
> sort(x1,dec=T)
[1] 10 6 5 4 3 2 2 1 -3 -4
> sort(x1,decreasing=T)
[1] 10 6 5 4 3 2 2 1 -3 -4
rev(sort()) .
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3 4
> rev(sort(x1))
[1] 10 6 5 4 3 2 2 1 -3 -4
rank()
(
random, average, first, max, min).
,
.
x=c(5:1,6,9,10,8,7);x
[1] 5 4 3 2 1 6 9 10 8 7
random=rank(x,ties=random)
average=rank(x,ties=average)
first=rank(x,ties=first)
max=rank(x,ties=max)
min=rank(x,ties=min)
rbind(x,random,average,first,max,min)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
x
5
4
3
2
1
6
9
10
8
7
random
5
4
3
2
1
6
9
10
8
7
average
5
4
3
2
1
6
9
10
8
7
first
5
4
3
2
1
6
9
10
8
7
max
5
4
3
2
1
6
9
10
8
7
min
5
4
3
2
1
6
9
10
8
7
,
.
74
x
random
average
first
max
min
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
6.0
11 10.0
9
13
12
8
7 10.0
6.0
2.0
8 7.0
5
10
9
4
3 6.0
1.0
1.5
8 6.5
5
10
9
4
3 6.5
1.5
1.0
8 6.0
5
10
9
4
3 7.0
2.0
2.0
8 7.0
5
10
9
4
3 7.0
2.0
1.0
8 6.0
5
10
9
4
3 6.0
1.0
random ; average
; first ; max
; min .
na.last rank() NA.
>
>
>
>
>
last=rank(x,na.last=T)
fierst=rank(x,na.last=F)
Na=rank(x,na.last=NA)
keep=rank(x,na.last=keep)
rbind(x,last,first,keep)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
x
8 11.0
13
NA 7.0 11.0
10
NA
14
7.0
last
3 5.5
7
9 1.5 5.5
4
10
8
1.5
first
5 7.5
9
1 3.5 7.5
6
2
10
3.5
keep
3 5.5
7
NA 1.5 5.5
4
NA
8
1.5
> Na
[1] 3.0 5.5 7.0 1.5 5.5 4.0 8.0 1.5
na.last=T NA ; na.last=F NA
; na.last=NA NA ; na.last=keep ,
NA, NA.
match(x,y) , y x.
> x
[1] 8 11 13 9 7 11 10 12 14
> y
[1] 9 10 9 10 9
75
> match(x,y)
[1] NA NA NA
> match(y,x)
[1] 4 7 4 7 4
1 NA NA
2 NA NA NA
()
R :
cumsum(x) x (
);
cumprod(x) ;
cummax(x) (
x);
cummin(x) (
).
> x=1:10
> cumsum(x)
[1] 1 3 6 10 15 21 28 36 45 55
> y=-5:5
> cumprod(y)
[1]
-5
20 -60 120 -120
0
> x=-5:4
> cummax(x)
[1] -5 -4 -3 -2 -1 0 1 2 3 4
> cummin(x)
[1] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
5.2.4
R (, , ), TRUE FALSE,
NA. 4 .
.
, , , .
4
3.2.1 3
76
5.2.5
. names( ).
26. , (-201, -201, -202, -201, -203).
x.
> group=c(17,19,25,13,7)
77
, , , .
,
.
names(group)=c(-201,-202,-203,-201,-201)
> group
-201 -202 -203 -201 -201
17
19
25
13
7
, :
> group[2]
-202
19
> group[-203]
-203
25
> group[-203]=18
> group
-201 -202 -203 -201 -201
17
19
18
13
7
5.2.6
[i], i
. , u[5] 5- u.
1. u[i]
.
i, u[i] . u i, u[i] ,
NA.
> u <- 1
> u[5] <- 5
> u
[1] 1 NA NA NA 5
78
u[-i] , i- u.
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3
> length(x1)
[1] 11
> x1[-6]
[1] 10 -3 6 2 -4 1 2 3 4
u[] ,
u.
> x1[]
[1] 10 -3
2 -4 NA
v (, , ). , v[i]. x
, . , v,
.
?
:
x . ,
x v . x,
v, x v . v, v
x NA. v[x] v,
- x TRUE.
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3
> x2=c(T,F,T,F,F)
> y=x1[x2];y
[1] 10 6 NA 2 5
> x3=rep(c(T,F),10);x3
79
TRUE FALSE
TRUE FALSE
3
TRUE FALSE
TRUE FALSE
TRUE FALSE
TRUE FALSE
TRUE FALSE
TRUE FALSE
5 NA NA NA NA
x . x , v[x] , v ,
- x.
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3
> x=c(1,3,5,15)
> y=x1[x];y
[1] 10 6 -4 NA
x . i , v.
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3
> x=c(-1,-3,-5)
> y=x1[x];y
[1] -3 2 NA 1 2 3 4 5
> y=x1[-(2:6)];y
[1] 10 1 2 3 4 5
x . v. , v[x] , v ,
x.
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3 4 5
> names(x1)=letters[1:length(x1)];x1
a b c d e f g h i j k
10 -3 6 2 -4 NA 1 2 3 4 5
>
> y=x1[c(a,c,f)];y
a c f
10 6 NA
> y=x1[letters[3:8]];y
80
c d e f g h
6 2 -4 NA 1 2
> y=x1[c(g,a,j,d)];y
g a j d
1 10 4 2
, 5 .
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3
> y1=x1[x1<5];y1
[1] -3 2 -4 NA 1 2 3 4
> y2=x1[(-4<x1)&(x1<3)];y2
[1] -3 2 NA 1 2
> y3=x1[(x1<=-2)|(x1>3)];y3
[1] 10 -3 6 -4 NA 4 5
> y4=x1[!((-3<=x1)&(x1<3))];y4
[1] 10 6 -4 NA 3 4 5
> y5=x1[!is.na(x1)];y5
[1] 10 -3 6 2 -4 1
y5 x1,
NA NaN.
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3 4 5
> x1[8]=NaN;x1
[1] 10 -3
6
2 -4 NA
1 NaN
> y6=x1[!is.nan(x1)];y6
[1] 10 -3 6 2 -4 NA 1 3 4 5
3.2.1 3
81
> x1=c(10,-3,6,2,-4,NA,1:5);x1
[1] 10 -3 6 2 -4 NA 1 2 3 4 5
> z=(x1 + 1)[(!is.na(x1)) & x1 > 0];z
[1] 11 7 3 2 3 4 5 6
z x1 + 1 (
1 1), .
5.2.7
() . which(),
.
27. x.
> x
[1] 10 4 7
> length(x)
[1] 15
6 14 10 10
8 11 11 20
x, 10.
> which(x>10)
[1] 8 13 14 15
, x , , , 10,
> x[13]
[1] 11
> y=which(x>10)
> x[y[2]]
[1] 11
.
28. x , 5.
> x
[1] 10 4 7 9 7 8
> y=which(x\%\%5==0);y
[1] 1 9 10 11 15
> x[y[2]]
[1] 10
6 14 10 10
82
8 11 11 20
which() which.max()
which.min(), , , .
> which.max(x)
[1] 15
> which.min(x)
[1] 2
which() , .
29. x 27
, 12
> y=which(abs(x-12)==min(abs(x-12)));y
[1] 13 14
.
> x[y]
[1] 11 11
duplicated() . , i- (),
(.. ).
duplicated(x, incomparables = FALSE, fromLast = FALSE, ...)
:
x .
incomparables , .
FALSE , .
fromLast
, .
... .
- ,
. : TRUE ,
FALSE .
83
5.3
5.3.1
TRUE FALSE
matrix()
matrix(x, nrow, ncol, byrow, dimnames)
,
nrow = m / ncol = n ( , 1);
(
).
84
TRUE
. dimnames , , (
).
> matrix(1:6, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,]
1
3
5
[2,]
2
4
6
> matrix(1:6, nrow = 2, ncol = 3, byrow=T)
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
> matrix(1:6, nrow = 2, ncol = 3, byrow=T,list(c(1,2),c(A,B,C)))
A B C
1 1 2 3
2 4 5 6
>
, ,
matrix(1:2, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,]
1
1
1
[2,]
2
2
2
matrix(1, nrow = 2, ncol = 3)
[,1] [,2] [,3]
[1,]
1
1
1
[2,]
1
1
1
:
> matrix(1:12, nrow = 5, ncol = 3)
[,1] [,2] [,3]
[1,]
1
6
11
[2,]
2
7
12
[3,]
3
8
1
[4,]
4
9
2
[5,]
5
10
3
(,
), , .
.
> matrix(1:12, ncol = 3)
[,1] [,2] [,3]
[1,]
1
5
9
[2,]
2
6
10
[3,]
3
7
11
[4,]
4
8
12
() ,
( ).
.
> A=matrix(1:12, ncol = 5);A
In matrix(1:12, ncol = 5) :
[12] [5]
[,1] [,2] [,3] [,4] [,5]
[1,]
1
4
7
10
1
[2,]
2
5
8
11
2
[3,]
3
6
9
12
3
> B=matrix(1:12, nrow = 5);B
In matrix(1:12, nrow = 5) :
[12] [5]
[,1] [,2] [,3]
[1,]
1
6
11
[2,]
2
7
12
[3,]
3
8
1
[4,]
4
9
2
[5,]
5
10
3
nrow(A), ncol(A) dim(A) ,
A .
> nrow(A)
[1] 3
> ncol(B)
[1] 3
86
> dim(A)
[1] 3 5
cbind(A, B)
> = cbind(A, B)
(), A () B ( A B ).
> A=matrix(1:12,nrow=3);A
[,1] [,2] [,3] [,4]
[1,]
1
4
7
10
[2,]
2
5
8
11
[3,]
3
6
9
12
> B=matrix(13:24,nrow=3);B
[,1] [,2] [,3] [,4]
[1,]
13
16
19
22
[2,]
14
17
20
23
[3,]
15
18
21
24
> Z=cbind(A,B);Z
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]
1
4
7
10
13
16
19
22
[2,]
2
5
8
11
14
17
20
23
[3,]
3
6
9
12
15
18
21
24
rbind(A, B)
> A = rbind(A, B)
, A B ( ). ,
cbind() rbind .
> Z=rbind(A,B);Z
[,1] [,2] [,3] [,4]
[1,]
1
4
7
10
[2,]
2
5
8
11
[3,]
3
6
9
12
[4,]
13
16
19
22
[5,]
14
17
20
23
[6,]
15
18
21
24
diag(x,nrow,ncol).
87
> diag(1,3,3)
[,1] [,2] [,3]
[1,]
1
0
0
[2,]
0
1
0
[3,]
0
0
1
nrow ( ncol,
).
> diag(nrow=4)
[,1] [,2] [,3] [,4]
[1,]
1
0
0
0
[2,]
0
1
0
0
[3,]
0
0
1
0
[4,]
0
0
0
1
diag() , ,
.
> X=matrix(1:16,nrow=4);X
[,1] [,2] [,3] [,4]
[1,]
1
5
9
13
[2,]
2
6
10
14
[3,]
3
7
11
15
[4,]
4
8
12
16
> diag(X)
[1] 1 6 11 16
, , , (. ).
. ,
( fix( )).
, MS Excel.
31. A
A=matrix(1:16,nrow=4);A
[,1] [,2] [,3] [,4]
[1,]
1
5
9
13
[2,]
2
6
10
14
[3,]
3
7
11
15
[4,]
4
8
12
16
88
. 5.1:
() .
5.3.2
,
, , , , :
A = matrix(1:9, nrow = 3);A
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
B = matrix(-(1:9), ncol = 3,byrow=T);B
[,1] [,2] [,3]
[1,]
-1
-2
-3
[2,]
-4
-5
-6
[3,]
-7
-8
-9
A + B
89
[1,]
[2,]
[3,]
, , , ( , ).
, , , ,
:
> A+3
[1,]
[2,]
[3,]
> B+3
[,1]
[1,]
2
[2,]
-1
[3,]
-4
> (1:3)*A
[,1]
[1,]
1
[2,]
4
[3,]
9
> A*(1:3)
[,1]
[1,]
1
[2,]
4
[3,]
9
> (1:9)+A
[,1]
[1,]
2
[2,]
4
[3,]
6
> B^2
[,1]
[1,]
1
[2,]
16
[3,]
49
[,2] [,3]
1
0
-2
-3
-5
-6
[,2] [,3]
4
7
10
16
18
27
[,2] [,3]
4
7
10
16
18
27
[,2] [,3]
8
14
10
16
12
18
[,2] [,3]
4
9
25
36
64
81
90
,
> (0:3)*A
[,1] [,2] [,3]
[1,]
0
12
14
[2,]
2
0
24
[3,]
6
6
0
In (0:3) * A :
:
> sqrt(A)
[,1]
[1,] 1.000000
[2,] 1.414214
[3,] 1.732051
> log(abs(B))
[,1]
[1,] 0.000000
[2,] 1.386294
[3,] 1.945910
[,2]
[,3]
2.000000 2.645751
2.236068 2.828427
2.449490 3.000000
[,2]
[,3]
0.6931472 1.098612
1.6094379 1.791759
2.0794415 2.197225
outer(x, y, )
x y. ,
. x, y.
> x =1:5; x
[1] 1 2 3 4 5
> y =-2:3;y
[1] -2 -1 0 1 2 3
> outer(x, y, "*")
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]
-2
-1
0
1
2
3
[2,]
-4
-2
0
2
4
6
[3,]
-6
-3
0
3
6
9
[4,]
-8
-4
0
4
8
12
[5,] -10
-5
0
5
10
15
> outer(x, y, "^")
[,1]
[,2] [,3] [,4] [,5] [,6]
[1,] 1.0000000 1.0000000
1
1
1
1
91
[2,]
[3,]
[4,]
[5,]
0.2500000
0.1111111
0.0625000
0.0400000
0.5000000
0.3333333
0.2500000
0.2000000
1
1
1
1
2
3
4
5
4
9
16
25
8
27
64
125
92
,
(., , )
backsolve (A, b) forwardsolve(B b), A B
, b .
> A = matrix(c(3,0,4,4), nrow = 2);A
[,1] [,2]
[1,]
3
4
[2,]
0
4
> b=c(1,1)
> backsolve(A,b)
[1] 0.00 0.25
> B= matrix(c(3,4,0,4), nrow = 2);B
[,1] [,2]
[1,]
3
0
[2,]
4
4
> forwardsolve(B,b)
[1] 0.33333333 -0.08333333
det(A) , solve(A)
:
> det(A)
[1] -4
> solve(A)
[,1] [,2]
[1,]
-1 1.00
[2,]
1 -0.75
R determinant(),
determinant(x, logarithm = TRUE, ...)
det() ( ), ,
, ( logarithm FALSE), ( logarithm = TRUE), 1
( ), 1 ( ).
> X
[,1] [,2] [,3]
93
[1,]
1
2
3
[2,]
2
1
1
[3,]
4
1
2
> determinant(X)
$modulus
[1] 1.609438
attr(,"logarithm")
[1] TRUE
$sign
[1] -1
attr(,"class")
[1] "det"
solve() ginv() (
MASS).
> X=matrix(c(1,2,4,2,1,1,3,1,2),nrow=3);X
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
2
1
1
[3,]
4
1
2
> solve(X)
[,1] [,2] [,3]
[1,] -2.000000e-01 0.2 0.2
[2,] -3.172066e-17 2.0 -1.0
[3,] 4.000000e-01 -1.4 0.6
> library(MASS)
> ginv(X)
[,1] [,2] [,3]
[1,] -2.000000e-01 0.2 0.2
[2,] -2.224918e-16 2.0 -1.0
[3,] 4.000000e-01 -1.4 0.6
. :
colSums(X, na.rm) ;
rowSums(X, na.rm) ;
colMeans(X na.rm) ;
rowMeans(X na.rm) .
94
: X (), na.rm
, NA (
na.rm=FALSE).
> A=matrix(1:12,nrow=3);A
[,1] [,2] [,3] [,4]
[1,]
1
4
7
10
[2,]
2
5
8
11
[3,]
3
6
9
12
> colSums(A)
[1] 6 15 24 33
> rowSums(A)
[1] 22 26 30
> colMeans(A)
[1] 2 5 8 11
> rowMeans(A)
[1] 5.5 6.5 7.5
6
eigen(X, symmetric, only.values = FALSE)
X . symmetric , TRUE, , X ( ), .
. symmetric ,
. only.values ,
.
> X
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
2
1
1
[3,]
4
1
2
> eigen(X)
$values
[1] 5.892488 -2.266818 0.374330
$vectors
[,1]
[,2]
[,3]
6
, A i |A I| = 0, x ,
Ax = i x
95
5.3.3
. A[i, j]
i- j- A. i j
.
i j , A[i,j] A, ,
i j.
i j , A[i,j] , .
i j . A[i,j] ,
, i j
TRUE.
i j , .. .
, .
A[i, ] A[i, 1:ncol(A)].
A[, j] A[1:nrow(A), j].
96
:
[5] 5- , , .
i , A[i]
. .
>
>
>
>
A=matrix(1:12,nrow=3)
rownames(A)=letters[1:nrow(A)]
colnames(A)=LETTERS[1:ncol(A)]
A
A B C D
a 1 4 7 10
b 2 5 8 11
c 3 6 9 12
>
,
.
> A[a,C]
[1] 7
> A[2,B]
[1] 5
> A[,B]
a b c
4 5 6
> A[b,]
A B C D
2 5 8 11
5.4
.
. . R
. array(X, ). .
32. 3 5 4
97
:
>
>
>
>
>
,
A
B
C
,
A
B
dim1 =c(A, B, C)
dim2 =c(X1, X2, X3, X4, X5)
dim3 = c(,,,)
dimnames(A) = list(dim1, dim2, dim3)
A
,
X1 X2 X3 X4 X5
1 4 7 10 13
2 5 8 11 14
3 6 9 12 15
,
X1 X2 X3 X4 X5
16 19 22 25 28
17 20 23 26 29
98
C 18 21 24
, ,
X1 X2 X3
A 31 34 37
B 32 35 38
C 33 36 39
, ,
X1 X2 X3
A 46 49 52
B 47 50 53
C 48 51 54
27 30
X4
40
41
42
X5
43
44
45
X4
55
56
57
X5
58
59
60
, , .
.
> A[,,""]
X1 X2 X3 X4 X5
A 46 49 52 55 58
B 47 50 53 56 59
C 48 51 54 57 60
> A[,X2,""]
A B C
4 5 6
, , , , .
5.5
R , .
() , .
.
() ( NA), ( ),
( ).
writer=list("", "", "1564", "1616", T, "",
" ",3," ", "", "")
.
>writer
[[1]]
[1] ""
[[2]]
[1] ""
[[3]]
[1] "1564"
[[4]]
[1] "1616"
[[5]]
[1] TRUE
[[6]]
[1] ""
[[7]]
[1] " "
[[8]]
[1] 3
[[9]]
[1] " "
[[10]]
[1] ""
[[11]]
[1] ""
vector("list ). . list()
.
is.list(), as.list().
. .
> writer[[1]]
[1] ""
> writer[[9]]
[1] " "
100
:
writer[[12]]=
writer
[[1]]
[1] ""
[[2]]
[1] ""
[[3]]
[1] "1564"
[[4]]
[1] "1616"
[[5]]
[1] TRUE
[[6]]
[1] ""
[[7]]
[1] " "
[[8]]
[1] 3
[[9]]
[1] " "
[[10]]
[1] ""
[[11]]
[1] ""
[[12]]
[1] ""
, :
> names(writer)<-c("",""," "," ",
" ",""," "," ",
"1","2","3")
writer
$
[1] ""
$
[1] ""
$
[1] "1564"
$
101
[1] "1616"
$
[1] TRUE
$
[1] ""
$
[1] " "
$
[1] 3
$1
[1] " "
$2
[1] ""
$3
[1] ""
$<NA>
[1] ""
, ,
.
.
> names(writer)[12]=
> writer
$
[1] ""
$
[1] ""
$
[1] "1564"
$
[1] "1616"
$
[1] TRUE
$
[1] ""
$
[1] " "
$
[1] 3
$1
[1] " "
102
$2
[1] ""
$3
[1] ""
$
""
:
writer=list(="",="", ="1564",
="1616", =T, ="",
=" ", =3, 1=" ",
2="", 3="")
.
. $, .
> writer$" "
[1] " "
> writer$
[1] ""
> writer$
[1] ""
, -
,
writer$
.
writer[[]]
[1] ""
writer[[""]]
[1] ""
. :
lst[1:4] , lst;
103
lst[-(2:4)] , , ;
lst[c("name "occupation")] ,
lst .
.
:
lst[[1]] ;
lst[1] , .
,
.
, . .
.
> Pushkin <- list(name = " ", year = 1799)
> Gogol <- list(name = " ", year = 1809)
> lst <- list(Pushkin, Gogol)
> lst[[1]]$name
[1] " "
> length(lst)
[1] 2
c() :
> clst <- c(Pushkin, Gogol)
> clst
$name
[1] " "
$year
[1] 1799
$name
[1] " "
$year
[1] 1799
> length(clst)
[1] 4
104
> clst$name
[1] " "
> clst$year
[1] 1799
> clst[[3]]
[1] " "
> clst[[4]]
[1] 1809
unlist(x, recursive, use.names = TRUE) x , . recursive ,
(logical, integer, real, complex, character) (
recursive = TRUE). use.names
( use.names = TRUE ).
34. determinant()7
,
> X
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
2
1
1
[3,]
4
1
2
> Y=determinant(X);Y
$modulus
[1] 1.609438
attr(,"logarithm")
[1] TRUE
$sign
[1] -1
attr(,"class")
[1] "det"
.
> z=unlist(Y);z
modulus
sign
1.609438 -1.000000
7
. 5.3.2
105
(logical < integer < real < complex < character). , ,
.
R relist(),
. relist():
flesh , ;
skeleton , .
35. z
> z
modulus
sign
1.609438 -1.000000
. ,
.
> Z=list(c(1),c(1));Z
[[1]]
[1] 1
[[2]]
[1] 1
.
> Y=relist(z,Z);Y
[[1]]
[1] 1.609438
[[2]]
[1] -1
5.6
5.6.1
factor()
,
(). factor().
106
36. , 150
: .
opros, 150
.
> length(opros)
[1] 150
.
> factor(opros)
().
[1]
[5]
[9]
[13]
[17]
[21]
[25]
[29]
[33]
[37]
[41]
[45]
[49]
[53]
[57]
[61]
[65]
[69]
[73]
[77]
[81]
[85]
[89]
[93]
[97]
[101]
[105]
[109]
107
[113]
[117]
[121]
[125]
[129]
[133]
[137]
[141]
[145]
[149]
Levels:
()
( ) ( ), ,
factor(x,ordered=F). NA,
, . ,
addNA(x), .
ordered(x) factor(),
.
is.ordered(x) , .
37. opros 30 ordered().
> opros2=opros[10:40]
> opros2
.
[1] ""
[5] " "
[9] ""
[13] ""
[17] " "
[21] ""
[25] ""
[29] " "
""
""
" " ""
" "
" "
""
""
" " ""
" "
" "
""
""
" " ""
.
> is.ordered(opros2)
[1] FALSE
108
" "
""
" "
" "
""
" "
" "
ordered()
> opros3=ordered(opros2)
> opros3
[1]
[5]
[9]
[13]
[17]
[21]
[25]
[29]
6 Levels: < < < < ... <
.
> is.ordered(opros3)
[1] TRUE
- is.factor(), as.factor() (
as.ordered() ).
> is.factor(opros2)
[1] FALSE
> as.factor(opros2)
[1]
[5]
[9]
[13]
[17]
[21]
[25]
[29]
Levels:
, as.vector().
.
> x=rep(c(1,5,7,3,2),6);x
[1] 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2
> y=factor(x);y
[1] 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2
109
Levels: 1 2 3 5 7
> is.vector(y)
[1] FALSE
> z=as.vector(y);z
[1] "1" "5" "7" "3" "2" "1" "5" "7" "3" "2" "1" "5" "7" "3" "2" "1"
[17] "5" "7" "3" "2" "1" "5" "7" "3" "2" "1" "5" "7" "3" "2"
> is.vector(z)
[1] TRUE
gl()
gl()
(, ).
gl(n, k, length, labels, ordered = FALSE)
:
n , .
k ,
( ).
> gl(5,3)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
Levels: 1 2 3 4 5
length ( length = nk). nk, ,
, nk, nk , .
> gl(5,3,15)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5
Levels: 1 2 3 4 5
> gl(5,3,12)
[1] 1 1 1 2 2 2 3 3 3 4 4 4
Levels: 1 2 3 4 5
> gl(5,3,25)
[1] 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 1 1 1 2 2 2 3 3 3 4
Levels: 1 2 3 4 5
110
labels , n,
( labels = 1:n).
gl(5,3,15,labels=LETTERS[1:5])
[1] A A A B B B C C C D D D E E E
Levels: A B C D E
gl(5,3,15,labels=c(1,2,3,4,
5))
[1] 1 1 1 2 2 2
[7] 3 3 3 4 4 4
[13] 5 5 5
Levels: 1 2 3 4 5
ordered , ,
( FALSE).
gl(5,3,15,labels=c(1,2,3,4,
5), ordered=T)
[1] 1 1 1 2 2 2
[7] 3 3 3 4 4 4
[13] 5 5 5
Levels: 1 < 2 < 3 < 4 < 5
5.6.2
table()
,
table( ). ,
, ,
. table() , , .
38. 37 opros2
table().
> table(opros2)
opros2
5
5
5
111
. NA
() NaN, .
table(x,exclude=NULL).
39. opros2 NA NaN.
> opros2=c(opros2,rep(c(NA,NaN),c(6,9)))
> opros2
[1] ""
""
[5] " "
" "
[9] ""
" "
[13] ""
""
[17] " "
" "
[21] ""
" "
[25] ""
""
[29] " "
" "
[33] NA
NA
[37] NA
"NaN"
[41] "NaN"
"NaN"
[45] "NaN"
"NaN"
""
""
" "
""
""
" "
""
""
NA
"NaN"
"NaN"
" "
""
" "
" "
""
" "
" "
NA
NA
"NaN"
"NaN"
, NA NaN
> table(opros2)
opros2
5
> table(opros2,exclude=NULL)
opros2
NaN
9
5
5
5
<NA>
5
6
6
table()
is.table(), as.table().
40. , table()
, table().
112
> x=rep(c(1,5,7,3,2),6);x
[1] 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2
> y=factor(x);y
[1] 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2
Levels: 1 2 3 5 7
. table().
> is.table(x)
[1] FALSE
> is.table(y)
[1] FALSE
table().
> z=as.table(x);z
A B C D E F
1 5 7 3 2 1
A1 B1 C1 D1
5 7 3 2
> is.table(z)
[1] TRUE
G
5
H
7
I
3
J
2
K
1
L
5
M
7
N
3
O
2
P
1
Q
5
R
7
S
3
T
2
U
1
V
5
W
7
y.
> z=as.table(y);z
as.table.default(y) :
> is.table(z)
[1] FALSE
, (table()) . ( ,
), .
:
> as.vector(z)
[1] 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2 1 5 7 3 2
, ( ) .
113
X
3
Y
2
Z
1
5.7
( ) (data frames)
R, .
, ,
( ),
( ). , .
data.frame():
> frm <- data.frame(data1, data2, ...)
, . (data1, data2, . . . ) (, ), ,
(- , ), .
, ( ) . ,
, . . data.frame
. .
, .
41. .
Y = matrix(c(1988,1987,1989,1989,2005,2005,
2006, 2005), nrow = 4)
rownames(Y) = c("", "", "", "")
colnames(Y) = c(" ", " ")
:
> Y
1988
2005
1987
2005
1989
2006
1989
2005
.
114
1985
2002 FALSE 2
1987
2004 TRUE 1
1986
2003 FALSE 3
1984
2001 FALSE 2
colnames(Y1)[3] = ""
colnames(Y1)[4] = ""
Y1
1985
2002
FALSE
2
1987
2004
TRUE
1
1986
2003
FALSE
3
1984
2001
FALSE
2
data.frame() ,
, .
> lst = list(1:4,FALSE, c("A", "B"))
> data.frame(lst)
X1.4 FALSE. c..A....B..
1
1 FALSE
A
2
2 FALSE
B
3
3 FALSE
A
4
4 FALSE
B
: .
. , , (. .
), .
42. 41.
Y1["", ""]
115
[1] 2
Y1["",]
. .
1989
2005
FALSE
2
Y1[4, 3]
[1] FALSE
Y1[,c(2,4)]
.
2005
2
2005
1
2006
3
2005
2
Y1$
[1] 2 1 3 2
Y1[[""]][3]
[1] FALSE
Y1[[""]][2]
[1] TRUE
(. 5.3.1
).
116
6
R
R , , .
( /
).
6.1
, R.
scan( );
read.table( ) read.csv( ).
6.1.1
scan( )
.
:
scan(file = "", what = double(0), nmax = -1, n = -1, sep = "",
quote = if(identical(sep, "\n")) "" else "\"", dec = ".",
skip = 0, nlines = 0, na.strings = "NA",
flush = FALSE, fill = FALSE, strip.white = FALSE,
quiet = FALSE, blank.lines.skip = TRUE, multi.line = TRUE,
comment.char = "", allowEscapes = FALSE,
encoding = "unknown")
:
117
file , ; file=, ; , ()
; ;
URL.
what : logical, integer, numeric,
complex, character, list; ,
;
nmax
;
;
n ; integer
sep ; - ;
quote ( );
dec ( );
skip , ;
nlines
;
na.strings
NA;
NA;
flush TRUE
( );
fill TRUE
,
what;
strip.white ; sep, ()
;
118
quiet ; FALSE
, ;
blank.lines.skip ; TRUE
( ) (, skip
nlines );
multi.line ; what
list; FALSE
; fill=T,
;
comment.char , ;
allowEscapes \n, \a, \b, \f , \r, \t, \v
( TRUE) ( FALSE);
encoding , .
6.1.2
read.table() read.csv()
( ),
read.table( ) read.csv().
:
read.table(file, header = FALSE, sep = "", quote = "\"",
dec = ".", row.names, col.names,
as.is = !stringsAsFactors,
na.strings = "NA", colClasses = NA, nrows = -1,
skip = 0, check.names = TRUE, fill = !blank.lines.skip,
strip.white = FALSE, blank.lines.skip = TRUE,
comment.char = "#",
allowEscapes = FALSE, flush = FALSE,
stringsAsFactors = default.stringsAsFactors(),
fileEncoding = "", encoding = "unknown")
:
read.csv(file, header = TRUE, sep = ",", quote="\"", dec=".",
fill = TRUE, comment.char="", ...)
119
120
na.strings , NA;
nrows ; ;
skip ;
, ;
check.names ; TRUE
;
fill ; TRUE
() ;
strip.white ;
sep,
;
:
read.table()
.
, ( ).
row.names (.. ),
, .
.
, ,
.
nrows ( ) .
scan().
121
6.2
:
write();
cat();
write.table(), write.csv() write.csv().
6.2.1
write()
write() ( )
R. :
write(x, file = "data",
ncolumns = if(is.character(x)) 1 else 5,
append = FALSE, sep = " ")
:
x , ;
file , ;
ncolumns , ;
append TRUE, , FALSE,
;
sep ( \t )
write(x),
.
6.2.2
cat()
cat()
cat(... , file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE)
:
. . . R, .
122
file , ;
, ;
sep ;
fill . fill , ( ).
fill FALSE,
\n; TRUE, width, ;
labels , . ,
fill=FALSE.
append , . TRUE,
, FALSE, .
cat() , .
6.2.3
write.table(),
write.csv()
write.csv()
write.table() ( ) . (
), .
:
write.table(x, file = "", append = FALSE, quote = TRUE, sep = " ",
eol = "\n", na = "NA", dec = ".", row.names = TRUE,
col.names = TRUE, qmethod = c("escape", "double"))
:
write.csv(...)
write.csv2(...)
:
x . ,
() .
123
file , .
append ,
. TRUE,
, FALSE .
quote . quote TRUE, .
FALSE . quote ,
,
.
.
sep , ;
.
eol ; Windows Unix - \n, \r;
MacOS - \r.
na
.
dec ( ).
row.names , .
( TRUE, ,
),
, .
col.names , .
qmethod , a . escape (
- ) double ( ).
write.csv( ) write.csv2( ) col.names, sep, dec
qmethod .
:
,
,
row.names=TRUE, .
124
.
,
;
col.names quote , .
, ,
.
( , ) ( quote)
dec ,
(.. ).
*.CSV
.
write.csv( ) write.csv2( )
*.csv.
row.names=TRUE, qmethod
col.names NA; row.names=FALSE, qmethod=
col.names=TRUE.
write.csv( ) dec ., sep ,.
write.csv2( ) dec=,, sep=;.
.
col.names, sep,
dec qmethod .
( ),
write.table( ) , .
43.
125
126
7
R
R ( , ) graphics. :
();
();
;
() , .
() . .
, .
, () .
.
7.1
:
par() ;
barplot() ();
127
boxplot() boxplot
();
boxplot.matrix() boxplot;
bxp() boxplot;
contour() ( );
curve() ();
dotchart() ();
frame() ;
plot.new() ;
hist() ();
image()
;
matplot() ;
mosaicplot() ();
persp() ;
pie() ;
plot() ;
plot.data.frame() ;
plot.default() ;
plot.factor() ;
plot.formula() formula;
plot.histogram() ();
128
plot.table() ( );
screen ;
stem() ();
stripchart() ( boxplot()).
.
, , ,
R.
7.1.1
par()
par(), .
:
par(..., no.readonly = FALSE)
. . . = , . ,
par(), R.
> par()
( 70 )
( ), ..
, .
$xlog
$ylog
$adj
$ann
$ask
$bg
$bty
$cex
$cex.axis
$cex.lab
$cex.main
$cex.sub
$cin
$col
$col.axis
$col.lab
$col.main
$col.sub
$cra
$crt
$csi
$cxy
$din
$err
$family
$fg
$fig
$fin
$font
$font.axis
$font.lab
$font.main
$font.sub
$lab
$las
$lend
129
$lheight
$ljoin
$lmitre
$lty
$lwd
$mai
$mar
$mex
$mfcol
$mfg
$mfrow
$mgp
$mkh
$new
$oma
$omd
$omi
$pch
$pin
$plt
$ps
$pty
$smo
$srt
$tck
$tcl
$usr
$xaxp
$xaxs
$xaxt
$xpd
$yaxp
$yaxs
$yaxt
(
,
130
mai=c(, , , ) ( ) (
(1.02, 0.82, 0.82, 0.42)); mar=c(, , , )
( ) ( c(5, 4, 4, 2) + 0.1); mex
, ;
mfcol mfrow (nr, nc),
nr nc , , ,
( );
mfg=c(i, j) , ( mfcol
mfrow );
new ; TRUE
; FALSE;
oma c(, , , )
, omd
c(x1, x2, y1, y2) ( NDC (normalized device
coordinates)); omi c(, , , )
;
pin ( ) ,
plt c(x1, x2, y1, y2)
, ps (
1/72 ), pty
, , s
, m ;
usr c(x1, x2, y1, y2)
( );
xlog ylog
.
44 par(), ( ),
.
131
. , , , ( ).
op = par(mfrow = c(3,3), mar = .1+ c(2,2,3,1))
.
.
.
par(op)
7.1.2
plot()
plot(x, y, ...)
y .
:
x .
,
.
. . . . :
type , . : p (), l , b
, c b, o
, h -
, s , S
(. ), n ( ).
type .7.1.
log ( x, y
xy),
.
lty . : solid (1)
; blank (0) ; dashed (2) ; dotdash (4) -; dotted (3) ; longdash (5) ; twodash (6)
132
1.5
2.0
1.0
0.0
0.5
1.5
2.0
0.0
0.5
1.0
2.0
0.5
1.0
1.5
2.0
0.0
0.5
1.0
2.0
0.0
1.0
0.0
1.0
0.0
1.0
1.5
1.0
S
1.0
s
1.0
2.0
2.0
1.0
1.5
1.5
1.0
2.0
0.0
0.0
1.5
1.0
1.5
0.0
1.0
1.0
1.0
c
1.0
1.0
0.0
1.0
0.5
1.0
1.0
0.0
0.5
0.0
1.0
0.5
0.0
0.0
0.0
0.0
1.0
1.0
1.0
0.0
0.5
1.0
x
1.5
2.0
0.0
0.5
1.0
x
133
1.0
1.5
2.0
0.0
1.0
lty=dotdash
lty=dotted
1.5
2.0
1.5
2.0
1.5
2.0
0.0
1.0
0.0
1.0
1.0
1.5
2.0
0.0
0.5
1.0
lty=longdash
lty=twodash
1.0
1.0
0.0
1.0
0.5
1.0
0.0
0.0
0.5
1.0
0.5
1.0
0.0
0.0
1.0
0.0
1.0
1.0
lty=dashed
1.0
lty=solid
0.0
0.5
1.0
1.5
2.0
0.0
0.5
1.0
x
lwd ;
lwd=1.
xlim (x1, x2) x.
ylim (y1, y2) y.
frame
.
plot(). , ,
.
44. y = cos(x) [2; 2] .
.
par(mfrow=c(1,2))
, .
x=seq(-2*pi,2*pi,by=0.01)
y=cos(x)
:
plot(x,y,type=l,lty=1,col=black,ylab=cos(x))
plot() cos,
2 2, .
plot(cos,-2*pi,2*pi)
7.3.
plot() :
plot(x, y = NULL, type = "p", xlim = NULL, ylim = NULL,
log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
ann = par("ann"), axes = TRUE, frame.plot = axes,
panel.first = NULL, panel.last = NULL, asp = NA, ...)
:
ann
; TRUE.
135
1.0
1.0
0.5
0.0
cos (x)
0.5
1.0
0.5
0.0
cos(x)
0.5
1.0
136
7.1.3
( )
par() mfrow mfcol,
:
split.screen(figs, screen, erase = TRUE)
screen(n = , new = TRUE)
erase.screen(n = )
close.screen(n, all.screens = FALSE)
split.screen() ,
;
.
screen() , .
erase.screen() ( )
close.screen() .
:
figs ,
( figs=c(2,3) ,
2 , 3 ).
screen .
erase .
n ( screen(n)),
( erase.screen()) (close.screen()).
new ; TRUE
.
all.screens .
.
1. split.screen() R . screen(),
erase.screen(), close.screen() split.screen() ( ) .
close.screen(all =
TRUE).
137
2. (.
).
3.
, . ,
.
4. screen(), erase.screen(), close.screen() split.screen()
par(mfrow) par(mfcol).
5. erase.screen , ( R ).
6. split.screen
close.screen(all = TRUE).
.
45. ( .)
par(bg = "white")
:
split.screen(c(2,1))
split.screen(c(1,3), screen = 2)
, .
> split.screen()
[1] 1 2 3 4 5
5 :
.
.
screen(1)
plot(cos,-pi,pi)
4 .
138
1.0
0.0
1.0
cos (x)
0.0
1.0
sin (x)
1.0
0 2
x
. 7.4:
screen(4)
plot(sin,-pi,pi)
.
close.screen(all = TRUE)
.7.4
46. ,
, .
par(bg = "white")
split.screen(c(2,1))
split.screen(c(1,2),2)
, . .
plot(1:10)
139
y.
.
erase.screen()
plot(1:10, ylab=)
, .
screen(1)
plot(1:10)
screen(4)
plot(1:10, ylab="ylab 4")
, .
screen(1, FALSE)
plot(10:1, axes=FALSE, lty=2, ylab="")
.
close.screen(all = TRUE)
.7.5
7.1.4
contour()
contour() .
:
ontour(x = seq(0, 1, length.out = nrow(z)),
y = seq(0, 1, length.out = ncol(z)),
z, nlevels = 10, levels = pretty(zlim, nlevels),
labels = NULL,
xlim = range(x, finite = TRUE),
ylim = range(y, finite = TRUE),
zlim = range(z, finite = TRUE),
labcex = 0.6, drawlabels = TRUE, method = "flattest",
vfont, axes = TRUE, frame.plot = axes,
col = par("fg"), lty = par("lty"), lwd = par("lwd"),
add = FALSE, ...)
:
140
2 4 6 8
1:10
10
Index
ylab 4
2 4 6 8
2 4 6 8
1:10
10
Index
10
Index
. 7.5:
x, y , z. x y ,
NA .
0 1. x
, x$x x$y x y.
x$z, z.
z (x,y): z = (f (xi , yj ))ij .
nlevels , levels.
levels , .
labels .
labcex .
drawlabels .
method . : simple, edge flattest ( ).
141
= 0, 3 -
for(f in pi^(0:3))
contour(cos(r^2)*exp(-r/f),
drawlabels = FALSE, axes = FALSE, frame = TRUE)
142
. 7.6:
48. method. x.
x <- -6:16
4 .
op <- par(mfrow = c(2, 2))
:
contour(outer(x, x), method = "edge", vfont = c("sans serif", "plain"))
p
z = x/ |x|. image(x, x, z),
.
z <- outer(x, sqrt(abs(x)), FUN = "/")
image(x, x, z)
contour(x, x, z, col = "pink", add = TRUE, method = "edge",
vfont = c("sans serif", "plain"))
143
:
contour(x, x, z, ylim = c(1, 6), method = "simple", labcex = 1)
.
contour(x, x, z, ylim = c(-6, 6), nlev = 20, lty = 2, method = "simple")
15
.7.7.
10
0.0
0.4
0.8
0.0
0.2
0.4
0.6
0.8
1.0
10
2
15
3
2
5
4
5
8
9
10
11
12
13
14
15
16
16
15
14
13
12
11
10
9
8
5
4
3
4
5
2 0
8 10121416
0
10
15
10
15
16
14
12
. 7.7: method
7.1.5
curve()
curve( ) .
curve(expr, from = NULL, to = NULL, n = 101, add = FALSE,
type = "l", ylab = NULL, log = NULL, xlim = NULL, ...)
:
144
expr ,
.
from to , . ,
x . x ,
[0; 1].
n ,
expr.
add
(add=TRUE) (add=FALSE).
49.
f1 (x) = x2 3x,
f2 (x) = x2 2, f3 (x) = cos(x) f4 (x) = sin cos(x) ex/2 curve(),
.7.8.
par(mfrow=c(2,2))
curve(x^3-3*x, -2, 2)
curve(x^2-2, add = TRUE, col = "violet")
plot(cos, -pi, 3*pi)
plot(cos, xlim = c(-pi,3*pi), n = 1001, col = "blue", add=TRUE)
f1 <- function(x) sin(cos(x)*exp(-x/2))
curve(f1, -8, 7, n=2001)
plot (f1, -8, -5)
7.1.6
R .
.
colors() colours() 657 .
colors() ( colours()) (.. ),
.
> color=colors()
> color[10:30]
[1] "aquamarine2"
[5] "azure1"
"aquamarine3"
"azure2"
145
"aquamarine4"
"azure3"
"azure"
"azure4"
1.0
cos (x)
0.0
0.5
2
1
0
1.0
1
2
x^3 3 * x
1.0
0.0
f1 (x)
0.5
1.0
0.5
0.0
1.0
f1 (x)
1.0
8.0
7.0
6.0
5.0
. 7.8: curve()
[9]
[13]
[17]
[21]
"beige"
"bisque3"
"blue"
"blue4"
"bisque"
"bisque4"
"blue1"
"bisque1"
"black"
"blue2"
"bisque2"
"blanchedalmond"
"blue3"
> colors()[1:5]
[1] "white"
"aliceblue"
[5] "antiquewhite2"
> colors()
[1] "white"
[4] "antiquewhite1"
[7] "antiquewhite4"
[10] "aquamarine2"
[13] "azure"
[16] "azure3"
[19] "bisque"
[22] "bisque3"
[25] "blanchedalmond"
"antiquewhite"
"aliceblue"
"antiquewhite2"
"aquamarine"
"aquamarine3"
"azure1"
"azure4"
"bisque1"
"bisque4"
"blue"
146
"antiquewhite1"
"antiquewhite"
"antiquewhite3"
"aquamarine1"
"aquamarine4"
"azure2"
"beige"
"bisque2"
"black"
"blue1"
[28]
[31]
[34]
[37]
[40]
[43]
[46]
[49]
[52]
[55]
[58]
[61]
[64]
[67]
[70]
[73]
[76]
[79]
[82]
[85]
[88]
[91]
[94]
[97]
[100]
[103]
[106]
[109]
[112]
[115]
[118]
[121]
[124]
[127]
[130]
[133]
[136]
[139]
[142]
[145]
[148]
[151]
[154]
[157]
[160]
[163]
[166]
[169]
[172]
"blue2"
"blueviolet"
"brown2"
"burlywood"
"burlywood3"
"cadetblue1"
"cadetblue4"
"chartreuse2"
"chocolate"
"chocolate3"
"coral1"
"coral4"
"cornsilk1"
"cornsilk4"
"cyan2"
"darkblue"
"darkgoldenrod1"
"darkgoldenrod4"
"darkgrey"
"darkolivegreen"
"darkolivegreen3"
"darkorange1"
"darkorange4"
"darkorchid2"
"darkred"
"darkseagreen1"
"darkseagreen4"
"darkslategray1"
"darkslategray4"
"darkviolet"
"deeppink2"
"deepskyblue"
"deepskyblue3"
"dimgrey"
"dodgerblue2"
"firebrick"
"firebrick3"
"forestgreen"
"gold"
"gold3"
"goldenrod1"
"goldenrod4"
"gray1"
"gray4"
"gray7"
"gray10"
"gray13"
"gray16"
"gray19"
"blue3"
"brown"
"brown3"
"burlywood1"
"burlywood4"
"cadetblue2"
"chartreuse"
"chartreuse3"
"chocolate1"
"chocolate4"
"coral2"
"cornflowerblue"
"cornsilk2"
"cyan"
"cyan3"
"darkcyan"
"darkgoldenrod2"
"darkgray"
"darkkhaki"
"darkolivegreen1"
"darkolivegreen4"
"darkorange2"
"darkorchid"
"darkorchid3"
"darksalmon"
"darkseagreen2"
"darkslateblue"
"darkslategray2"
"darkslategrey"
"deeppink"
"deeppink3"
"deepskyblue1"
"deepskyblue4"
"dodgerblue"
"dodgerblue3"
"firebrick1"
"firebrick4"
"gainsboro"
"gold1"
"gold4"
"goldenrod2"
"gray"
"gray2"
"gray5"
"gray8"
"gray11"
"gray14"
"gray17"
"gray20"
147
"blue4"
"brown1"
"brown4"
"burlywood2"
"cadetblue"
"cadetblue3"
"chartreuse1"
"chartreuse4"
"chocolate2"
"coral"
"coral3"
"cornsilk"
"cornsilk3"
"cyan1"
"cyan4"
"darkgoldenrod"
"darkgoldenrod3"
"darkgreen"
"darkmagenta"
"darkolivegreen2"
"darkorange"
"darkorange3"
"darkorchid1"
"darkorchid4"
"darkseagreen"
"darkseagreen3"
"darkslategray"
"darkslategray3"
"darkturquoise"
"deeppink1"
"deeppink4"
"deepskyblue2"
"dimgray"
"dodgerblue1"
"dodgerblue4"
"firebrick2"
"floralwhite"
"ghostwhite"
"gold2"
"goldenrod"
"goldenrod3"
"gray0"
"gray3"
"gray6"
"gray9"
"gray12"
"gray15"
"gray18"
"gray21"
[175]
[178]
[181]
[184]
[187]
[190]
[193]
[196]
[199]
[202]
[205]
[208]
[211]
[214]
[217]
[220]
[223]
[226]
[229]
[232]
[235]
[238]
[241]
[244]
[247]
[250]
[253]
[256]
[259]
[262]
[265]
[268]
[271]
[274]
[277]
[280]
[283]
[286]
[289]
[292]
[295]
[298]
[301]
[304]
[307]
[310]
[313]
[316]
[319]
"gray22"
"gray25"
"gray28"
"gray31"
"gray34"
"gray37"
"gray40"
"gray43"
"gray46"
"gray49"
"gray52"
"gray55"
"gray58"
"gray61"
"gray64"
"gray67"
"gray70"
"gray73"
"gray76"
"gray79"
"gray82"
"gray85"
"gray88"
"gray91"
"gray94"
"gray97"
"gray100"
"green2"
"greenyellow"
"grey1"
"grey4"
"grey7"
"grey10"
"grey13"
"grey16"
"grey19"
"grey22"
"grey25"
"grey28"
"grey31"
"grey34"
"grey37"
"grey40"
"grey43"
"grey46"
"grey49"
"grey52"
"grey55"
"grey58"
"gray23"
"gray26"
"gray29"
"gray32"
"gray35"
"gray38"
"gray41"
"gray44"
"gray47"
"gray50"
"gray53"
"gray56"
"gray59"
"gray62"
"gray65"
"gray68"
"gray71"
"gray74"
"gray77"
"gray80"
"gray83"
"gray86"
"gray89"
"gray92"
"gray95"
"gray98"
"green"
"green3"
"grey"
"grey2"
"grey5"
"grey8"
"grey11"
"grey14"
"grey17"
"grey20"
"grey23"
"grey26"
"grey29"
"grey32"
"grey35"
"grey38"
"grey41"
"grey44"
"grey47"
"grey50"
"grey53"
"grey56"
"grey59"
"gray24"
"gray27"
"gray30"
"gray33"
"gray36"
"gray39"
"gray42"
"gray45"
"gray48"
"gray51"
"gray54"
"gray57"
"gray60"
"gray63"
"gray66"
"gray69"
"gray72"
"gray75"
"gray78"
"gray81"
"gray84"
"gray87"
"gray90"
"gray93"
"gray96"
"gray99"
"green1"
"green4"
"grey0"
"grey3"
"grey6"
"grey9"
"grey12"
"grey15"
"grey18"
"grey21"
"grey24"
"grey27"
"grey30"
"grey33"
"grey36"
"grey39"
"grey42"
"grey45"
"grey48"
"grey51"
"grey54"
"grey57"
"grey60"
148
[322]
[325]
[328]
[331]
[334]
[337]
[340]
[343]
[346]
[349]
[352]
[355]
[358]
[361]
[364]
[367]
[370]
[373]
[376]
[379]
[382]
[385]
[388]
[391]
[394]
[397]
[400]
[403]
[406]
[409]
[412]
[415]
[418]
[421]
[424]
[427]
[430]
[433]
[436]
[439]
[442]
[445]
[448]
[451]
[454]
[457]
[460]
[463]
[466]
"grey61"
"grey64"
"grey67"
"grey70"
"grey73"
"grey76"
"grey79"
"grey82"
"grey85"
"grey88"
"grey91"
"grey94"
"grey97"
"grey100"
"honeydew2"
"hotpink"
"hotpink3"
"indianred1"
"indianred4"
"ivory2"
"khaki"
"khaki3"
"lavenderblush"
"lavenderblush3"
"lemonchiffon"
"lemonchiffon3"
"lightblue1"
"lightblue4"
"lightcyan1"
"lightcyan4"
"lightgoldenrod2"
"lightgoldenrodyellow"
"lightgrey"
"lightpink2"
"lightsalmon"
"lightsalmon3"
"lightskyblue"
"lightskyblue3"
"lightslategray"
"lightsteelblue1"
"lightsteelblue4"
"lightyellow2"
"limegreen"
"magenta1"
"magenta4"
"maroon2"
"mediumaquamarine"
"mediumorchid1"
"mediumorchid4"
"grey62"
"grey65"
"grey68"
"grey71"
"grey74"
"grey77"
"grey80"
"grey83"
"grey86"
"grey89"
"grey92"
"grey95"
"grey98"
"honeydew"
"honeydew3"
"hotpink1"
"hotpink4"
"indianred2"
"ivory"
"ivory3"
"khaki1"
"khaki4"
"lavenderblush1"
"lavenderblush4"
"lemonchiffon1"
"lemonchiffon4"
"lightblue2"
"lightcoral"
"lightcyan2"
"lightgoldenrod"
"lightgoldenrod3"
"lightgray"
"lightpink"
"lightpink3"
"lightsalmon1"
"lightsalmon4"
"lightskyblue1"
"lightskyblue4"
"lightslategrey"
"lightsteelblue2"
"lightyellow"
"lightyellow3"
"linen"
"magenta2"
"maroon"
"maroon3"
"mediumblue"
"mediumorchid2"
"mediumpurple"
149
"grey63"
"grey66"
"grey69"
"grey72"
"grey75"
"grey78"
"grey81"
"grey84"
"grey87"
"grey90"
"grey93"
"grey96"
"grey99"
"honeydew1"
"honeydew4"
"hotpink2"
"indianred"
"indianred3"
"ivory1"
"ivory4"
"khaki2"
"lavender"
"lavenderblush2"
"lawngreen"
"lemonchiffon2"
"lightblue"
"lightblue3"
"lightcyan"
"lightcyan3"
"lightgoldenrod1"
"lightgoldenrod4"
"lightgreen"
"lightpink1"
"lightpink4"
"lightsalmon2"
"lightseagreen"
"lightskyblue2"
"lightslateblue"
"lightsteelblue"
"lightsteelblue3"
"lightyellow1"
"lightyellow4"
"magenta"
"magenta3"
"maroon1"
"maroon4"
"mediumorchid"
"mediumorchid3"
"mediumpurple1"
[469]
[472]
[475]
[478]
[481]
[484]
[487]
[490]
[493]
[496]
[499]
[502]
[505]
[508]
[511]
[514]
[517]
[520]
[523]
[526]
[529]
[532]
[535]
[538]
[541]
[544]
[547]
[550]
[553]
[556]
[559]
[562]
[565]
[568]
[571]
[574]
[577]
[580]
[583]
[586]
[589]
[592]
[595]
[598]
[601]
[604]
[607]
[610]
[613]
"mediumpurple2"
"mediumseagreen"
"mediumturquoise"
"mintcream"
"mistyrose2"
"moccasin"
"navajowhite2"
"navy"
"olivedrab"
"olivedrab3"
"orange1"
"orange4"
"orangered2"
"orchid"
"orchid3"
"palegreen"
"palegreen3"
"paleturquoise1"
"paleturquoise4"
"palevioletred2"
"papayawhip"
"peachpuff2"
"peru"
"pink2"
"plum"
"plum3"
"purple"
"purple3"
"red1"
"red4"
"rosybrown2"
"royalblue"
"royalblue3"
"salmon"
"salmon3"
"seagreen"
"seagreen3"
"seashell1"
"seashell4"
"sienna2"
"skyblue"
"skyblue3"
"slateblue1"
"slateblue4"
"slategray2"
"slategrey"
"snow2"
"springgreen"
"springgreen3"
"mediumpurple3"
"mediumslateblue"
"mediumvioletred"
"mistyrose"
"mistyrose3"
"navajowhite"
"navajowhite3"
"navyblue"
"olivedrab1"
"olivedrab4"
"orange2"
"orangered"
"orangered3"
"orchid1"
"orchid4"
"palegreen1"
"palegreen4"
"paleturquoise2"
"palevioletred"
"palevioletred3"
"peachpuff"
"peachpuff3"
"pink"
"pink3"
"plum1"
"plum4"
"purple1"
"purple4"
"red2"
"rosybrown"
"rosybrown3"
"royalblue1"
"royalblue4"
"salmon1"
"salmon4"
"seagreen1"
"seagreen4"
"seashell2"
"sienna"
"sienna3"
"skyblue1"
"skyblue4"
"slateblue2"
"slategray"
"slategray3"
"snow"
"snow3"
"springgreen1"
"springgreen4"
150
"mediumpurple4"
"mediumspringgreen"
"midnightblue"
"mistyrose1"
"mistyrose4"
"navajowhite1"
"navajowhite4"
"oldlace"
"olivedrab2"
"orange"
"orange3"
"orangered1"
"orangered4"
"orchid2"
"palegoldenrod"
"palegreen2"
"paleturquoise"
"paleturquoise3"
"palevioletred1"
"palevioletred4"
"peachpuff1"
"peachpuff4"
"pink1"
"pink4"
"plum2"
"powderblue"
"purple2"
"red"
"red3"
"rosybrown1"
"rosybrown4"
"royalblue2"
"saddlebrown"
"salmon2"
"sandybrown"
"seagreen2"
"seashell"
"seashell3"
"sienna1"
"sienna4"
"skyblue2"
"slateblue"
"slateblue3"
"slategray1"
"slategray4"
"snow1"
"snow4"
"springgreen2"
"steelblue"
[616]
[619]
[622]
[625]
[628]
[631]
[634]
[637]
[640]
[643]
[646]
[649]
[652]
[655]
"steelblue1"
"steelblue4"
"tan2"
"thistle"
"thistle3"
"tomato1"
"tomato4"
"turquoise2"
"violet"
"violetred2"
"wheat"
"wheat3"
"yellow"
"yellow3"
"steelblue2"
"tan"
"tan3"
"thistle1"
"thistle4"
"tomato2"
"turquoise"
"turquoise3"
"violetred"
"violetred3"
"wheat1"
"wheat4"
"yellow1"
"yellow4"
"steelblue3"
"tan1"
"tan4"
"thistle2"
"tomato"
"tomato3"
"turquoise1"
"turquoise4"
"violetred1"
"violetred4"
"wheat2"
"whitesmoke"
"yellow2"
"yellowgreen"
.7.9 R.
x=1:657
y1=rep(1,length(x))
plot(x,y1,type=l,lty=1,col=colors()[5],xlim=c(0,658),ylim=c(0,658))
for (i in 2:657) abline(h=i,col=colors()[i])
RGB #RRGGBB, RR, GG BB , , 00
FF.
- palette()
> palette()
[1] "black"
"red"
[8] "gray"
> z=palette()
> z[3]
[1] "green3"
"green3"
"blue"
"cyan"
"magenta" "yellow"
rgb(red, green,
blue), red, green, blue = 0, 1.
> rgb(0,0,1)
[1] "#0000FF"
> rgb(0,0,0.2)
[1] "#000033"
> rgb(0.1,0.1,0.2)
[1] "#1A1A33"
151
600
500
400
300
0
100
200
y1
100
200
300
400
500
600
. 7.9: R
7.2
:
abline()
;
arrows() ;
axis() ;
box() ;
grid() ;
152
legend() ;
lines() , ;
mtext() ;
points() ;
polygon() ;
rect() ;
segments() ;
symbols() (, ,
, , , boxplot) ;
text() ;
title() ;
xspline()
.
7.2.1
abline(), lines(), arrows(), points(),
polygon(), rect(), segments(), symbols()
, .
abline()
:
abline(a = NULL, b = NULL, h = NULL, v = NULL, reg = NULL,
coef = NULL, untf = FALSE, ...)
y = a + bx
.
:
a b , .
153
h y .
v x .
coef c(a,b) a b.
reg .
1 b
y = bx, ;
2 a b.
untf . untf=TRUE,
( )
( xlog ylog), (
) , .
. . . :
col .
lty .
lwd .
:
abline(a, b, untf = FALSE, ...)
abline(h=, untf = FALSE, ...)
abline(v=, untf = FALSE, ...)
abline(coef=, untf = FALSE, ...)
abline(reg=, untf = FALSE, ...)
50.
R. .7.10.
x=seq(-pi,pi,by=0.01)
y=cos(x)
plot(x,y, type=n,xlab="x", ylab="y", xlim=c(-pi-0.1,pi+0.1),
ylim=c(-1.1,1.1))
abline(a=0,b=1,lty=1)
154
0.0
1.0
0.5
0.5
1.0
abline(a=0,b=-1,lty=2)
abline(coef=c(0,0.5),lty=3)
abline(reg=c(0,-0.5),lty=4)
abline(v = -3:3, col=colors()[451:457])
abline(h=seq(-1,1,by=0.5), col=colors()[21:25])
abline(h=0,v=0,lwd=2,col=black)
. 7.10:
lines()
lines() . :
lines(x, y = NULL, type = "l", ...)
x
( 0,
0) (
0, 0).
155
x 0,
y 0.
x () y NA.
() ( )
.
type = h, col, ,
.
51. , .
par(bg=white)
x=seq(-pi,pi,by=0.01)
y=sqrt(abs(cos(x)))
x1=seq(-3,3,by=0.5)
y1=sqrt(abs(cos(x1)))
split.screen(c(2,1))
plot(x,y, type=l,xlab="x", ylab="y", xlim=c(-pi-0.1,pi+0.1),
ylim=c(-0.1,1.1))
lines(x1,y1,col=blue,lwd=2)
lines(x1,y1,type=h,col=green)
split.screen(c(1,2),2)
plot(x,y, type=l,xlab="x", ylab="y", xlim=c(-pi-0.1,pi+0.1),
ylim=c(-0.1,1.1))
z=list(x1,y1)
lines(x1,y1,col=darkred,lwd=2)
screen(4)
plot(x,y, type=l,xlab="x", ylab="y", xlim=c(-pi-0.1,pi+0.1),
ylim=c(-0.1,1.1))
z=cbind(x1,y1)
lines(x1,y1,col=lightgoldenrod,lwd=2)
close.screen(all = TRUE)
points()
points() ( - )
. :
points(x, y = NULL, type = "p", ...)
, x, x
. x
0x, 0y y.
156
0.6
0.0
0.6
0.0
0.6
0.0
1 2 3
1 2 3
x
. 7.11:
:
pch . 0 255, : 0 ,
1 , 2 4, 3 +, 4 , 5 , 6 , 7 , 8 , 9
, 10 , 11 , 12
, 13 , 14 ; 1525
; 2631
; 32127 ASCII ; 128255 . (0127) .7.12.
cex .
bg ,
1525.
col .
52. points().
157
20
x=0:20
y=0:20
plot(x,y,type=n)
for (i in 0:14)
points(x[i+1],y[20],pch=i,cex = 1.5)
for (i in 15:25)
points(x[i+1-15],y[18],pch=i,cex = 1.5,col=i,bg=i)
for (i in 32:51)
points(x[i+1-32],y[16],pch=i,cex = 1.5)
for (i in 52:71)
points(x[i+1-52],y[14],pch=i,cex = 1.5)
for (i in 72:91)
points(x[i+1-72],y[12],pch=i,cex = 1.5)
for (i in 92:111)
points(x[i+1-92],y[10],pch=i,cex = 1.5)
for (i in 112:127)
points(x[i+1-112],y[8],pch=i,cex = 1.5)
10
15
10
15
20
. 7.12: , points()
158
arrows()
53. .
x=1:12;y=c(12,10,11,9,6,7,5,8,1,3,2,4)
i = order(x,y); x <- x[i]; y <- y[i]
par(mfrow=c(1,2))
plot(x,y)
s = seq(length(x)-1)# one shorter than data
arrows(x[s], y[s], x[s+1], y[s+1], col= 1:3,angle=10)
12
12
plot(x,y)
## draw arrows from point to point :
s <- seq(length(x)-1)# one shorter than data
segments(x[s], y[s], x[s+1], y[s+1], col= 1:3)
10
10
8 10
8 10
. 7.13:
polygon()
160
161
rect()
rect(xleft, ybottom, xright, ytop, density = NULL, angle = 45,
col = NA, border = NULL, lty = par("lty"), lwd = par("lwd"),...)
polygon(), :
xleft 0x ( ).
ybottom 0y ( ).
xright 0x ( ).
ytop 0y ( ).
54. . 4 .
op = par(mfrow=c(2,2))
.
plot(c(1,9), 1:2, type="n",xlab="", ylab="")
polygon(1:9, c(2,1,2,1,1,2,1,2,1),
col=c("red", "blue"),
border=c("green", "yellow"),
lwd=3, lty=c("dashed", "solid"))
, NA.
plot(c(1,9), 1:2, type="n",xlab="", ylab="")
polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
col=c("red", "blue"),
border=c("green", "yellow"),
lwd=3, lty=c("dashed", "solid"))
.
plot(c(100, 250), c(300, 450), type = "n", xlab="", ylab="")
i = 4*(0:10)
rect(100+i, 300+i, 150+i, 380+i, col=rainbow(11, start=.7,end=.1))
.
162
1.8
1.4
1.0
1.0
1.4
1.8
400
350
300
300
350
400
450
450
100
150
200
250
100
120
140
160
180
200
. 7.14:
symbols()
symbols(x, y = NULL, circles, squares, rectangles, stars,
thermometers, boxplots, inches = TRUE, add = FALSE,
fg = par("col"), bg = NA,
xlab = NULL, ylab = NULL, main = NULL,
xlim = NULL, ylim = NULL, ...)
163
, , , , boxplot.
:
x, y .
circles .
squares .
rectangles : , .
stars -
.
thermometers .
. ,
( fg) ( ). , ( fg) ( , ) (
bg).
boxplots .
boxplots,
,
boxplots.
inches . inches=TRUE ( ), ,
. inches=FALSE, 0. inches , .
add .
fg ( ).
bg .
164
7.2.2
axis(),
grid() box()
, () , ,
.
axis()
100
20
40
60
80
20
40
60
80
100
. 7.15: symbols()
lwd = 1, lwd.ticks = lwd, col = NULL, col.ticks = NULL,
hadj = NA, padj = NA, ...)
. :
side ,
. : 1 , 2 , 3 , 4 .
at ( ). Inf, NaN NA .
labels . labels = TRUE,
at. , at ( ).
tick ()
.
166
grid()
op=par(mfrow=c(3,2))
plot(1:4, rnorm(4), axes = FALSE)
axis(1, at=1:4, labels=LETTERS[1:4])
axis(2)
:
plot(1:7, rnorm(7), axes=FALSE,
type = "s", frame = FALSE, col = "red")
axis(4, col = "violet", col.axis="dark violet", lwd = 2)
axis(3, col = "gold", lty = 2, lwd = 0.5)
plot(1:4, rnorm(4), axes = FALSE)
axis(1, at=1:4, labels=LETTERS[1:4],col=darkblue,
col.ticks=violet,lwd.ticks=2)
axis(2,col=red)
0.
plot(1:3)
grid(NA, 5, lwd = 2)
:
plot(cos,-pi,pi)
grid(7, 5, lwd = 1, lty = 1) # grid only in y-direction
(
)
plot(1:7, abs(rnorm(7)), type = h, axes = FALSE)
axis(1, at = 1:7, labels = letters[1:7],col=blue)
box(which=plot,lty = solid, col = red)
box(which=inner,lty = 4, col = blue,lwd=2)
box(which=outer,lty = 5, col = gold,lwd=3)
box(which=figure,lty = 3, col = green)
par(op)
169
7
1.0 0.0
2.5
rnorm(7)
0.2
0.2
rnorm(4)
3.0
2.0
1:3
1.0
0.0
1:7
1.5
rnorm(4)
1.0
1:4
1.0
1.5
2.0
3.0
abs(rnorm(7))
0.0
1.0
cos (x)
2.5
Index
1.0
1:4
1:7
7.2.3
expression(), text(), legend(), mtext()
title()
, .
expression()
expression()1 ,
, .
:
expression()
1
expression() R ,
170
demo(plotmath)
.
.7.177.21
Arithmetic Operators
Radicals
x+y
x+y
sqrt(x)
xy
xy
sqrt(x, y)
x*y
xy
x
y
x/y
x y
Relations
x == y
x=y
x %+% y
xy
x != y
xy
x%/%y
xy
x<y
x<y
x %*% y
xy
x <= y
xy
x %.% y
xy
x>y
x>y
x >= y
xy
+x
+x
x %~~% y
xy
Sub/Superscripts
x %=~% y
xy
x[i]
xi
x %==% y
xy
x^2
x2
x %prop% y
xy
x*y
xy
plain(x)
paste(x, y, z)
xyz
italic(x)
bold(x)
bolditalic(x)
underline(x)
Juxtaposition
Typeface
Lists
list(x, y, z)
x, y, z
. 7.17:
171
Ellipsis
Arrows
x %<>% y
xy
x %>% y
xy
x %<% y
xy
x %up% y
xy
Set Relations
xy
x %subset% y
x %down% y
x %<=>% y
xy
xy
x1, , xn
x1 + + xn
x %subseteq% y
xy
x %=>% y
xy
x %supset% y
xy
x %<=% y
xy
x %supseteq% y
xy
x %dblup% y
xy
x %notsubset% y
xy
x %dbldown% y
xy
x %in% y
xy
x %notin% y
xy
Symbolic Names
Alpha Omega
alpha omega
phi1 + sigma1
ring(x)
x^
~
x
x
Accents
hat(x)
tilde(x)
Upsilon1
infinity
32
bar(xy)
xy
32 * degree
widehat(xy)
xy
60 * minute
60
widetilde(xy)
xy
30 * second
30
. 7.18:
Style
displaystyle(x)
textstyle(x)
scriptstyle(x)
scriptscriptstyle(x)
Spacing
x ~ ~y
x y
x + phantom(0) + y
x+ +y
x + over(1, phantom(0))
x+
Fractions
x
frac(x, y)
y
x
over(x, y)
y
x
atop(x, y)
. 7.19:
text()
172
Big Operators
n
sum(x[i], i = 1, n)
xi
prod(plain(P)(X == x), x)
P(X = x)
integral(f(x) * dx, a, b)
f(x)dx
a
union(A[i], i == 1, n)
Ai
i=1
intersect(A[i], i == 1, n)
Ai
i=1
lim f(x)
lim(f(x), x %>% 0)
x0
min(g(x), x >= 0)
min g(x)
inf(S)
inf S
sup(S)
sup S
x0
. 7.20:
Grouping
(x + y) * z
(x + y)z
x^y + z
xy + z
x^(y + z)
x(y+z)
x^{y + z}
xy+z
(a, b]
x
y
group(lceil, x, rceil)
group(lfloor, x, rfloor)
group("|", x, "|")
|x|
. 7.21:
text (x, y = NULL, labels = seq_along(x), adj = NULL,
173
174
bg (, bty
n).
box.lwd (, bty = o).
box.lty (, bty = o).
box.col (, bty = o).
pt.bg (.7.2.1, points()).
cex .
pt.cex (.7.2.1,
points()).
pt.lwd (.7.2.1,
points()).
xjust . : 0 , 0.5
, 1 .
yjust . : 0 , 0.5
, 1 .
x.intersp .
y.intersp .
adj .
text.width (.. ). .
text.col .
merge , .
trace ( ).
plot . FALSE
,.
177
ncol (),
. ncol = 1 ( ).
horiz .
title .
inset . 0 1 .
xpd
().
title.col .
title()
, ,
title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
line = NA, outer = FALSE, ...)
, .
:
main .
.
sub . .
xlab 0.
ylab 0.
line .
outer .
...
57. , .
178
op=par(mfrow=c(3,2))
plot(-1:1,-1:1, type = "n", xlab = "Re", ylab = "Im")
K <- 16
text(exp(1i * 2 * pi * (1:K) / K), col = 2)
plot(1:10, (-4:5)^2, main="Parabola", xlab="",ylab=)
mtext("10 of them")
for(s in 1:4)
mtext(expression(y=x^2), line = (-1)^s,
side=s, col=s, font=s, cex= (1+s)/2)
x <- seq(-pi, pi, len = 65)
plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2)
points(x, cos(x), pch = 3, col = 4)
lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3,4,6),
text.col = "green4", lty = c(2, -1, 1), pch = c(-1, 3, 4),
merge = TRUE, bg = gray90)
x <- seq(-pi, pi, len = 65)
plot(x, sin(x), type="l", col = 2, xlab = expression(phi),
ylab = expression(f(phi)))
abline(h=-1:1, v=pi/2*(-6:6), col="gray90")
lines(x, cos(x), col = 3, lty = 2)
ex.cs1 <- expression(plain(sin) * phi, paste("cos", phi))
legend(-3, .9, ex.cs1, lty=1:2, col=2:3,
adj = c(0, .6))
x <- seq(-pi, pi, len = 65)
y=cos(x)
plot(x, y, type=n)
legend("bottomright", "(x,y)", pch=1, title="bottomright")
legend("bottom", "(x,y)", pch=1, title="bottom")
legend("bottomleft", "(x,y)", pch=1, title="bottomleft")
legend("topleft", "(x,y)", pch=1, title="topleft, inset = .05",
inset = .05)
legend("topright", "(x,y)", pch=1, title="topright, inset = .02",
inset = .02)
x <- seq(-4, 4, len = 101)
179
y <-sin(x)
plot(x, y, type = "l", xaxt = "n", xlab=,ylab=)
title(main = expression(paste(plain(sin) * phi, " and ",
plain(cos) * phi)),
ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken
xlab = expression(paste("Phase Angle ", phi)),
col.main = "blue")
curve(cos,from=-4,to=4,add=T)
axis(1, at = c(-pi, -pi/2, 0, pi/2, pi),
labels = expression(-pi, -pi/2, 0, pi/2, pi))
abline(h = 0, v = pi/2 * c(-1,1), lty = 2, lwd = .1, col = "gray70")
par(op)
180
2
1
16
15
9
1.0
1.0
11
0.5
14
13
12
0.0
x215
0.0
Im
0.5
1.0
x2
x2
8
10
25
0 5
1.0
Parabola
10 of 2
them
10
1.0
f()
sin
cos
1.0
0.0
0.5 1.5
sin
cos
tan
1.0
sin(x)
Re
bottom
(x,y)
2
bottomright
(x,y)
0
(x,y)
1.0
(x,y)
0.0
bottomleft
(x,y)
1.0
1.0
0.0
sin
1.0
Phase Angle
. 7.22:
181
8
.
.
, , ( ) ,
, .
8.1
: uniroot
base uniroot.all multiroot rootSolve.
8.1.1
uniroot
f , (.. ) . ,
f .
interval ,
(,
).
lower upper interval
.
f.lower f.upper ( f ).
tol .
maxiter .
... .
,
x (f (x ) == 0), x
tol. , , .
uniroot : x root, f (x ) f.root,
iter estim.prec. x , estim.prec
NA.
uniroot , .
, .
58. 5x2 10x = 0
[1; 3], [0; 3].
> f=function(x,a=5,b=10){f=a*x^2-b*x}
> uniroot(f,c(1,3))
$root
[1] 2.000000
$f.root
[1] -2.678252e-06
183
$iter
[1] 6
$estim.prec
[1] 6.535148e-05
> uniroot(f,c(0,3))
$root
[1] 0
$f.root
[1] 0
$iter
[1] 0
$estim.prec
[1] 0
x = 0.
8.1.2
uniroot.all
uniroot.all1 uniroot,
.
uniroot.all(f, interval, lower=min(interval), upper=max(interval),
tol=.Machine$double.eps^0.2, maxiter=1000, n=100, ...)
n , ,
. .
59.
5x2 10x = 0 [0; 3].
> f=function(x,a=5,b=10){f=a*x^2-b*x}
> uniroot.all(f,c(0,3),n=10)
[1] 0.000000 2.000000
. ,
, .
1
rootSolve
184
> uniroot.all(f,c(0,3))
[1] 0.000000 1.999999
n = 100 ( ).
8.1.3
multiroot
multiroot ( ) n n .
multiroot(f, start, maxiter=100,
rtol=1e-6, atol=1e-8, ctol=1e-8,
useFortran=TRUE, positive=FALSE,
jacfunc=NULL, jactype="fullint",
verbose=FALSE, bandup=1, banddown=1, ...)
, :
f , .
, start.
start . start ,
.
maxiter .
rtol atol
. ,
.
ctol .
, ,
.
positive . positive=TRUE, , .
: root
, f.root , iter , estim.precis .
185
8.2
R , ,
,
, ,
.
8.2.1
R
.
integrate(f, lower, upper, ..., subdivisions=100,
rel.tol = .Machine$double.eps^0.25, abs.tol = rel.tol,
stop.on.error = TRUE, keep.xy = FALSE, aux = NULL)
:
f R , . ,
.
lower upper .
. ,
(-) , () Inf,
(. 60).
... , f. , .
subdivisions ,
.
rel.tol , 0.5
1028 .
abs.tol .
186
stop.on.error . , .
: keep.xy aux, ,
S.
. :
value .
abs.error .
subdivisions .
message OK .
.
60.
= 0 = 1 [0; ).
> integrate(dnorm,0,Inf)
0.5 with absolute error < 4.7e-05
: 2, 20,
200, 2.000 20.000.
> integrate(dnorm,0,2)
0.4772499 with absolute error < 5.3e-15
> integrate(dnorm,0,20)
0.5 with absolute error < 3.7e-05
> integrate(dnorm,0,200)
0.5 with absolute error < 1.6e-07
> integrate(dnorm,0,2000)
0.5 with absolute error < 4.4e-06
> integrate(dnorm,0,20000)
0 with absolute error < 0
( 20000)
.
1
.
f (x) = (x+1)
x
0 . .
187
hessian .
... , .
, , ,
.
, D() , .. ( ) .
.
61.
f (x, y) = sin(cos(x + y 2 )) x y,
x = 4 y = 6 .
> trig = expression(sin(cos(x + y^2)))
> ( D.x = D(trig, "x") )
-(cos(cos(x + y^2)) * sin(x + y^2))
> ( D.y = D(trig, "y") )
-(cos(cos(x + y^2)) * (sin(x + y^2) * (2 * y)))
:
> ( D.x.x = D(D.x, "x") )
-(sin(cos(x + y^2)) * sin(x + y^2) * sin(x + y^2) + cos(cos(x +
y^2)) * cos(x + y^2))
> ( D.x.y = D(D.x, "y") )
-(sin(cos(x + y^2)) * (sin(x + y^2) * (2 * y)) * sin(x + y^2) +
cos(cos(x + y^2)) * (cos(x + y^2) * (2 * y)))
> ( D.y.y = D(D.y, "y") )
-(sin(cos(x + y^2)) * (sin(x + y^2) * (2 * y)) * (sin(x + y^2) *
(2 * y)) + cos(cos(x + y^2)) * (cos(x + y^2) * (2 * y) *
(2 * y) + sin(x + y^2) * 2))
> ( D.y.x = D(D.y, "y") )
-(sin(cos(x + y^2)) * (sin(x + y^2) * (2 * y)) * (sin(x + y^2) *
(2 * y)) + cos(cos(x + y^2)) * (cos(x + y^2) * (2 * y) *
(2 * y) + sin(x + y^2) * 2))
189
.
eval(). ( x y) , eval()
.
> x=pi/4; y=pi/6
> eval(D.x)
[1] -0.7698184
> eval(D.y)
[1] -0.806152
> eval(D.x.x)
[1] -0.7893344
> eval(D.x.y)
[1] -0.8265891
> eval(D.y.y)
[1] -2.405239
> eval(D.y.x)
[1] -2.405239
deriv() deriv3() .
> deriv(trig, c("x","y"), func = TRUE)
function (x, y)
{
.expr2 <- x + y^2
.expr3 <- cos(.expr2)
.expr5 <- cos(.expr3)
.expr6 <- sin(.expr2)
.value <- sin(.expr3)
.grad <- array(0, c(length(.value), 2L), list(NULL, c("x",
"y")))
.grad[, "x"] <- -(.expr5 * .expr6)
.grad[, "y"] <- -(.expr5 * (.expr6 * (2 * y)))
attr(.value, "gradient") <- .grad
.value
}
- , .value () .
190
8.2.2
R, , .
optim() .
optim(par, fn, gr = NULL, ...,
method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN"),
lower = -Inf, upper = Inf,
control = list(), hessian = FALSE)
:
par , .
fn ( ) , ,
. .
gr , BFGS, CG
L-BFGS-B.
... , fn gr.
method . optim()
:
Nelder-Mead . ,
, . .
BFGS - ,
.
CG , , .
192
L-BFGS-B BFGS, .
SANN .
, . . .
lower upper ,
L-BFGS-B.
control ,
:
trace . , .
trace, .
fnscale . , . ,
fn(par)/fnscale.
parscale , . , par/parscale
.
ndeps , , 103 .
maxit . :
100 BFGS, CG L-BFGS-B, 500 NelderMead, 10000 SANN.
abstol ,
.
reltol . 108 .
alpha , beta, gamma
Nelder-Mead.
REPORT BFGS,
L-BFGS-B SANN, trace . 10
BFGS L-BFGS-B 100
SANN.
193
type . . 1
FletcherReeves, 2 PolakRibiere, 3 BealeSorenson.
lmm bfL-BFGS-B.
5.
factr , bfLBFGS-B . ,
(1015 ),
factr. 107 .
pgtol , bfL-BFGSB.
temp tmax SANN.
.
hessian .
optim() ( , ).
optim()
:
par (
).
value .
counts , ,
.
convergence - :
0 ( SANN).
1 .
10 Nelder-Mead.
51 bfL-BFGS-B.
52 bfL-BFGS-B..
message ( NULL).
194
function(x) {
x[1]
x[2]
(x2 - x1 * x1)^2 + (1 - x1)^2
, ():
grr <- function(x) {
x1 <- x[1]
x2 <- x[2]
c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
200 * (x2 - x1 * x1))
}
:
> optim(c(-1.2,1), fr)
$par
[1] 1.000260 1.000506
$value
[1] 8.825241e-08
$counts
function gradient
195
NA
$convergence
[1] 0
$message
NULL
> optim(c(-1.2,1), fr, grr, method = "BFGS")
$par
[1] 1 1
$value
[1] 9.594955e-18
195
$counts
function gradient
110
43
$convergence
[1] 0
$message
NULL
> optim(c(-1.2,1), fr, NULL, method = "BFGS", hessian = TRUE)
$par
[1] 0.9998044 0.9996084
$value
[1] 3.827383e-08
$counts
function gradient
122
38
$convergence
[1] 0
$message
NULL
$hessian
[,1]
[,2]
[1,] 801.6881 -399.9218
[2,] -399.9218 200.0000
> optim(c(-1.2,1), fr, grr, method = "CG")
$par
[1] -0.7648373 0.5927588
$value
[1] 3.106579
$counts
function gradient
402
101
$convergence
[1] 1
$message
NULL
> optim(c(-1.2,1), fr, grr, method = "CG", control=list(type=2))
$par
196
.
optimize(f = , interval = , ..., lower = min(interval),
upper = max(interval), maximum = FALSE,
tol = .Machine$double.eps^0.25)
optimise(f = , interval = , ..., lower = min(interval),
upper = max(interval), maximum = FALSE,
:
f .
maximum , f.
interval , ,
.
... f.
lower .
upper .
maximum . ( ) ?
tol .
optimize optimise ()
. , (
) (), , , ,
().
optimize optimise :
minimum ()
objective.
.
63. f (x) =
[1; 4], [0; 3] [1, 3].
> f = function (x,a,b) {(x-a)^2*sin(x)/((x+b)^2)}
> xmin = optimize(f, c(-1, 4), tol = 0.0001, a = 5, b=2)
> xmin
$minimum
198
(x5)2 sin x
(x+2)2
[1] 3.642265
$objective
[1] -0.02779574
> xmax = optimize(f, c(-1, 4), tol = 0.0001, a = 5, b=2, maximum=T)
> xmax
$maximum
[1] 0.691799
$objective
[1] 1.634088
> xmin = optimize(f, c(0, 3), tol = 0.0001, a = 5, b=2)
> xmin
$minimum
[1] 2.999958
$objective
[1] 0.02258725
> xmax = optimize(f, c(0, 3), tol = 0.0001, a = 5, b=2, maximum=T)
> xmax
$maximum
[1] 0.6917868
$objective
[1] 1.634088
> xmin = optimize(f, c(1, 3), tol = 0.0001, a = 5, b=2)
> xmin
$minimum
[1] 2.999952
$objective
[1] 0.02258839
> xmax = optimise(f, c(1, 3), tol = 0.0001, a = 5, b=2, maximum=T)
> xmax
$maximum
[1] 1.000048
$objective
[1] 1.495910
nlm() f
. .
199
nlm(f, p, ..., hessian = FALSE, typsize = rep(1, length(p)),
fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6,
stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),
steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)
:
f .
, . .
, deriv
.
p , .
... f .
hessian . TRUE,
.
typsize .
fscale f .
print.level , .
0 ( ) ,
1 .
2 .
ndigit f.
gradtol , ,
.
stepmax .
steptol .
iterlim .
200
check.analyticals . .
nlm() :
minimum f .
estimate , f.
gradient .
hessian
.
code . 1 2 - ( ) , 35 .
iterations .
.
64. f (x) =
[1; 4], [0; 3] [1, 3].
(x5)2 sin x
(x+2)2
[1] -0.02779574
$estimate
[1] 3.642274
$gradient
[1] 4.610341e-09
$code
[1] 1
$iterations
[1] 7
, , .
nlminb() ( ) ()
PORT ( - . http://netlib.belllabs.com/cm/cs/cstr/153.pdf).
nlminb(start, objective, gradient = NULL, hessian = NULL, ...,
scale = 1, control = list(), lower = -Inf, upper = Inf)
:
start ,
.
objective , .
, . . ( Inf NA).
objective (. ...).
gradient (), .
hessian ,
( ).
...
objective.
scale (. PORT
http://netlib.bell-labs.com/cm/cs/cstr/153.pdf).
control :
202
eval.max
. 200.
iter.max . 150.
trace . 0 ,
.
abs.tol . 0. A
, 1e-20.
rel.tol . 1e-10.
x.tol . 1.5e8.
step.min .
2.2e-14.
lower upper
.
, :
par (.. ).
objective .
convergence . 0 .
message ( NULL).
iterations . N
evaluations .
65. f (x) =
[1; 4], [0; 3] [1, 3].
(x5)2 sin x
(x+2)2
[1] -0.02779574
$convergence
[1] 0
$message
[1] "relative convergence (4)"
$iterations
[1] 7
$evaluations
function gradient
9
10
x, x 0:
> nlminb(4,f,a=5,b=2,lower=0,upper=Inf)
$par
[1] 3.642276
$objective
[1] -0.02779574
$convergence
[1] 0
$message
[1] "relative convergence (4)"
$iterations
[1] 7
$evaluations
function gradient
8
10
,
constrOptim, .
constrOptim(theta, f, grad, ui, ci, mu = 1e-04, control = list(),
method = if(is.null(grad)) "Nelder-Mead" else "BFGS",
outer.iterations = 100, outer.eps = 1e-05, ...)
:
theta p ( , ).
f .
204
grad f ( ) NULL.
ui k p.
ci k.
mu .
control, method hessian , optim.
outer.iterations , .
outer.eps - .
... , f grad.
, ,
( ) ui ci 0.
- , optim(), : barrier.value
( )
outer.iterations.
.
66. f (x, y) = 100(y x2 ) +
(1 x)2 .
(x[1] = x, x[2] = y):
fr <x1 <x2 <100 *
}
function(x) {
x[1]
x[2]
(x2 - x1 * x1)^2 + (1 - x1)^2
, ():
grr <- function(x) {
x1 <- x[1]
x2 <- x[2]
c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
200 * (x2 - x1 * x1))
}
205
206
[1] R Development Core Team (2009). R: A language and environment for
statistical computing. R Foundation for Statistical Computing, Vienna,
Austria. ISBN 3-900051-07-0, URL http://www.R-project.org.
[2] Brian S. Everitt and Torsten Hothorn (2005). A Handbook of Statistical
Analyses Using R, London and Erlangen, http://www.R-project.org.
[3] Michael J. Crawley (2007). The R Book, John Wiley & Sons Ltd, The
Atrium, Southern Gate, Chichester. ISBN-13: 978-0-470-51024-7.
[4] John M. Chambers (2008). Software for Data Analysis. Programming
with R, Springer Science+Business Media, LLC, USA, ISBN: 978-0-38775935-7
[5] Phil Spector (2008). Data Manipulation with R,
Science+Business Media, LLC, USA, ISBN 978-0-387-74730-9
Springer
[6] Yosef Cohen and Jeremiah Y. Cohen (2008). Statistics and Data with
R, John Wiley & Sons Ltd,The Atrium, Southern Gate, Chichester, ISBN
978-0-470-75805-2
[7] J. H. Maindonald (2001). Using R for Data Analysis and Graphics An
Introduction. Statistical Consulting Unit of the Graduate School, Australian
National University.
[8] John Verzani. simpleR Using R for Introductory Statistics,
http://cran.R-project.org/other-docs.html.
[9] Jim Lemon. Kickstarting R, http://cran.R-project.org/other-docs.html.
[10] Karline Soetaert (2009)
bfPackage rootSolve : roots, gradients and steady-states in R, Centre
for Estuarine and Marine Ecology, Netherlands Institute of Ecology,
http://finzi.psych.upenn.edu/R/library/rootSolve/doc/rootSolve.pdf
207
208