Зарядов — Введение в стат пакет R


Чтобы посмотреть этот PDF файл с форматированием и разметкой, скачайте его и откройте на своем компьютере.
Ó÷åáíî-ìåòîäè÷åñêîåïîñîáèå
ÇàðÿäîâÈâàíÑåðãååâè÷
Ââåäåíèåâñòàòèñòè÷åñêèéïàêåò
R
:òèïû
ïåðåìåííûõ,ñòðóêòóðûäàííûõ,÷òåíèåè
çàïèñüèíôîðìàöèè,ãðàôèêà.
Ìîñêâà
ÈçäàòåëüñòâîÐîññèéñêîãîóíèâåðñèòåòàäðóæáûíàðîäîâ
2010
Îãëàâëåíèå
1Ââåäåíèå5
1.1Öåëüýòîéêíèãè............................5
1.2×òîèçñåáÿïðåäñòàâëÿåòïàêåò
R
.................5
1.3Èñòîðèÿñîçäàíèÿ...........................6
1.4¾Ïëþñû¿è¾ìèíóñû¿
R
.......................6
1.5Ïîëåçíûåññûëêè...........................7
1.6Áëàãîäàðíîñòè.............................8
2Íà÷àëüíûåýòàïûðàáîòûâR9
2.1Óñòàíîâêàèçàïóñêñòàòèñòè÷åñêîãîïàêåòà............9
2.1.1Óñòàíîâêàïàêåòà.......................9
2.1.2Çàïóñêïðîãðàììûèíà÷àëîðàáîòû.............10
2.1.3Ðàáîòàñîñêðèïòàìè.....................13
2.1.4Âûõîäèçïðîãðàììû,ñîõðàíåíèåäàííûõ.........14
2.2Ïîëåçíûåêîìàíäû...........................15
2.3Ïàêåòû.................................20
2.4Êëàññûîáúåêòîâ,òèïûäàííûõèñòðóêòóðûîáúåêòîââ
R
...21
2.4.1Êëàññûîáúåêòîââ
R
.....................21
2.4.2Ñïåöèàëüíûåïåðåìåííûåâ
R
................28
2.4.3Ñîçäàíèå÷èñëîâûõïîñëåäîâàòåëüíîñòåéâ
R
.......30
2.4.4Êëàññûäàííûõâ
R
......................31
3Îïåðàöèèíàäðàçëè÷íûìèïåðåìåííûìè.ÌàòåìàòèêàâR33
3.1Ïðåäèñëîâèå..............................33
3.2Ïðîñòåéøèåîïåðàöèè.........................33
3.2.1Ëîãè÷åñêèåîïåðàöèè.....................33
3.2.2Ìàòåìàòè÷åñêèåôóíêöèè..................35
3.2.3Òðèãîíîìåòðè÷åñêèåôóíêöèè................40
3.2.4Îïåðàöèèíàäêîìïëåêñíûìèïåðåìåííûìè........41
2
4Îïåðàòîðûöèêëàèóñëîâèÿ.Ñîçäàíèåñîáñòâåííûõôóíêöèé
âR43
4.1Îïåðàòîðûöèêëàèóñëîâèÿâ
R
...................43
4.1.1Îïåðàòîð
if
...........................43
4.1.2Îïåðàòîð
ifelse
........................45
4.1.3Îïåðàòîð
for
..........................46
4.1.4Îïåðàòîð
while
........................47
4.1.5Îïåðàòîðû
repeat
,
break
è
next
..............47
4.1.6Îïåðàòîð
switch
........................48
4.2Íàïèñàíèåôóíêöèéâ
R
.......................49
4.2.1Ñòàíäàðòíàÿôîðìàçàäàíèÿôóíêöèèâ
R
.........49
4.2.2Àðãóìåíò
...
..........................51
4.2.3Ôîðìàëüíûåàðãóìåíòû,ëîêàëüíûåïåðåìåííûåèñâî-
áîäíûåïåðåìåííûå......................52
4.2.4Ïîëíàÿôîðìàçàäàíèÿôóíêöèèâ
R
............53
4.2.5Ñèëüíîåïðèñâàèâàíèåâ
R
..................55
4.2.6Êîìàíäû
apply()
,
sapply()
è
lapply()
...........56
4.2.7Ïðèìåðûíàïèñàíèÿôóíêöèéâ
R
ñèñïîëüçîâàíèåì
óïðàâëÿþùèõêîíñòðóêöèé..................60
5ÊëàññûäàííûõâR62
5.1Ïðåäèñëîâèå..............................62
5.2Âåêòîðû.................................62
5.2.1Ñïîñîáûçàäàíèÿâåêòîðîââ.................62
5.2.2Ñèìâîëüíûåâåêòîðûèñòðîêè................66
5.2.3×èñëîâûåâåêòîðû.......................69
5.2.4Ëîãè÷åñêèåâåêòîðû......................76
5.2.5Çàäàíèåèì¼íýëåìåíòàìâåêòîðîâ..............77
5.2.6Âåêòîðûèèíäåêñû......................78
5.2.7Ôóíêöèè
which()
,
duplicated()
è
unique()
........82
5.3Ìàòðèöû................................84
5.3.1Çàäàíèåìàòðèöû.......................84
5.3.2Îïåðàöèèíàäìàòðèöàìè...................89
5.3.3Îïåðàöèèñèíäåêñàìè....................96
5.4Ìíîãîìåðíûåìàññèâû........................97
5.5Ñïèñêè.................................99
5.6Ôàêòîðûèòàáëèöû..........................106
5.6.1Ôàêòîðû
factor()
......................106
5.6.2Òàáëèöû
table()
......................111
5.7Òàáëèöûäàííûõ............................114
3
6ÂâîäèâûâîääàííûõâR117
6.1Ââîääàííûõâ
R
............................117
6.1.1Ôóíêöèÿ
scan()
........................117
6.1.2Ôóíêöèè
read.table()
è
read.csv()
.............119
6.2Âûâîääàííûõâ
R
...........................122
6.2.1Ôóíêöèÿ
write()
........................122
6.2.2Ôóíêöèÿ
cat()
.........................122
6.2.3Ôóíêöèè
write.table()
,
write.csv()
è
write.csv()
....123
7ÁàçîâàÿãðàôèêàâR127
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
4
Ãëàâà1
Ââåäåíèå
1.1Öåëüýòîéêíèãè
Äàííîåïîñîáèåïðåäíàçíà÷åíîäëÿñòóäåíòîâ34êóðñîâñïåöèàëüíîñòè
¾Ïðèêëàäíàÿìàòåìàòèêàèèíôîðìàòèêà¿,èçó÷àþùèõêóðñ¾Ïðàêòèêóìïî
ñòàòèñòèêå¿,àòàêæåäëÿâñåõæåëàþùèõðàçîáðàòüñÿâïðèêëàäíûõàñïåêòàõ
ìàòåìàòè÷åñêîéñòàòèñòèêèíàïðèìåðåðàáîòûâïàêåòå
R
.
1.2×òîèçñåáÿïðåäñòàâëÿåòïàêåòR
R
ýòîîäíîâðåìåííîèñâîáîäíîðàñïðîñòðàíÿåìàÿïðîãðàììíàÿñðåäàñ
îòêðûòûìêîäîì,ðàçâèâàåìàÿâðàìêàõïðîåêòà
GNU
,èÿçûêïðîãðàììèðî-
âàíèÿäëÿñòàòèñòè÷åñêîéîáðàáîòêèäàííûõèðàáîòûñãðàôèêîé.
R
ìîæíîïðèìåíÿòüâåçäå,ãäåíóæíàðàáîòàñäàííûìè.Ýòîèñàìàìàòå-
ìàòè÷åñêàÿñòàòèñòèêàâîâñåõå¼ïðèëîæåíèÿõ,èïåðâè÷íûéàíàëèçäàííûõ,
èìàòåìàòè÷åñêîåìîäåëèðîâàíèå.Îñíîâíàÿìîùü
R
ëó÷øåâñåãîïðîÿâëÿåòñÿ
èìåííîïðèñòàòèñòè÷åñêîìàíàëèçåäàííûõ:îòâû÷èñëåíèÿñðåäíèõâåëè÷èí
äîñåðü¼çíûõîïåðàöèéñâðåìåííûìèðÿäàìè.Ñïîìîùüþ
R
ìîæíîïîäãîòî-
âèòüäàííûåäëÿèññëåäîâàíèÿ,êîòîðîåìîæåòáûòüîñóùåñòâëåíîñïîìîùüþ
ðåàëèçîâàííûõâðàçëè÷íûõôóíêöèÿõñòàòèñòè÷åñêèõìåòîäîâ,àçàòåìâû-
âåñòèïîëó÷åííûåðåçóëüòàòûäëÿäàëüíåéøåãîàíàëèçà.
Ñåé÷àñïðàêòè÷åñêèâîâñåõçàïàäíîåâðîïåéñêèõèàìåðèêàíñêèõóíèâåð-
ñèòåòàõèçó÷àþòèèñïîëüçóþò
R
,åæåãîäíîèçäàþòñÿìíîãîñòðàíè÷íûåó÷åá-
íèêèèìîíîãðàôèèîòíîñèòåëüíîêàêðàáîòûññàìèìïàêåòîì
R
,òàêèåãî
ïðèìåíåíèÿïðèèññëåäîâàíèèèîáðàáîòêåäàííûõâñòàòèñòèêå,ìåäèöèíå,
ýêîëîãèè,ôèíàíñîâîìàíàëèçå,àêòóàðíîéìàòåìàòèêåèïð.Ìíîãèåêîìïà-
íèèòàêæåïðèìåíÿþò
R
,íàïðèìåðBoeing.
5
1.3Èñòîðèÿñîçäàíèÿ
R
âîçíèêêàêñâîáîäíûéàíàëîãñðåäû
S-PLUS
,êîòîðàÿâñâîþî÷åðåäü
ÿâëÿåòñÿêîììåð÷åñêîéðåàëèçàöèåéÿçûêàðàñ÷¼òîâ
S
.
ßçûê
S
áûëðàçðàáîòàíâ1976ãîäóâêîìïàíèèAT
&
TLabs.Ïåðâàÿðåà-
ëèçàöèÿ
S
áûëàíàïèñàíàíàFORTRANèðàáîòàëàïîäóïðàâëåíèåìîïåðà-
öèîííîéñèñòåìûGCOS.Â1980ãîäóðåàëèçàöèÿáûëàïåðåïèñàíàïîäUNIX.
Èìåííîòîãäàâíàó÷íîéñðåäåèñòàëðàñïðîñòðàíÿòüñÿ
S
.Â1988ãîäóâûøëà
òðåòüÿâåðñèÿ,êîììåð÷åñêàÿðåàëèçàöèÿêîòîðîéñòàëàíàçûâàòüñÿ
S-PLUS
.
Äîâîëüíîâûñîêàÿñòîèìîñòüïðåäëàãàåìîãîêîììåð÷åñêîãîñòàòèñòè÷åñêîãî
ïàêåòàèïðèâåëàêâîçíèêíîâåíèþ
R
.
Âàâãóñòå1993äâîåíîâîçåëàíäñêèõó÷¼íûõ(RobertGentlemanèRoss
Ihaka,StatisticsDepartment,AucklandUniversity)àíîíñèðîâàëèñâîþðàçðà-
áîòêóïîäíàçâàíèåì
R
.Ýòîáûëàíîâàÿðåàëèçàöèÿÿçûêà
S
,îòëè÷àþùàÿñÿ
îò
S-PLUS
ðÿäîìäåòàëåé,íàïðèìåð,ðàáîòîéñïàìÿòüþ,îáðàùåíèåìñãëî-
áàëüíûìèèëîêàëüíûìèïåðåìåííûìè.
Ïîíà÷àëóïðîåêòðàçâèâàëñÿäîâîëüíîìåäëåííî,íîñïîÿâëåíèåìâîçìîæ-
íîñòèäîâîëüíîë¼ãêîãîíàïèñàíèÿäîïîëíåíèé(ïàêåòîâ)âñ¼áîëüøååêîëè÷å-
ñòâîëþäåéñòàëîïåðåõîäèòüñ
S-PLUS
íà
R
.Ïîñëåóñòðàíåíèÿïðîáëåì,
ñâÿçàííûõñïàìÿòüþ,ñðåäèïîëüçîâàòåëåé
R
ñòàëèïîÿâëÿòüñÿè¾ïîêëîííè-
êè¿äðóãèõïàêåòîâ(
SAS
,
Stata
,
SYSTAT
,
SPSS
).Êîëè÷åñòâîêíèãïî
R
çàïîñëåäíèåãîäûçíà÷èòåëüíîâûðîñëî,à÷èñëîäîïîëíèòåëüíûõêáàçîâîé
âåðñèèïàêåòîâñòàëîáîëüøåäâóõòûñÿ÷(íàíà÷àëîÿíâàðÿ2010ãîäà÷èñëî
ïàêåòîâñîñòàâèëî2142).
Äîïîëíèòåëüíóþïîïóëÿðíîñòü
R
ïðèíåñëîñîçäàíèåöåíòðàëüíîéñèñòå-
ìûõðàíåíèÿèðàñïðîñòðàíåíèÿïàêåòîâ
CRAN
(ComprehensiveRArchive
äëÿáîëååñåðü¼çíîéðàáîòûâñåãäàìîæíîäîïîëíèòåëüíîóñòàíîâèòü
âñïîìîãàòåëüíûåïàêåòûñíåîáõîäèìûìèôóíêöèÿìè;
íàäàííûéìîìåíòðàçðàáîòàíûïàêåòû,ïðèìåíèìûåïðàêòè÷åñêèâîâ
âñåõîáëàñòÿõçíàíèÿ,ãäåèñïîëüçóåòñÿñòàòèñòèêà;
ìîæíîðàáîòàòüñáîëüøèìèìàññèâàìèäàííûõ(íåñêîëüêîñîòåíòûñÿ÷
íàáëþäåíèé);
âñòðîåííàÿñèñòåìàïîìîùèèïîäñêàçîê;
õîðîøèåãðàôè÷åñêèåâîçìîæíîñòèïðåäñòàâëåíèÿðåçóëüòàòîâèññëåäî-
âàíèé;
âîçìîæíîñòüñàìîñòîÿòåëüíîãîíàïèñàíèÿíåîáõîäèìûõôóíêöèé;
ìíîãîñâîáîäíîéëèòåðàòóðûïî
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
;
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://nzi.psych.upenn.edu/nmz.htmlïîèñêâìàòåðèàëàõïî
R
;
Ãëàâà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
ïîäWindows
2
äîâîëüíîòèïè÷íà.Ù¼ëêàåìïîñêà÷àííîìó
exe-ôàéëó.Ïîõîäóóñòàíîâêèçàäà¼òñÿðÿäâîïðîñîâ.
Ïåðâûéâîïðîñÿçûêâîâðåìÿóñòàíîâêè.Âûáèðàåìðóññêèé(â
ïðèíöèïå,îíèïðåäëàãàåòñÿ).
Äàëååïðåäëàãàåòñÿçàêðûòüâñåðàáî÷èåïðèëîæåíèÿïåðåäíà÷à-
ëîìóñòàíîâêè.Ìîæíîòàêèñäåëàòü(àìîæíîèíåäåëàòü).Íàæè-
ìàåìêíîïêó¾Äàëåå¿.
1
Êàêïðàâèëî,âíîâîéâåðñèèóñòðàíÿþòñÿíåäîñòàòêèïðåäûäóùèõ.Íî,æåëàòåëüíî
óñòàíîâèòüíåñêîëüêîâåðñèé(òåêóùóþèïðåäûäóùóþ),÷òîáûïðîâåðèòü,êàêàÿèçíèõïî
ôóíêöèîíàëüíîñòèáîëüøåïîäõîäèò.Êïðèìåðó,ââåðñèèR-2.9.2ïîñðàâíåíèþñR-2.7.2
åñòüíåäîñòàòîêäëÿïîëíîöåííîéðàáîòûâïîäîêíàõíåîáõîäèìîèõðàçâîðà÷èâàòüíà
âåñüýêðàí
2
Âäàëüíåéøåìðå÷üïîéä¼òîðàáîòåèìåííîâýòîéîïåðàöèîííîéñèñòåìå
9
Çàòåìâûâîäèòñÿëèöåíçèîííîåñîãëàøåíèå,åñëèñíèìñîãëàñíû,
òîñíîâàæì¼ìíàêíîïêó¾Äàëåå¿.
Âûáèðàåìïàïêó,ãäåáóäåòóñòàíîâëåíïàêåò.(Ïîóìîë÷àíèþñî-
çäà¼òñÿäèðåêòîðèÿ¾R¿ñïîääèðåêòîðèåé¾R-2.10.1¿â¾Program
Files¿)
Äàëååïðåäëàãàþòñÿâàðèàíòûóñòàíîâêè.Âûáèðàåìâñåpdf-ôàéëû
ñðóêîâîäñòâîìïîëüçîâàòåëÿ(Manual).Ïîëíûéâàðèàíòóñòàíîâêè
çàéì¼òïðèìåðíî65ìåãàáàéòíàæ¼ñòêîìäèñêå.
Ñîçäà¼ìÿðëûêèâïðåäëàãàåìîéïàïêå(èëèâûáèðàåìñàìè)ìåíþ
¾Ïóñê¿.
Íàñëåäóþùåìøàãåâûáèðàåìïðåäëàãàåìûåâàðèàíòû(ñîçäàíèå
ÿðëûêàíà¾Ðàáî÷åìñòîëå¿èïðèâÿçûâàíèåê
R
ôàéëîâñðàñøè-
ðåíèåì.Rdata).

Ñàìàóñòàíîâêàçàéì¼òáóêâàëüíîïàðóìèíóò.Ïîå¼îêîí÷àíèþíàðà-
áî÷åìñòîëåïîÿâèòñÿÿðëûêñóêàçàíèåìâåðñèèïàêåòà.
2.1.2Çàïóñêïðîãðàììûèíà÷àëîðàáîòû
Ïîñëåóñòàíîâêèíàðàáî÷åìñòîëåèìååòñÿÿðëûê
R
,ù¼ëêàÿïîêîòîðî-
ìó,çàïóñêàåìïðîãðàììó.Ïîÿâèòñÿîñíîâíîåîêíîïðîãðàììû,ñîäåðæàùåå
ïîäîêíîRConsole.Êàæäàÿñåññèÿâ
R
íà÷èíàåòñÿñîñëåäóþùèõñòðîê
Rversion2.12.1(2010-12-16)
Copyright(C)2010TheRFoundationforStatisticalComputing
ISBN3-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
âûñòóïàþòëèáî=,ëèáî-(ñîñòîÿ-
ùèéèçäâóõñèìâîëîâ:è-)ïðèñâàèâàíèåñïðàâà,ëèáî&#x-257;&#x-257;&#x--25;-25;-11;&#x-1-2;V,-;ɗ-;ɗ-;-ïðèñâàèâàíèå
ñëåâà,êàêïîîäèíî÷êå,òàêèâïîñëåäîâàòåëüíîñòè.
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)
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}RDocumentation
ListObjects
Description
thecurrentenvironment.Althoughcallednameforbackcompatibility,
infactthisargumentcanspecifytheenvironmentinanyform;see
myfunc()
#definealocalvariableinsidemyfunc
myfunc-function(){y-1;ls()}
myfunc()#shows"y"
Ñòðóêòóðàñïðàâî÷íîéèíôîðìàöèè:

íàïåðâîéñòðîêåïðèâåäåíûíàçâàíèåôóíêöèèèïàêåò,âêîòîðîéîíà
íàõîäèòñÿ;

Description
îïèñàíèåôóíêöèè,äëÿ÷åãîîíàèñïîëüçóåòñÿ,÷òîîíà
äåëàåò;

Usage
ïðàâèëüíîåíàïèñàíèåôóíêöèèñîâñåìèå¼àðãóìåíòàìè;

Arguments
ïîäðîáíîåîïèñàíèåàðãóìåíòîâôóíêöèè,êàêèåçíà÷åíèÿ
îíèìîãóòïðèíèìàòü;

6
Êàâû÷êèîáÿçàòåëüíû
17
ReferenceDistribution
MASS::mvrnormSimulatefromaMultivariateNormal
Distribution
Matrix::ddenseMatrix-class
VirtualClass"ddenseMatrix"ofNumeric
DenseMatrices
Matrix::dgeMatrix-class
Class"dgeMatrix"ofDenseNumeric(S4
Class)Matrices
Matrix::diagonalMatrix-class
Class"diagonalMatrix"ofDiagonal
Matrices
Matrix::dspMatrix-class
ôóíêöèé,ñîäåðæàùèõóêàçàííîåâêàâû÷êàõèìÿ.
Ïðèìåð4.
Êîìàíäà
apropos("èìÿ")
.
�apropos("norm")
[1]"dlnorm""dnorm""normalizePath""plnorm"
[5]"pnorm""qlnorm""qnorm""qqnorm"
[9]"qqnorm.default""rlnorm""rnorm"
Ïîëó÷èëèñïèñîêèç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
ïî-
ñëåäíèõêîìàíä.

1.Ìîæíîçàéòèâ
Ïàêåòû
êîìàíäíîãîìåíþèâûáðàòü
Óñòàíîâèòüïà-
êåò(û)
.Ñíà÷àëàïðåäëîæàòâûáðàòüçåðêàëî
CRAN
ñòðàíó,îòêóäà
áóäóòñêà÷åíïàêåò.Âñëó÷àåóäà÷èïîÿâèòñÿñïèñîêäîñòóïíûõïàêåòîâ,
îòêóäàíóæíîâûáðàòüíóæíûé(íóæíûå)èíàæàòüêíîïêó
ÎÊ
.Äàëåå
ïðåäëîæàòñîçäàòüïåðñîíàëüíóþáèáëèîòåêóäëÿñêà÷åííûõïàêåòîâ.
Ïîñëåýòîãîíóæíûéïàêåòóñòàíîâëåí.Äëÿåãîèíèöèàëèçàöèèíóæíà
êîìàíäà
library(èìÿ_ïàêåòà)
.
2.Åñëèïîêàêèì-ëèáîïðè÷èíàì
R
ñàìîñòîÿòåëüíîíåìîæåòâûéòèíàçåð-
êàëà
CRAN
,òîìîæíîíåïîñðåäñòâåííîçàéòèíàñàéòhttp://cran.gis-
lab.info/web/packages/èâûáðàòüíóæíûéïàêåòäëÿóñòàíîâêè(Ïðåèìó-
ùåñòâîìîæíîïðåäâàðèòåëüíîïîñìîòðåòüèíôîðìàöèþïîïàêåòó,òàê
êêàæäîìóïàêåòóïðèëàãàåòñÿîïèñàíèåâõîäÿùèõâíåãîôóíêöèé).Âñå
ïàêåòûçààðõèâèðîâàíû.Ñêà÷èâàåìíóæíûéââûáðàííóþïàïêó.Ïîñëå
ýòîãîçàõîäèìâêîìàíäíîììåíþâ
Ïàêåòû
èâûáèðàåì
Óñòàíîâèòü
ïàêåòûèçëîêàëüíûõzip-ôàéëîâ
.Âîòêðûâøåìñÿîêíåâûáèðàåì
ïàïêóñíóæíûìïàêåòîì,âûáèðàåìïàêåòèâñ¼,îíçàãðóæåí.Äëÿ
åãîèíèöèàëèçàöèèïåðåäíåïîñðåäñòâåííûìèñïîëüçîâàíèåìñíîâàíóæ-
íàêîìàíäà
library(èìÿ_ïàêåòà)
.
Êàêâèäíî,óñòàíîâêàíîâûõïàêåòîââ
R
äîâîëüíîïðîñòà.
2.4Êëàññûîáúåêòîâ,òèïûäàííûõè
ñòðóêòóðûîáúåêòîââR
2.4.1Êëàññûîáúåêòîââ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]00000
Âðåçóëüòàòåñîçäàííóëåâîéâåêòîðòèïà
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+0i0+0i0+0i0+0i0+0i
23
ñîçäà¼òíóëåâîéêîìïëåêñíûéâåêòîð
9
.Âêà÷åñòâåàðãóìåíòàâçÿòîçíà÷åíèå
length.out=5
.Ïàðàìåòð
length.out
çàäà¼òäëèíóâûâîäèìîãîâåêòîðà(÷èñ-
ëîâîéïàðàìåòð),
real
è
imaginary
çàäàþòäåéñòâèòåëüíóþèìíèìóþ÷àñòè
êîìïëåêñíîãîâåêòîðà(îáàýòèàðãóìåíòà-÷èñëîâûåâåêòîðà).
�z=complex(5,c(1:5),c(6:10));z
[1]1+6i2+7i3+8i4+9i5+10i
Íàçâàíèÿàðãóìåíòîâôóíêöèéâ
R
ìîæíîîïóñêàòüèïèñàòüòîëüêîèõçíà-
÷åíèÿ,íîýòîòîëüêîâòîìñëó÷àå,åñëèàðãóìåíòûèäóòïîïîðÿäêó.
Ïàðàìåòðû
modulus
è
argument
àëüòåðíàòèâíûéñïîñîáçàäàíèÿêîì-
ïëåêñíîãîâåêòîðà÷åðåçåãîìîäóëü

èàðãóìåíò
'
10
.
�z=complex(5,modulus=c(1:5),argument=c(6:10));z
[1]0.9601703-0.2794155i1.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=
p

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
Áîëååïîäðîáíîîâåêòîðàõáóäåòèçëîæåíîâðàçäåëå5.2ãëàâû5.
10
Íàïîìíèìïðåäñòàâëåíèåêîìïëåêñíîãî÷èñëà
z
:
z
=
x
+
iy
,ãäå
x
=

cos(
'
)
,
y
=

sin(
'
)
24
Ïåðåâîäêîìïëåêñíûõîáúåêòîââäåéñòâèòåëüíûåìîæíîñäåëàòü
ïðèïîìîùèôóíêöèè
as.numeric(èìÿ_îáúåêòà)
,ëèáîïðèïîìîùè
as.double(èìÿ_îáúåêòà)
.
�z=2.3-1i*6
�x=as.numeric(z)
Warningmessage:
ìíèìûå÷àñòèóáðàíûïðèïðåîáðàçîâàíèè
�y=as.double(z)
Warningmessage:
ìíèìûå÷àñòèóáðàíûïðèïðåîáðàçîâàíèè
�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]FALSEFALSEFALSEFALSE
Êàêâèäíîèçïðèìåðà,ïðèîáðàùåíèèêêîìàíäå
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
Â
R
ñóùåñòâóåòðÿäîñîáûõîáúåêòîâ:
Inf
áåñêîíå÷íîñòü:ïîëîæèòåëüíàÿ(
+
1

Inf
)èîòðèöàòåëüíàÿ(
�1

-Inf
);
NA
¾îòñóòñòâóþùååçíà÷åíèå¿(NotAvailable);
NaN
¾íå÷èñëî¿(NotaNumber);
NULL
¾íè÷òî¿.
28
Âñåýòèîáúåêòûìîæíîèñïîëüçîâàòüâëþáûõâûðàæåíèÿõ.Ðàññìîòðèìèõ
áîëååïîäðîáíî.
Inf
ïîÿâëÿåòñÿïðèïåðåïîëíåíèèèâðåçóëüòàòåîïåðàöèéâèäà
a
0
,ãäå
a�
0
.
�x=5/0;x
[1]Inf
�y=log(0);y
[1]-Inf
Ïðîâåðèòüîáúåêòíàêîíå÷íîñòü(áåñêîíå÷íîñòü)ìîæíîïðèïîìîùèêîìàíä
is.nite()
(
is.innite()
):
�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Ñîçäàíèå÷èñëîâûõïîñëåäîâàòåëüíîñòåéâ
R
Âýòîìðàçäåëåðàçáåð¼ìðàçëè÷íûåâàðèàíòûçàäàíèÿ÷èñëîâûõïîñëåäî-
âàòåëüíîñòåé
12
.
Åñëèýëåìåíòû÷èñëîâîéïîñëåäîâàòåëüíîñòèîòëè÷àþòñÿäðóãîòäðóãàíà
åäèíèöó(âîçðàñòàþùàÿïîñëåäîâàòåëüíîñòü),èëèíà

1
(óáûâàþùàÿïîñëå-
äîâàòåëüíîñòü),òîìîæíîçàäàòüòîëüêîíà÷àëüíîå
a
èêîíå÷íîå
b
çíà÷åíèÿ
èñêîìîéïîñëåäîâàòåëüíîñòè
a:b
.
�x=2:10;x
[1]2345678910
�y=10:2;y
[1]1098765432
Âòîðîéâàðèàíòïðèïîìîùèêîìàíäû
seq()
.Âàðèàíòûèñïîëüçîâàíèÿ:

seq(from,to)
àíàëîãè÷íîâàðèàíòó
a:b
.
12
Çàäàíèå÷èñëîâûõâåêòîðîâðàññìîòðåíîâ5.2.3ãëàâû5
30
�x=seq(from=-5,to=5);x
[1]-5-4-3-2-1012345
�x=seq(-5,5);x
[1]-5-4-3-2-1012345

seq(from,to,by=)
çàäàþòñÿíà÷àëüíîå,êîíå÷íîåçíà÷åíèÿèøàã
ïîñëåäîâàòåëüíîñòè.
�x=seq(-5,5,by=0.5);x
[1]-5.0-4.5-4.0-3.5-3.0-2.5-2.0-1.5-1.0-0.50.00.51.01.52.0
[16]2.53.03.54.04.55.0
�x=seq(-5,5,0.5);x
[1]-5.0-4.5-4.0-3.5-3.0-2.5-2.0-1.5-1.0-0.50.00.51.01.52.0
[16]2.53.03.54.04.55.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.55555560.5555556
[7]1.66666672.77777783.88888895.0000000

seq(ñ)
ïîñëåäîâàòåëüíîñòüñòðîèòñÿîò
1
äî
c
.
�x=seq(6);x
[1]123456
�y=seq(-4);y
[1]10-1-2-3-4
Äðóãèåâàðèàíòûçàäàíèÿ÷èñëîâûõïîñëåäîâàòåëüíîñòåé(òî÷íååãîâîðÿ,
÷èñëîâûõâåêòîðîâ)áóäóòïðåäñòàâëåíûâðàçäåëå5.2.3ãëàâû5.
2.4.4ÊëàññûäàííûõâR
Ïîñëåäíèéðàçäåëýòîéãëàâûïîñâÿù¼íñòðóêòóðàìäàííûõâ
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
j
y
ïîýëåìåíòíîåëîãè÷åñêîå
ÈËÈ
x
jj
y
ëîãè÷åñêîå
ÈËÈ
òîëüêîäëÿïåðâûõýëåìåíòîâ
x
è
y
xor(
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';�xy
[1]FALSE
��x=10000;y='a';yx
[1]TRUE
Òàêæåìîæíîñðàâíèâàòüèëîãè÷åñêèåïåðåìåííûå:
�x=F;y=T
�xy
[1]TRUE
Ëîãè÷åñêèåïåðåìåííûåìîæíîñðàâíèâàòüêàêñ÷èñëîâûìè,òàêèññèìâîëü-
íûìè.Ïðèýòîìëîãè÷åñêàÿïåðåìåííàÿïåðåâîäèòñÿëèáîâ÷èñëîâîéòèï,
ëèáîâñèìâîëüíûé.
Ïðèñðàâíåíèèëþáîãîîáúåêòàñ
NA
èëè
NaN
ðåçóëüòàòîìáóäåò
NA
.
34
�x=10;y=NA
��xy
[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)
âû÷èñëåíèå
e
x
;

exp1m(x)
äëÿ
j
x
j
1
íàõîæäåíèå
e
x

1
.
Â
R
ðåàëèçîâàíîíåñêîëüêîëîãàðèôìè÷åñêèõôóíêöèé:

log(x)
è
logb(x)
íàòóðàëüíûéëîãàðèôì÷èñëà
x
;

log10(x)
ëîãàðèôìïîäåñÿòè÷íîìóîñíîâàíèþ;

log2(x)
ëîãàðèôìïîîñíîâàíèþ2;

log1p(x)
âû÷èñëåíèåëîãàðèôìà
ln(1+
x
)
äëÿ
j
x
j
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
);

oor(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
Ïðåäóïðåæäåíèå
Insqrt(y):ñîçäàíûNaN
×òîáûíàéòèêâàäðàòíûéêîðåíüîòðèöàòåëüíîãî÷èñëà
x
,íóæíî
x
îïðåäåëèòü
êàêêîìïëåêñíîå÷èñëî:
�y=as.complex(y);sqrt(y)
[1]0+2i
Ñïåöèàëüíûåôóíêöèè
Êñïåöèàëüíûìôóíêöèÿìîòíîñÿòñÿ:áåòà-ôóíêöèÿèå¼ëîãàðèôì,ãàììà-
ôóíêöèÿ,ëîãàðèôììîäóëÿãàììà-ôóíêöèè,ïðîèçâîäíûåãàììà-ôóíêöèè,
ôàêòîðèàëè÷èñëîñî÷åòàíèé.

gamma(x)
ãàììà-ôóíêöèÿ
�(
x
)
,
�(
x
)=
1
R
0
t
x

1
e

t
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

�(
a
+
b
)
;
ïàðàìåòðû
a
è
b
áîëüøåíóëÿ.

1
Ïðè
k
0
âûäà¼òñÿíîëü,íåöåëîå
k
îêðóãëÿåòñÿäîöåëîãî÷èñëà
39

lchoose(n,k)
íàòóðàëüíûéëîãàðèôì÷èñëàñî÷åòàíèé
C
k
n
.

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
;

Mod(z)
ìîäóëü

=
p
x
2
+
y
2
êîìïëåêñíîãî÷èñëà
z
=
x
+i
y
;

Arg(z)
àðãóìåíò
'
êîìïëåêñíîãî÷èñëà
z
=
x
+i
y
,ãäå
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
.
2
Ñìîòðèðàçäåë5.2.3ãëàâû5
42
Ãëàâà4
Îïåðàòîðûöèêëàèóñëîâèÿ.
Ñîçäàíèåñîáñòâåííûõôóíêöèéâ
R
Íåñìîòðÿíàòî,÷òîâðàìêàõïðîåêòà
R
íàïèñàíîî÷åíüáîëüøîå÷èñëîïà-
êåòîâñðàçíîîáðàçíûìèôóíêöèÿìè,êàæäûéïîëüçîâàòåëüìîæåòñîçäàâàòü
ñâîèñîáñòâåííûåôóíêöèè,èñïîëüçóÿâíèõîïåðàòîðûóïðàâëåíèÿöèêëû
èóñëîâíûåîïåðàòîðû.
4.1ÎïåðàòîðûöèêëàèóñëîâèÿâR
Âýòîé÷àñòèðàññìîòðèìñòðóêòóðû,ïîçâîëÿþùèåóïðàâëÿòüîáðàáîòêîé
äàííûõ,òàêíàçûâàåìûåîïåðàòîðûóïðàâëåíèÿöèêëûèóñëîâíûåîïåðà-
òîðû.
4.1.1Îïåðàòîðif
Íà÷í¼ìñóñëîâíîãîîïåðàòîðà
if
,êîòîðûéâ
R
èìååòäâåôîðìûçàïèñè:
êðàòêóþ(áåçðåàëèçàöèèàëüòåðíàòèâû)èïîëíóþ(âîçìîæíîñòüðåàëèçàöèè
àëüòåðíàòèâíûõîïåðàöèé).
Êðàòêèéâàðèàíòîïåðàòîðàif
if(óñëîâèå)âûðàæåíèå
óñëîâèå
ëþáîéîïåðàòîðóñëîâèÿ(

,

,

=
,

=
,
==
,
!=
),ðåçóëüòàòîì
âûïîëíåíèÿêîòîðîãîÿâëÿåòñÿëîãè÷åñêèéâåêòîðåäèíè÷íûéäëèíû.Åñëè
çíà÷åíèåâåêòîðà
TRUE
,òîâûïîëíÿåòñÿ
âûðàæåíèå
.
43
âûðàæåíèå
îäíîèëèíåñêîëüêîâûðàæåíèé,âûïîëíÿåìûõâñëó÷àå
âåðíîñòèóñëîâèÿ.Åñëèçàäàíîíåñêîëüêîâûðàæåíèé,òîîíèäîëæíûáûòü
çàêëþ÷åíûâôèãóðíûåñêîáêè
fg
èðàçäåëÿòüñÿòî÷êîéñçàïÿòîé(åñëèíà
îäíîéñòðîêå)
1
.
Îïåðàòîðâîçâðàùàåòçíà÷åíèå
âûðàæåíèÿ
âñëó÷àåâåðíîñòèóñëîâèÿ
èëèíè÷åãîíåâîçâðàùàåò(
NULL
).
�x=5;y=4
��if(xy){z=x+y;z}
[1]9
Óñëîâèåâûïîëíåíîèâðåçóëüòàòåïîëó÷åíîçíà÷åíèå
z
.
�x=5;y=4
�if(xy)w=x+y
�w
Îøèáêà:îáúåêò'w'íåíàéäåí
Âýòîìïðèìåðåóñëîâèåíåâûïîëíÿåòñÿ,ñëåäîâàòåëüíî,çàäàííîåâûðàæåíèå
íåáóäåòïîñ÷èòàíîèîáúåêò
w
íåñîçäà¼òñÿ.
Ïðèìåð7.
Ðàññìîòðèìåù¼îäèíïðèìåð,âêîòîðîì
x
è
y
ÿâëÿþòñÿâåêòî-
ðàìèîäèíàêîâîéäëèíû(10).Çàäàäèìóñëîâèå:åñëè
x
íåðàâåí
y
,òîáåð¼òñÿ
îòíîøåíèå
x=y
.Âðåçóëüòàòåäîëæíûïîëó÷èòüâåêòîð,÷üÿðàçìåðíîñòü
ñîâïàäàåòñðàçìåðíîñòüþèñõîäíûõâåêòîðîâ.Íîèçêàêèõýëåìåíòîâîí
ñîñòîèò?
�x=1:10;y=10;1
�x
[1]12345678910
�y
[1]10987654321
�if(xy){x/y}
[1]0.10000000.22222220.37500000.57142860.83333331.2000000
[7]1.75000002.66666674.500000010.0000000
Ïîëó÷àåòñÿ,÷òîâçÿòîîòíîøåíèåòîëüêîïåðâûõýëåìåíòîââåêòîðîâ.
Êðîìåòîãî,âûâîäèòñÿïðåäóïðåæäåíèå
Ïðåäóïðåæäåíèå
Inif(x!=y){:
äëèíàóñëîâèÿ�1,áóäåòèñïîëüçîâàíòîëüêîïåðâûéýëåìåíò
Òàêèìîáðàçîì,äëèíàóñëîâèÿäîëæíàáûòüðàâíîéåäèíèöå.
1
íàïîìíèì,÷òîâñëó÷àåáëîêàâûðàæåíèéâûâîäèòñÿðåçóëüòàòïîñëåäíåãîèçíèõ
44
Ïîëíûéâàðèàíòîïåðàòîðàif
if(óñëîâèå)âûðàæåíèå1elseâûðàæåíèå2
Åñëèóñëîâèåâûïîëíÿåòñÿ,òîâû÷èñëÿåòñÿâûðàæåíèå1(èëèáëîêâûðàæå-
íèé),âïðîòèâíîìñëó÷àåâûðàæåíèå2(èëèáëîêâûðàæåíèé)
2
.
�x=5;y=4
�if(xy){x+y}else{x-y}
[1]1
4.1.2Îïåðàòîðifelse
Ôóíêöèÿ
ifelse(óñëîâèå,yes,no)
ïîçâîëÿåòïåðåìåííîéâçàâèñèìîñòèîòâûïîëíåíèÿ(íåâûïîëíåíèÿ)íåêîòî-
ðîãîóñëîâèÿïðèíèìàòüðàçëè÷íûåçíà÷åíèÿ.Îòëè÷èåîòîïåðàòîðà
if
ñîñòî-
èòâòîì,÷òîçäåñüóñëîâèåÿâëÿåòñÿëîãè÷åñêèìâåêòîðîìëþáîéçàäàííîé
ðàçìåðíîñòè(çàâèñèòîòðàçìåðíîñòèñðàâíèâàåìûõîáúåêòîâ).Ïîðÿäêîâûé
íîìåðýëåìåíòàëîãè÷åñêîãîâåêòîðàèåãîçíà÷åíèåîïðåäåëÿåòêàêîéýëåìåíò
âåêòîðà
yes
èëè
no
áåð¼òñÿíîâîéïåðåìåííîé.
Ïðèìåð8.
Ñîçäàäèìäâàâåêòîðà
x
è
y
îäèíàêîâîéäëèíûèïðèñâîèìïåðå-
ìåííîé
z
ëèáîñîçíàêîì
+
íîìåðñîâïàäàþùèõýëåìåíòîâ,ëèáîñîçíàêîì

íîìåðíåñîâïàäàþùèõýëåìåíòîâ.
�x=c(1,3,1,5,1,7,1,9)
�y=c(2,3,4,5,2,7,1,8)
�z=ifelse(x==y,1:10,(-1):(-10))
�z
[1]-12-34-567-8
Äðóãîéïðèìåð.Ñîçäàäèìïîñëåäîâàòåëüíîñòü÷èñåëîò
6
äî

4
èâû÷èñëèì
êîðåíüêâàäðàòíûé.
�x-c(6:-4)
�sqrt(x)
[1]2.4494902.2360682.0000001.7320511.4142141.0000000.000000
[8]NaNNaNNaNNaN
Ïðåäóïðåæäåíèå
Insqrt(x):ñîçäàíûNaN
2
ñì.òàêæåïðèìåð9
45
Ïðèèçâëå÷åíèèêâàäðàòíîãîêîðíÿèçîòðèöàòåëüíûõ÷èñåëáûëèñîçäàíû
NaN
èâûâåäåíîïðåäóïðåæäåíèå.Ïîïðîáóåìâû÷èñëèòüêîðåíüòàê,÷òîáû
íåáûëîïðåäóïðåæäåíèÿ.
�sqrt(ifelse(x�=0,x,NA))
[1]2.4494902.2360682.0000001.7320511.4142141.0000000.000000
[8]NANANANA
Åù¼îäèíïðèìåð:
�x=rep(c(1,2,3),3)
�x=matrix(x,3,3)
�z=cos(ifelse(1x,x*pi,x*pi/2))
�z
[,1][,2][,3]
[1,]6.123032e-176.123032e-176.123032e-17
[2,]1.000000e+001.000000e+001.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]12345678910
�y
[1]10987654321
�w=vector(length=10,mode='numeric')
�for(iin1:10)
+{if(x[i]y[i]){w[i]=x[i]/y[i]}
+else{w[i]=x[i]*y[i]}
+}
�w
[1]0.10000000.22222220.37500000.57142860.833333330.0000000
[7]28.000000024.000000018.000000010.0000000
46
Âöèêëåìûçàäàëèóñëîâíûéîïåðàòîð,âûïîëíåíèåêîòîðîãîîçíà÷àåò,÷òî
ýëåìåíòâåêòîðà
w
ðàâåíîòíîøåíèþ
x=y
,íåâûïîëíåíèåóñëîâèÿïðèâîäèòê
ïîÿâëåíèþýëåìåíòà
w
=
xy
.Îòìåòèì,÷òîäîöèêëàíóæíîçàäàòüâåêòîð
w
,
èíà÷åáóäåòâûäàíîñîîáùåíèåîáîøèáêå.
Åù¼îäèíïðèìåð.Ñíîâàèñïîëüçóåìâåêòîðà
x
è
y
èçïðèìåðà7,íîòåïåðü
w
ýòîïðîñòî÷èñëî.
�for(iin1: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(iinc){m=i}
m
Îøèáêà:Îáúåêò'm'íåíàéäåí
4.1.4Îïåðàòîðwhile
Åù¼îäèíîïåðàòîðöèêëà.
while(óñëîâèå)âûðàæåíèå
Ïîêàâûïîëíÿåòñÿóñëîâèåâû÷èñëÿåòñÿâûðàæåíèå,êàêòîëüêîðåçóëüòàòîì
óñëîâèÿñòàíîâèòñÿ
FALSE
,âûõîäèìèçöèêëà.
�x=-10
�while(x0){z=x;x=x+1}
�z
[1]-1
4.1.5Îïåðàòîðûrepeat,breakènext
Îïåðàòîð
repeat
repeatâûðàæåíèå
47
ñîçäà¼òáåñêîíå÷íûéöèêë,âêîòîðîìâû÷èñëÿåòñÿâûðàæåíèå(èëèáëîêâû-
ðàæåíèé).Äëÿâûõîäàèçýòîãîöèêëàíóæíîèñïîëüçîâàòü
break
,àòàêæå
óñëîâíûéîïåðàòîðâêà÷åñòâåîäíîãîèçâûðàæåíèé.
Ïðèìåð10.
Ïîñòðîèìöèêëïðèïîìîùèîïåðàòîðà
repeat
,ïðèïîìîùè
óñëîâíîãîîïåðàòîðà
if
è
break
çàäàäèìâûõîä.Ðåçóëüòàòîìðàáîòûîïåðà-
òîðà
repeat
áóäåòâû÷èñëåíèåíàòóðàëüíîãîëîãàðèôìààáñîëþòíîãîçíà÷å-
íèÿïåðåìåííîé
t
.
�t=-10
�repeat{
+if�(t0)break
+f=log(abs(t))
+t=t+1}
�f
[1]-Inf
Êàêòîëüêî
t
ñòàëîáîëüøå
0
âûõîäèìèçöèêëàñïîìîùüþîïåðàòîðà
break
èïîëó÷àåìïîñëåäíååâû÷èñëåííîåçíà÷åíèå
f
=ln
j
t
j
,ðàâíîå
f
=ln0=
�1
.
Îïåðàòîð
next
êàêè
break
ìîæåòïðèìåíÿòüñÿòîëüêîâíóòðèöèêëîâ.Îòëè÷èåîò
break
ñîñòîèòâòîì,÷òîïðîèñõîäèòíåïðåðûâàíèåöèêëà,àïåðåõîäíàñëåäóþùèé
âèòîê.
4.1.6Îïåðàòîðswitch
Îïåðàòîð
switch(óïðàâëÿþùååâûðàæåíèå,àëüòåðíàòèâíûåäåéñòâèÿ)
ïîçâîëÿåòâûïîëíÿòüîäíóèçíåñêîëüêèõîïåðàöèéâçàâèñèìîñòèîòðåçóëü-
òàòîâóïðàâëÿþùåãîâûðàæåíèÿ.
Óïðàâëÿþùååâûðàæåíèåâîçâðàùàåò:

ëèáîöåëîå÷èñëî(îò1äî÷èñëààëüòåðíàòèâ),êîòîðîåÿâëÿåòñÿíîìåðîì
âûïîëíÿåìîãîäåéñòâèÿ;

ëèáîñèìâîëüíóþïåðåìåííóþ(ñòðîêó),ñîîòâåòñòâóþùóþèìåíèâûïîë-
íÿåìîéîïåðàöèè.
48
Åñëèâîçâðàùàåìîåêîíòðîëüíûìâûðàæåíèåìçíà÷åíèåíåñîîòâåòñòâóåòíè
íîìåðóâûïîëíÿåìîéîïåðàöèè,íèèìåíè,òîðåçóëüòàòîìîïåðàòîðà
switch
áóäåò
NULL
.
Îïåðàòîð
switch
íåÿâëÿåòñÿñàìîñòîÿòåëüíûì,èñïîëüçóåòñÿëèáîâíóòðè
ôóíêöèé,ëèáîâíóòðèäðóãèõóïðàâëÿþùèõêîíñòðóêöèé.
Ïðèìåð11.
Ñîçäàäèì÷èñëîâîéâåêòîð
x
äëèíû5,ýëåìåíòûêîòîðîãîïðè-
íèìàþòçíà÷åíèÿâçàâèñèìîñòèîòçíà÷åíèÿ
i
ëèáî
cos

,ëèáî
e
,ëèáî
log
2
(4)
,ëèáî
lg
10
(0
:
01)
,ëèáî,íàêîíåö,÷èñëî,ñîîòâåòñòâóþùååëîãè÷åñêîìó
çíà÷åíèþ
TRUE
.
�x=numeric(5)
�for(iin1:5)
+{x[i]=switch(i,cos(pi),exp(1),log2(4),log10(0.01),TRUE)}
�x
[1]-1.0000002.7182822.000000-2.0000001.000000
4.2ÍàïèñàíèåôóíêöèéâR
4.2.1ÑòàíäàðòíàÿôîðìàçàäàíèÿôóíêöèèâR
Ñîçäàòüñîáñòâåííóþôóíêöèþâ
R
äîâîëüíîïðîñòî.Îáùèéâèä:
function(àðãóìåíòû){âûðàæåíèå}
Çäåñü:

function
êëþ÷åâîåñëîâî,ñîîáùàþùåå
R
îòîì,÷òîáóäåòñîçäàíà
ôóíêöèÿ;

àðãóìåíòûôóíêöèè
ñïèñîêôîðìàëüíûõàðãóìåíòîâ(ìîæåòèìåòü
ïðîèçâîëüíóþäëèíó),îòêîòîðûõçàâèñèò
âûðàæåíèå
Áàðãóìåí-
òûðàçäåëÿþòñÿçàïÿòîé.Ôîðìàëüíûìàðãóìåíòîììîæåòáûòü:ñèì-
âîë(ò.å.èìÿïåðåìåííîé,íàïðèìåð,
x
èëè
y
),âûðàæåíèåâèäà
ñèì-
âîë=âûðàæåíèå
(íàïðèìåð,
x=TRUE
),ñïåöèàëüíûéôîðìàëüíûé
àðãóìåíòòðîåòî÷èå
...
.

âûðàæåíèå
(òåëîôóíêöèè)ïðåäñòàâëÿåòñîáîéêîìàíäóèëèáëîê
êîìàíä(çàêëþ÷¼ííûõâôèãóðíûåñêîáêè
fg
),êàêïðàâèëî,çàâèñÿùèõ
îòîïðåäåë¼ííûõðàíååàðãóìåíòîâôóíêöèè.Îòäåëüíûåêîìàíäûâáëî-
êåïèøóòñÿñíîâîéñòðîêè(íîìîæíîèíàîäíîéñòðîêå÷åðåç
;
).

Ôóíêöèÿâêà÷åñòâåñâîåãîçíà÷åíèÿâîçâðàùàåòðåçóëüòàòïîñëåäíåãîâ
ôèãóðíûõñêîáêàõâûðàæåíèÿ.
49
Îáðàùåíèåêôóíêöèèèìååòâèä
èìÿ(àðã1,àðã2,...)
.Çäåñüçíà÷åíèÿ
âûðàæåíèé
àðã1,àðã2,...
ÿâëÿþòñÿôàêòè÷åñêèìèàðãóìåíòàìè,êîòîðûå
ïîäñòàâëÿþòñÿâìåñòîñîîòâåòñòâóþùèõôîðìàëüíûõàðãóìåíòîâ,îïðåäåë¼í-
íûõïðèçàäàíèèôóíêöèè.
Ïðèìåð12.
Ñîçäàäèìôóíêöèþ,âû÷èñëÿþùóþíîðìóêîðåíüêâàäðàòíûé
ñêàëÿðíîãîïðîèçâåäåíèÿâåêòîðîâ
x
è
y
.
�norm=function(x,y)sqrt(x%*%y)
�norm(1:4,2:5)
[,1]
[1,]6.324555
Åñëèôàêòè÷åñêèåàðãóìåíòûçàäàíûâèìåíîâàííîìâèäå
èìÿ=âûðà-
æåíèå
,òîîíèìîãóòáûòüïåðå÷èñëåíûâïðîèçâîëüíîìïîðÿäêå.Áîëååòîãî,
ñïèñîêôàêòè÷åñêèõàðãóìåíòîâìîæåòíà÷èíàòüñÿñàðãóìåíòîâ,ïðåäñòàâ-
ëåííûõâîáû÷íîéïîçèöèîííîéôîðìå,ïîñëå÷åãîìîãóòèäòèàðãóìåíòûâ
èìåíîâàííîéôîðìå.Íàïðèìåð,èìååòñÿôóíêöèÿ
fun
,çàäàííàÿñëåäóþùèì
îáðàçîì:
�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)
Âñåýòèñïîñîáûýêâèâàëåíòíû.
Âîìíîãèõñëó÷àÿõïðèîïðåäåëåíèèôóíêöèèíåêîòîðûìàðãóìåíòàììî-
ãóòáûòüïðèñâîåíûçíà÷åíèÿïîóìîë÷àíèþ.Òîãäàïðèâûçîâåôóíêöèèýòè
àðãóìåíòûìîãóòáûòüîïóùåíû.Ïðåäïîëîæèì,÷òîôóíêöèÿ
fun
îïðåäåëåíà
êàê
�fun-function(arg1,arg2,arg3=20,arg4=TRUE)
{
#òåëîôóíêöèèîïóùåíî
}
Òîãäàîáðàùåíèåêýòîéôóíêöèèâèäà
�fun(d,f)
50
ýêâèâàëåíòíîòð¼ì,ïðèâåä¼ííûìâûøå.Ñëåäóþùååîáðàùåíèå
�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(iin1: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.2205202.4299103.541140
[1]-2.185287-2.436616-3.001431
[1]-0.13654894-0.033105370.07264300
ãäåïåðâàÿñòðîêàýòîìàêñèìàëüíûåçíà÷åíèÿâåêòîðîâ,âòîðàÿñòðîêà
ìèíèìàëüíûå,àïîñëåäíÿÿñðåäíèåçíà÷åíèÿ.
4.2.3Ôîðìàëüíûåàðãóìåíòû,ëîêàëüíûåïåðå-
ìåííûåèñâîáîäíûåïåðåìåííûå
Âñåâñòðå÷àþùèåñÿâòåëåôóíêöèèñèìâîëûäåëÿòñÿíàòðèãðóïïû:ýòî
ôîðìàëüíûåàðãóìåíòû,ëîêàëüíûåïåðåìåííûåèñâîáîäíûåïåðåìåííûå.

Ôîðìàëüíûåàðãóìåíòû(ôîðìàëüíûåïàðàìåòðû)ýòîàðãóìåíòû,ïå-
ðå÷èñëåííûåâçàãîëîâêåôóíêöèè(âêðóãëûõñêîáêàõïîñëåêëþ÷åâîãî
ñëîâà
function
).

Ëîêàëüíûåïåðåìåííûåýòîïåðåìåííûå,íåÿâëÿþùèåñÿôîðìàëüíû-
ìèàðãóìåíòàìè,çíà÷åíèÿêîòîðûõîïðåäåëÿþòñÿâîâðåìÿâûïîëíåíèÿ
ôóíêöèè.
52

Ïåðåìåííûå,íåÿâëÿþùèåñÿôîðìàëüíûìèàðãóìåíòàìèèëîêàëüíûìè
ïåðåìåííûìè,ÿâëÿþòñÿñâîáîäíûìèïåðåìåííûìè.
Íàïðèìåð,âôóíêöèè
�f=function(x)
{
y=2*x
print(x)
print(y)
print(z)
}
x
ôîðìàëüíûéàðãóìåíò,
y
ëîêàëüíàÿïåðåìåííàÿ,
z
ñâîáîäíàÿïåðåìåí-
íàÿ.Îáëàñòüâèäèìîñòèôîðìàëüíûõàðãóìåíòîâèëîêàëüíûõïåðåìåííûõ
òîëüêîñàìàýòàôóíêöèÿ.Ýòîîçíà÷àåò,÷òîèçìåíåíèÿýòèõïåðåìåííûõâíóò-
ðèôóíêöèèíèêàêíåîòðàæàþòñÿíàïåðåìåííûõñòàêèìèæåèìåíàìèâî
âíåøíåéôóíêöèè.Îáëàñòüâèäèìîñòèñâîáîäíûõïåðåìåííûõðàñïðîñòðàíÿ-
åòñÿäîâíåøíåéôóíêöèè,âêîòîðîéîíèáûëèîïðåäåëåíû.Èçìåíåíèåòàêèõ
ïåðåìåííûõâòåëåôóíêöèèâëèÿåòòàêæåíàñîîòâåòñòâóþùèåïåðåìåííûåâ
ýòîéâíåøíåéôóíêöèè.
4.2.4ÏîëíàÿôîðìàçàäàíèÿôóíêöèèâR
Ïîëíàÿôîðìàçàïèñèîòêðàòêîéîòëè÷àåòñÿòîëüêîäîïîëíèòåëüíîéçà-
ïèñüþâòåëåôóíêöèè:
�èìÿ=function(àðãóìåíòû){âûðàæåíèå
Ðåçóëüòàòîìôóíêöèèÿâëÿåòñÿçíà÷åíèåïåðåìåííîé
z
,òàêêàêîíàïîñëåä-
íåéâû÷èñëÿåòñÿâôóíêöèè.
[1]12624120720504040320
[9]3628803628800
Ââîäèìêîìàíäóâîçâðàùåíèÿçíà÷åíèÿ.
ff1=function(x){
y=sum(x);z=cumprod(x)
Âûâîäèìûéðåçóëüòàò
minmax(x,y)
$`ìàêñèìàëüíîåçíà÷åíèåâåêòîðàx`
[1]9
$`ìàêñèìàëüíîåçíà÷åíèåâåêòîðày`
[1]9
$`ìèíèìàëüíîåçíà÷åíèåâåêòîðàx`
[1]1
$`ìèíèìàëüíîåçíà÷åíèåâåêòîðày`
[1]2
4.2.5ÑèëüíîåïðèñâàèâàíèåâR
Äåéñòâóÿîáû÷íûìîáðàçîì,ìåíÿÿçíà÷åíèÿôîðìàëüíûõàðãóìåíòîâ
âíóòðèôóíêöèè,âûíåìåíÿåòåñîîòâåòñòâóþùèõôàêòè÷åñêèõàðãóìåíòîâ.
Åäèíñòâåííûéñïîñîáñäåëàòüýòîèñïîëüçîâàòüâòåëåôóíêöèèñèëüíîå
ïðèñâàèâàíèå


:
arg1-âûðàæåíèå
Ïðèìåð16.
Ðàññìîòðèìïðèìåðññèëüíûìïðèñâàèâàíèåì
3
.
y=5;z=6
ff=function(x){
y=sum(x);z-sqrt(y)
3
Åñëèíåò

,òîâñåâûðàæåíèÿíàïèñàíûâðåäàêòîðåñêðèïòîâ
55
Âôóíêöèèèùåòñÿçíà÷åíèåïåðåìåííîé
y
,èèìåííîýòîçíà÷åíèåèâû-
âîäèòñÿêàêðåçóëüòàòâûïîëíåíèÿôóíêöèè.Íîíàéäåííîåâíóòðèôóíêöèè
çíà÷åíèå
y
íåâëèÿåòíàîïðåäåë¼ííîåâíåôóíêöèè.Áëàãîäàðÿñèëüíîìóïðè-
ñâàèâàíèþçíà÷åíèå
z
,íàéäåííîåâíóòðèôóíêöèè,¾ïåðåêðûâàåò¿çíà÷åíèå,
ïðèñâîåííîåâíåôóíêöèè.
4.2.6Êîìàíäûapply(),sapply()èlapply()
Àíîíèìíûåôóíêöèè
Ñ
apply()
,
sapply()
è
lapply()
÷àñòîèñïîëüçóþò
àíîíèìíûå
ôóíêöèè,
ïðèìåðîäíîéèçêîòîðûõïðèâåä¼ííèæå.
(function(x,y){z-2*x^2+y^2;x+y+z})(0:7,1)
[1]251223385780107
Ñîçäà¼òñÿ÷èñëîâîéâåêòîð,íîñàìàôóíêöèÿíåîáëàäàåòèìåíåì.
Êîìàíäàapply()
Êîìàíäà
apply()
èñïîëüçóåòñÿòîãäà,êîãäàíóæíîïðèìåíèòüêàêóþ-
íèáóäüôóíêöèþêñòðîêåèëèñòîëáöóìàòðèöû(òàáëèöûäàííûõ).Ïîëíàÿ
ôîðìàçàïèñè:
apply(X,óêàçàòåëü,ôóíêöèÿ,...)
Çäåñü:

Õ
èìÿìàòðèöûèëèòàáëèöûäàííûõ;

óêàçàòåëü
óêàçûâàåì,ê÷åìóïðèìåíÿåìôóíêöèþ:
1
êñòðîêàì,
2
êñòîëáöàì,
ñ(1,2)
êñòðîêàìèñòîëáöàìîäíîâðåìåííî(åñëèôóíê-
öèÿïðèìåíÿåòñÿêîâñåìýëåìåíòàììàòðèöûèðåçóëüòàòìàòðèöà,òî
îïðåäåëÿåòñÿïîðÿäîêâûâîäàýëåìåíòîâ);

ôóíêöèÿ
èìÿïðèìåíÿåìîéôóíêöèè,Åñëèíóæíîïðèìåíèòüïðî-
ñòûåîïåðàöèèâèäà
+
,
-
èò.ä.,òîèõíåîáõîäèìîçàäàòüâêàâû÷êàõ.
Ïîêàæåìíàïðèìåðå.
Ïðèìåð17.
Ñíà÷àëàñîçäàäèììàòðèöó
X
.
56
�(X=matrix(1:25,nrow=5))
[,1][,2][,3][,4][,5]
[1,]16111621
[2,]27121722
[3,]38131823
[4,]49141924
[5,]510152025
Íàéä¼ìñóììóýëåìåíòîâïîñòðîêàì
apply(X,1,sum)
[1]5560657075
èïîñòîëáöàì
�apply(X,2,sum)
[1]15406590115
Âîáîèõñëó÷àÿõïîëó÷èëèâåêòîðà,÷üèäëèíûñîîòâåòñòâóþò÷èñëóñòîëá-
öîâèñòðîêñîîòâåòñòâåííî.
Íàéä¼ìêîðåíüêâàäðàòíûéïîâñåìýëåìåíòàììàòðèöû.
apply(X,1,sqrt)
[,1][,2][,3][,4][,5]
[1,]1.0000001.4142141.7320512.0000002.236068
[2,]2.4494902.6457512.8284273.0000003.162278
[3,]3.3166253.4641023.6055513.7416573.872983
[4,]4.0000004.1231064.2426414.3588994.472136
[5,]4.5825764.6904164.7958324.8989795.000000
Çäåñüèçâëåêàåòñÿêîðåíüêâàäðàòíûéèçâñåõýëåìåíòîâìàòðèöûèâûâîä
ïðîèñõîäèòïîñëåäîâàòåëüíîïîñòðîêàì.
Ïðèìåíèìñîáñòâåííóþôóíêöèþ:
�apply(X,1,function(x)x^2-x)
[,1][,2][,3][,4][,5]
[1,]0261220
[2,]3042567290
[3,]110132156182210
[4,]240272306342380
[5,]420462506552600
Ýëåìåíòûâûâîäÿòñÿïîñòðîêàì:ñíà÷àëàïîïåðâîéñòðîêå,çàòåìïîâòî-
ðîéèò.ä.
57
Êîìàíäàsapply()
Ôóíêöèÿ
sapply()
ïðèìåíÿåòñÿàíàëîãè÷íî
apply()
,íîòîëüêîêâåêòî-
ðàìèñïèñêàì.Îíàïîëåçíàïðèñëîæíûõèòåðàöèîííûõâû÷èñëåíèÿõ,òàê
êàêïîçâîëÿåòèçáåæàòüñîçäàíèÿöèêëîâ(ñìîòðèâû÷èñëåíèåôàêòîðèàëîââ
4.2.7).
sapply(X,ôóíêöèÿ,...,simplify=TRUE,USE.NAMES=TRUE)

Õ
îáúåêò,êêîòîðîìóïðèìåíÿåòñÿêîìàíäà;

ôóíêöèÿ
ïðèìåíÿåìàÿêîáúåêòó
X
ôóíêöèÿ;

simplify
ëîãè÷åñêèéàðãóìåíò:íóæíîëèïðåäñòàâëÿòüâûâîäèìûé
ðåçóëüòàòââèäåâåêòîðàèëèìàòðèöû(çíà÷åíèå
TRUE
ïîóìîë÷à-
íèþ);

USE.NAMES
ëîãè÷åñêèéàðãóìåíò:åñëèäàííûéàðãóìåíòïðèíèìà-
åòçíà÷åíèå
TRUE
(ïîóìîë÷àíèþ)è
Õ
ñèìâîëüíîãîòèïà,òîâêà÷åñòâå
íàçâàíèéäëÿâûâîäèìûõðåçóëüòàòîâèñïîëüçóåòñÿ
Õ
.
Âðåçóëüòàòåïðèìåíåíèÿ
sapply()
ñîçäà¼òñÿñïèñîêòîéæåðàçìåðíîñòè,÷òî
èîáúåêò
Õ
.
Ïðèìåð18.
Ñîçäàäèìñïèñîê,ýëåìåíòàìèêîòîðîãîÿâëÿþòñÿòðèâåêòîðà
äâà÷èñëîâûõèîäèíëîãè÷åñêèé.
Êîìàíäà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]12344321
[[3]]
[1]TRUETRUEFALSE
Íàéä¼ìïðèïîìîùè
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
Êðîìåòîãîâûâåäåíîñîîáùåíèå
Ïðåäóïðåæäåíèå
Inmean.default(X[[1L]],...):
àðãóìåíòíåÿâëÿåòñÿ÷èñëîâûìèëèëîãè÷åñêèì:âîçâðàùàþNA
Ýòîëîãè÷íî,òàêêàêïåðâûéýëåìåíòñïèñêàñèìâîëüíûéâåêòîðèêíåìó
íåïðèìåíèìû÷èñëîâûåôóíêöèè.
4.2.7ÏðèìåðûíàïèñàíèÿôóíêöèéâRñèñ-
ïîëüçîâàíèåìóïðàâëÿþùèõêîíñòðóêöèé
Âýòîé÷àñòèðàçáåð¼ìíåñêîëüêîâàðèàíòîâôóíêöèé,ïîçâîëÿþùèõâû-
÷èñëèòüôàêòîðèàë
x
(
x
!
),èñïîëüçóÿöèêëûèóñëîâíûåîïåðàòîðû.
Ñïîìîùüþîïåðàòîðîâ
if
è
for
:
fac1=function(x){
f=1
Èñïîëüçóÿîïåðàòîð
while
,âàæíîíåçàáûòüïðîêîíòðîëüíóþïåðåìåííóþ
(
t
âíàøåìñëó÷àå)èïðîèçìåíåíèåå¼çíà÷åíèéíàêàæäîéèòåðàöèè(
t
=
t

1
).Ïðèìåíÿÿñîçäàííóþôóíêöèþäëÿïîñëåäîâàòåëüíîñòè÷èñåëîò0äî
5,ïîëó÷èì
�sapply(0:5,fac2)
[1]112624120
Íàêîíåö,ïðèïîìîùèîïåðàòîðà¾áåñêîíå÷íîãî¿öèêëà
repeat
:
fac3=function(x){
f=1
t=x
repeat{
if(t2)break
f=f*t
t=t-1}
4
Ñì.ðàçäåë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]0000000000
�vector('complex',10)
[1]0+0i0+0i0+0i0+0i0+0i0+0i0+0i0+0i0+0i0+0i
�vector('logical',10)
[1]FALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSEFALSE
�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]35-24
�y=c(T,F,T,T);y
[1]TRUEFALSETRUETRUE
�z=c('a','b','ab','abc');z
[1]"a""b""ab""abc"
63
Åñëèâêà÷åñòâåàðãóìåíòîâôóíêöèè
c()
çàäàòüäàííûåðàçëè÷íûõòèïîâ,òî
îíèáóäóòïðèâîäèòüñÿêåäèíîìó:ëîãè÷åñêèåè÷èñëîâûåäàííûåïðèâîäÿòñÿ
ê÷èñëîâîìóòèïóäàííûõ;ëîãè÷åñêèå,÷èñëîâûåèñèìâîëüíûåêñèìâîëü-
íîìóòèïó;äåéñòâèòåëüíûåèêîìïëåêñíûåêêîìïëåêñíîìóòèïó.
�x=c(2,3,-2,T,F,T);x
[1]23-2101
�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.22.0
�y=c(-0.6,pi,Inf,5);y
[1]-0.6000003.141593Inf5.000000
�z=c(2,x,-1,y);z
[1]2.0000001.000000-3.2000002.000000-1.000000-0.6000003.141593
[8]Inf5.000000
Íàêîíåö,ìîæíîçàäàòüâåêòîð,íàáèðàÿäàííûåíàêëàâèàòóðå.Ýòîðåà-
ëèçóåòñÿïðèïîìîùè
scan()
1
.
Ïðèìåð20.
Ñîçäàäèì÷èñëîâîéâåêòîð
y
ïðèïîìîùèôóíêöèè
scan()
.
�y=scan()
Ïîñëåýòîãîñëåäóåòíàæàòüíàêëàâèøó
Enter
.Âêîíñîëèïîÿâèòñÿïðè-
ãëàøåíèå
1:
ïîñëå÷åãîìîæíîââîäèòüäàííûå(÷èñëà),íàáèðàÿèõíàêëàâèàòóðå.Ïðîáåë
ðàçäåëÿåò÷èñëà,
1:234
íàæàòèåíà
Enter
îçíà÷àåòïåðåõîäíàíîâóþñòðîêó
3:13567
7:
Äâîéíîåíàæàòèåíàêëàâèøó
Enter
çàâåðøàåòââîä.Âûâîäèòñÿñîîáùåíèå
îêîëè÷åñòâåñ÷èòàííûõýëåìåíòîââåêòîðà.
1
Áîëååïîäðîáíîîáýòîéôóíêöèè,àòàêæåîñ÷èòûâàíèèäàííûõèçôàéëîâðàññêàçàíî
âãëàâå6.
64
Read6items
×òîáûâûâåñòèñîçäàííûéòàêèìîáðàçîìâåêòîð,äîñòàòî÷íîíàáðàòüåãî
èìÿ:
�y
[1]23413567
Ñòîèòçàìåòèòü,÷òîïðèïîìîùè
scan()
ìîæíîñîçäàâàòüòîëüêî÷èñëîâûå
âåêòîðà.
Âçàêëþ÷åíèåðàññìîòðèìäâåïîëåçíûõôóíêöèè:
is.vector()
è
as.vector()
.Ïåðâàÿèçíèõïðîâåðÿåò,ÿâëÿåòñÿëèàðãóìåíòýòîéôóíêöèè
âåêòîðîì.
Ïðèìåð21.
Ñîçäàäèìäâàîáúåêòàâåêòîð
y
,ñîñòîÿùèéèçýëåìåíòîâ
ðàçíîãîòèïà,èôàêòîð
z
(ôàêòîðûðàññìîòðåíûâðàçäåëå5.6äàííîéãëàâû).
�y=c(T,F,1,-3,2,1+1i*2);y
[1]1+0i0+0i1+0i-3+0i2+0i1+2i
�z=factor(y)
�z=factor(y);z
[1]1+0i0+0i1+0i-3+0i2+0i1+2i
Levels:-3+0i0+0i1+0i1+2i2+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]""""""""""""""""""""
Ôóíêöèÿpaste()
Êîíêàòåíàöèþ(ñêëåéêà)ñòðîêîñóùåñòâëÿåòôóíêöèÿ
paste()
.Âïðîñòåé-
øåìâàðèàíòå,êîãäàýëåìåíòûñèìâîëüíûåñòðîêè,ïðîèñõîäèòñêëåéêàóêà-
çàííûõñòðîêñîâñòàâêîéïðîáåëàâêà÷åñòâåðàçäåëèòåëÿ.
paste("çèìà","ïåðâûé","ñåçîí","ãîäà")
[1]"çèìàïåðâûéñåçîíãîäà"
Åñëèàðãóìåíòûôóíêöèè
paste()
ìàññèâû,òîñêëåèâàþòñÿñîîòâåò-
ñòâóþùèåêîìïîíåíòû.Ïðèýòîì,åñëèäëèíûìàññèâîâðàçëè÷íû,ïðîèñõîäèò
öèêëè÷åñêàÿïîäñòàíîâêàìåíüøåãîèçíèõ.
�paste(c('çèìà','âåñíà','ëåòî','îñåíü'),c('-âðåìÿãîäà'),sep='')
[1]"çèìà-âðåìÿãîäà""âåñíà-âðåìÿãîäà""ëåòî-âðåìÿãîäà""îñåíü-âðåìÿãîäà"
Åñëèîäèíèçâåêòîðîâ÷èñëîâîé,òîîíàâòîìàòè÷åñêèáóäåòêîíâåðòè-
ðîâàíâñèìâîëüíûé:
�paste("x",1:5)
�[1]"x1""x2""x3""x4""x5"
Ìîæíîçàìåíèòüðàçäåëèòåëüíàäðóãîé:
�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(iin1: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]12345678910111213141516171819202122232425
[26]26
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]12345678
Âåêòîð,ñîñòîÿùèéèçïîñëåäîâàòåëüíûõ÷èñåë,ìîæíîïîëó÷èòüñïîìî-
ùüþêîìàíäû
íà÷àëüíîå-çíà÷åíèå:êîíå÷íîå-çíà÷åíèå
.Íàïðèìåð,
1:5
.
Íàýòóêîìàíäóïîõîæàôóíêöèÿ
seq()
,êîòîðàÿãåíåðèðóåòàðèôìåòè÷åñêóþ
ïðîãðåññèþ.Íóæíîçàäàòüíà÷àëüíîåçíà÷åíèå,êîíå÷íîåçíà÷åíèåèëèáîøàã
ïðîãðåññèè:
�seq(0,1,by=0.1)
[1]0.00.10.20.30.40.50.60.70.80.91.0
ëèáîêîëè÷åñòâîýëåìåíòîâïîñëåäîâàòåëüíîñòè:
�seq(0,1,len=11)
[1]0.00.10.20.30.40.50.60.70.80.91.0
Âðåçóëüòàòåáóäåòñôîðìèðîâàíâåêòîð,ñîñòîÿùèéèççàäàííîãî÷èñëàýëå-
ìåíòîâ,ðàâíîìåðíîðàñïðåäåë¼ííûõíàçàäàííîìîòðåçêå.
Ôóíêöèÿ
rep(v,k)
ñîçäà¼òâåêòîð,ñîñòîÿùèéèç
k
êîïèéâåêòîðà
v
,åñëè
k
ýòî÷èñëî:
�rep(c(1,2),3)
[1]121212
ëèáî,åñëè
k
=(
k
1
;k
2
;:::;k
n
)
âåêòîð,òîýëåìåíòûâåêòîðà
v
áóäóòïîâòî-
ðÿòüñÿ
k
1
;k
2
;:::;k
n
ðàçñîîòâåòñòâåííî.
�x=rep(c(1,2,3,4,5),c(1,2,3,4,5));x
[1]122333444455555
Òàêæåìîæíîñîçäàâàòüâåêòîðàïðèïîìîùèôóíêöèé,ãåíåðèðóþùèõñëó-
÷àéíóþïîñëåäîâàòåëüíîñòü÷èñåë,ïîä÷èíÿþùèõñÿîäíîìóèçðåàëèçîâàííûõ
â
R
âåðîÿòíîñòíûõçàêîíîâ
2
.
Ýëåìåíòûâåêòîðàìîãóòèìåòüçíà÷åíèÿ
Inf
,
NaN
,
NA
.Íàïðèìåð,
2
Ñìîòðèãëàâó8
69
�age=c(23,NA,NA,18,19,Inf,NaN)
Íàäâåêòîðàìèìîæíîâûïîëíÿòüàðèôìåòè÷åñêèåîïåðàöèèèýëåìåíòàð-
íûåôóíêöèè.Áèíàðíàÿîïåðàöèÿíàääâóìÿâåêòîðàìèîäèíàêîâîéäëèíû
ïðîèçâîäèòñÿíàäêàæäîéïàðîéýëåìåíòîâ,èðåçóëüòàòîìÿâëÿåòñÿâåêòîðòîé
æåäëèíû,÷òîèèñõîäíûå.Âñëó÷àå,êîãäàðàçìåðíîñòüâåêòîðîâíåñîâïàäàåò,
ïðîèçâîäèòñÿïðèâåäåíèåäëèíûáîëååêîðîòêîãîâåêòîðàêäëèíåäëèííîãî.
Ïðèâåäåíèåâûïîëíÿåòñÿñëåäóþùèìîáðàçîìýëåìåíòûâåêòîðàäóáëèðó-
þòñÿíåîáõîäèìîå÷èñëîðàçïîëíîñòüþ(åñëèäëèíàáîëüøåãîâåêòîðàêðàòíà
äëèíåìåíüøåãîâåêòîðà)
�c(1,2,3,4)+c(1,2)
[1]2446
�c(1,2,3,4)-c(1,2)
[1]0022
�c(1,2,3,4)*c(1,2)
[1]1438
�c(1,2,3,4)/c(1,2)
[1]1132
�c(1,2,3,4)^c(1,2)
[1]14316
�c(1,2,3,4)/c(1,0)
[1]1Inf3Inf
�2*c(1,2,3,4,5)
[1]246810
èëè÷àñòè÷íî(âïðîòèâíîìñëó÷àå),ïðèýòîìâûäà¼òñÿïðåäóïðåæäåíèå,íî
ðåçóëüòàòâñ¼ðàâíîâû÷èñëÿåòñÿ.
�c(3,1,4,1,5,9,2)+c(9,5)
[1]126136141411
Âûðàæåíèÿâèäà
âåêòîð*÷èñëî
,
âåêòîð/÷èñëî
,
âåêòîð+÷èñëî
,
âåêòîð-÷èñëî
îçíà÷àþò,÷òîêàæäûéýëåìåíòâåêòîðàóìíîæàåòñÿèëèäå-
ëèòñÿíà÷èñëî,ëèáîêêàæäîìóýëåìåíòóâåêòîðàïðèáàâëÿåòñÿ(âû÷èòàåòñÿ)
÷èñëî
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�x1*2
[1]20-6124-8NA246810
�x1/4
[1]2.50-0.751.500.50-1.00NA0.250.500.751.001.25
70
�x1+5
[1]1521171NA678910
�x1-2
[1]8-540-6NA-10123
Ýëåìåíòàðíûåìàòåìàòè÷åñêèåôóíêöèèïðèìåíÿþòñÿêêàæäîéêîìïî-
íåíòåâåêòîðà.
�x=c(0,pi/2,pi)
�sin(x)
[1]0.000000e+001.000000e+001.224606e-16
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�sqrt(x1)
[1]3.162278NaN2.4494901.414214NaNNA1.0000001.414214
[9]1.7320512.0000002.236068
Ïðåäóïðåæäåíèå
Insqrt(x1):ñîçäàíûNaN
Ôóíêöèèäëÿðàáîòûñâåêòîðàìè
Â
R
ðåàëèçîâàíîìíîãîïîëåçíûõôóíêöèéäëÿðàáîòûñâåêòîðàìè,ïîç-
âîëÿþùèõèçáåæàòüèñïîëüçîâàíèÿöèêëîâ:

length()
äëèíàâåêòîðà.
�x=c(1:5,NA,NaN,6:10);x
[1]12345NANaN678910
�length(x)
[1]12

max()
è
min()
íàõîæäåíèåìàêñèìàëüíîãîèìèíèìàëüíîãîýëåìåí-
òîââçàäàííîìâåêòîðå.Åñëèõîòÿáûîäèíýëåìåíòðàâåí
NA
,òîðå-
çóëüòàòïîèñêàìàêñèìóìà(ìèíèìóìà)
NA
,åñëèåñòü
NaN
ðåçóëü-
òàò
NaN
.Äëÿóñòðàíåíèÿ
NA
(
NaN
)èçðàñ÷¼òîâäîñòàòî÷íîçàäàòü
max(x,na.rm=T)
è
min(x,na.rm=T)
.Äëÿ÷èñëîâîãîâåêòîðàíóëå-
âîéäëèíûìàêñèìàëüíîåçíà÷åíèåðàâíî
-Inf
,àìèíèìàëüíîå
Inf
.
�x=c(1:5,NA,NaN,6:10);x
[1]12345NANaN678910
�min(x)
[1]NaN
71
�max(x)
[1]NaN
�min(x,na.rm=T)
[1]1
�max(x,na.rm=T)
[1]10

pmax()
è
pmin()
ïàðàëëåëüíûéìàêñèìóì(ìèíèìóì)äëÿëþ-
áîãîçàäàííîãî÷èñëàâåêòîðîâ.Ðåçóëüòàòâåêòîð,äëèíàêîòîðî-
ãîðàâíàäëèíåìàêñèìàëüíîãîèçñðàâíèâàåìûõâåêòîðîâèýëåìåí-
òûêîòîðîãîåñòüìàêñèìàëüíûå(ìèíèìàëüíûå)çíà÷åíèÿñðàâíèâàå-
ìûõâåêòîðîâ,íàõîäÿùèåñÿíàîäèíàêîâûõïîçèöèÿõ.Ò.å.
pmax(
x;y
)=
(max(
x
1
;y
1
)
;
max(
x
2
;y
)
;:::
)
,
pmin(
x;y
)=(min(
x
1
;y
1
)
;
min(
x
2
;y
)
;:::
)
.Åñ-
ëèâåêòîðà-àðãóìåíòèìåþòðàçíóþäëèíó,òîíàèìåíüøèéâåêòîðïðèâî-
äèòñÿïðèïîìîùèïîâòîðåíèÿýëåìåíòîâêäëèíåíàèáîëüøåãîâåêòîðà.
�x=rpois(10,1);x
[1]2013111102
�y=rpois(6,1);y
[1]042100
�pmax(x,y)
[1]2423111422
�pmin(x,y)
[1]0011000101
Åñëèñðåäèñðàâíèâàåìûõýëåìåíòîâèñõîäíûõâåêòîðîâåñòü
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]NaNNANaN
�pmax(x,y,na.rm=T)
[1]NaNNANaN

mean()
3
ñðåäíååàðèôìåòè÷åñêîåâåêòîðà.Åñëèåñòüõîòÿáûîäèí
ýëåìåíò,÷ü¼çíà÷åíèå
NA
(
NaN
),òîðåçóëüòàòîìñóììèðîâàíèÿòàêæå
áóäåò
NA
(
NaN
).×òîáûèçáåæàòüýòîãî,òàêæåíóæíîçàäàòüäîïîëíè-
òåëüíûéàðãóìåíò
mean(x,na.rm=TRUE)
.
3
Ñýòîéôóíêöèåéåù¼âñòðåòèìñÿâãëàâå8
72
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�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-362-4NA12345
�range(x1)
[1]NANA
�range(x1,na.rm=T)
[1]-410

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]12345NANaN678910
�prod(x)
[1]NA
�prod(x,na.rm=T)
[1]3628800


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-362-4NA12345
�sort(x1)
[1]-4-3122345610
�sort(x1,dec=T)
[1]106543221-3-4
�sort(x1,decreasing=T)
[1]106543221-3-4

rev(sort())
ñîðòèðîâêàâåêòîðàâóáûâàþùåìïîðÿäêå.
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�rev(sort(x1))
[1]106543221-3-4

rank()
ïðèñâàèâàíèåðàíãîâýëåìåíòàìâåêòîðàâïîðÿäêåâîçðàñòàíèÿ
çíà÷åíèÿýòèõýëåìåíòîââñîîòâåòñòâèèñîäíèìèççàäàííûõìåòîäîâ(
random
,
average
,
rst
,
max
,
min
).
Åñëèâñåýëåìåíòûâåêòîðàðàçëè÷íû,òîðåçóëüòàòïðèñâàèâàíèÿðàíãîâäëÿ
âñåõìåòîäîâîäèíèòîòæå.
x=c(5:1,6,9,10,8,7);x
[1]54321691087
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]
x54321691087
random54321691087
average54321691087
first54321691087
max54321691087
min54321691087
Åñëèæåñðåäèýëåìåíòîââåêòîðàåñòüïîâòîðÿþùèåñÿ,òîðàíãèîäèíàêîâûì
ýëåìåíòàìâåêòîðàïðèñâàèâàþòñÿâêàæäîììåòîäåïîñâîåìó.
74
[,1][,2][,3][,4][,5][,6][,7][,8][,9][,10]
x6.01110.0913128710.06.0
random2.087.05109436.01.0
average1.586.55109436.51.5
first1.086.05109437.02.0
max2.087.05109437.02.0
min1.086.05109436.01.0
Ìåòîä
random
ðàíãêàæäîãîèçîäèíàêîâûõýëåìåíòîâîïðåäåëÿåòñÿñëó-
÷àéíûìîáðàçîì;
average
îäèíàêîâûìýëåìåíòàìïðèñâàèâàåòñÿñðåäíåå
àðèôìåòè÷åñêîåèõðàíãîâ;
rst
ðàíãèîäèíàêîâûõýëåìåíòîâîïðåäåëÿþò-
ñÿèõðàñïîëîæåíèåìââåêòîðå;
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]
x811.013NA7.011.010NA147.0
last35.5791.55.541081.5
first57.5913.57.562103.5
keep35.57NA1.55.54NA81.5
�Na
[1]3.05.57.01.55.54.08.01.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]8111397111012147
�y
[1]9109109
75
�match(x,y)
[1]NANANA1NANA2NANANA
�match(y,x)
[1]47474
Êóìóëÿòèâíûå(íàêîïèòåëüíûå)ôóíêöèè
Êêóìóëÿòèâíûìôóíêöèÿìâ
R
îòíîñÿòñÿñëåäóþùèåôóíêöèè:

cumsum(x)
êóìóëÿòèâíàÿñóììàïîàðãóìåíòó
x
(ïîñëåäîâàòåëüíîå
ñëîæåíèåýëåìåíòîââåêòîðà);

cumprod(x)
êóìóëÿòèâíîåïðîèçâåäåíèå;

cummax(x)
êóìóëÿòèâíûéìàêñèìóì(ïîñëåäîâàòåëüíûéìàêñèìóì
ïîýëåìåíòàì
x
);

cummin(x)
êóìóëÿòèâíûéìèíèìóì(ïîñëåäîâàòåëüíûéìèíèìóìïî
ýëåìåíòàìâåêòîðà).
�x=1:10
�cumsum(x)
[1]13610152128364555
�y=-5:5
�cumprod(y)
[1]-520-60120-120000000
�x=-5:4
�cummax(x)
[1]-5-4-3-2-101234
�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
Ïðèìåð24.
Ñîçäàäèìëîãè÷åñêèéâåêòîð
young
òîéæåäëèíû,÷òîè
age
,
ñêîìïîíåíòàìè,ðàâíûìè
TRUE
,ãäåóñëîâèåâûïîëíåíî,è
FALSE
,ãäå
óñëîâèåíåâûïîëíåíî.
�age=c(1,2,NA,Inf,NaN,18,19,40)
�young-(age&#x--51;Jge;&#x-514;=2)&(age=30)
�young
[1]FALSETRUENAFALSENATRUETRUEFALSE
Ëîãè÷åñêèåâåêòîðûìîãóòèñïîëüçîâàòüñÿâîáû÷íîéàðèôìåòèêå.Ïðè
ýòîì
TRUE
èíòåðïðåòèðóåòñÿêàê
1

FALSE
êàê
0
.
Ôóíêöèÿïðîâåðêèíàïðèíàäëåæíîñòüëèáîêòèïóäàííûõ,ëèáîêêëàññó
äàííûõ,àòàêæåôóíêöèèïðîâåðêèíàñîîòâåòñòâèåíåêîòîðûìçíà÷åíèÿì
âîçâðàùàþòëîãè÷åñêèéâåêòîðòîéæåäëèíû,÷òîèïðîâåðÿåìûéîáúåêò,ñ
êîìïîíåíòàìè
TRUE
è
FALSE
.
Ïðèìåð25.
Ïðîâåðèì,ÿâëÿþòñÿëèýëåìåíòûâåêòîðà
a
ïåðåìåííûìèâèäà
NA
èëè
NaN
.Äëÿýòîãîâîñïîëüçóåìñÿôóíêöèåé
is.na()
.
�a=c(0,1,Inf,NaN,NA)
�is.na(a)
[1]FALSEFALSEFALSETRUETRUE
Âðåçóëüòàòåïîëó÷èìëîãè÷åñêèéâåêòîð,÷üÿäëèíàñîâïàäàåòñäëèíîé
âåêòîðà
a
,ñêîìïîíåíòàìè,ðàâíûìè
FALSE
,ãäåñîîòâåòñòâóþùèåçíà÷å-
íèÿâåêòîðà
a
íåðàâíû
NaN
èëè
NA

TRUE
âîñòàëüíûõñëó÷àÿõ.
Ôóíêöèÿ
is.nan(a)
òàêæåâîçâðàùàåòëîãè÷åñêèéâåêòîðòîéæåäëèíû,
÷òîèïðîâåðÿåìûéîáúåêò(âåêòîð
a
),ñýëåìåíòàìè
TRUE
,ãäåñîîòâåò-
ñòâóþùèåçíà÷åíèÿèñõîäíîãîâåêòîðà
a
ðàâíû
NaN

FALSE
âîñòàëüíûõ
ñëó÷àÿõ.
�is.nan(a)
[1]FALSEFALSEFALSETRUEFALSE
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
171925137
Ïîñëåïðèñâàèâàíèÿèì¼íýëåìåíòàìâåêòîðàîáðàùàòüñÿêýëåìåíòàì
ìîæíîêàêñïîìîùüþèíäåêñà,òàêèèìåíè:
�group[2]
ÍÏ-202
19
�group['ÍÏ-203']
ÍÏ-203
25
�group['ÍÏ-203']=18
�group
ÍÏ-201ÍÏ-202ÍÏ-203ÍÊ-201ÍÈ-201
171918137
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]1NANANA5
78
Âûðàæåíèåâèäà
u[-i]
îçíà÷àåò,÷òîáóäåòñîçäàííîâûéâåêòîðïóò¼ìóäà-
ëåíèÿ
i
-ãîýëåìåíòàèçèñõîäíîãîâåêòîðà
u
.
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�length(x1)
[1]11
�x1[-6]
[1]10-362-412345
Îáðàùåíèåâèäà
u[]
ïðèâîäèòêñîçäàíèþíîâîãîâåêòîðà,ñîñòîÿùåãîèç
âñåõýëåìåíòîâèñõîäíîãîâåêòîðà
u
.
�x1[]
[1]10-362-4NA12345
Âåêòîðûâêà÷åñòâåèíäåêñîâ
Ïóñòü
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-362-4NA12345
�x2=c(T,F,T,F,F)
�y=x1[x2];y
[1]106NA25
�x3=rep(c(T,F),10);x3
79
[1]TRUEFALSETRUEFALSETRUEFALSETRUEFALSETRUEFALSETRUEFALSE
[13]TRUEFALSETRUEFALSETRUEFALSETRUEFALSE
�y=x1[x3];y
[1]106-4135NANANANA

x
ýòîïîëîæèòåëüíûéöåëî÷èñëåííûéâåêòîð.Òîãäàêîìïîíåíòûâåê-
òîðà
x
èíòåðïðåòèðóþòñÿêàêîáû÷íûåèíäåêñû,è
v[x]
âåêòîð,ñîñòî-
ÿùèéèçýëåìåíòîââåêòîðà
v
âòîéïîñëåäîâàòåëüíîñòè,âêîòîðîéîíè
óêàçàíûââåêòîðå-èíäåêñå
x
.
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�x=c(1,3,5,15)
�y=x1[x];y
[1]106-4NA

x
îòðèöàòåëüíûéöåëî÷èñëåííûéâåêòîð.Àáñîëþòíûåçíà÷åíèÿâåê-
òîðà
i
èíòåðïðåòèðóþòñÿêàêíîìåðàýëåìåíòîâ,èñêëþ÷àåìûõèç
v
.
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�x=c(-1,-3,-5)
�y=x1[x];y
[1]-32NA12345
�y=x1[-(2:6)];y
[1]1012345

x
ñèìâîëüíûéâåêòîð.Åãîçíà÷åíèÿèíòåðïðåòèðóþòñÿêàêèìåíàýëå-
ìåíòîââåêòîðà
v
.Âðåçóëüòàòå,
v[x]
âåêòîð,ñôîðìèðîâàííûéèçñîîò-
âåòñòâóþùèõýëåìåíòîââåêòîðà
v
âòîéïîñëåäîâàòåëüíîñòè,âêîòîðîé
îíèóêàçàíûâ
x
.
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
cdefgh
62-4NA12
�y=x1[c('g','a','j','d')];y
gajd
11042
Òàêæåâêà÷åñòâåèíäåêñîâìîæíîóêàçûâàòüðàçëè÷íûåîïåðàöèè,ïðèâî-
äÿùèåêñîçäàíèþëîãè÷åñêîãîâåêòîðà
5
.Íàïðèìåð
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�y1=x1[x15];y1
[1]-32-4NA1234
�y2=x1[(-4x1)&(x13)];y2
[1]-32NA12
�&#x=-2|;&#xx100;y3=x1[(x1=-2)|(x13)];y3
[1]10-36-4NA45
�y4=x1[!((-3=x1)&(x13))];y4
[1]106-4NA345
èëè
�y5=x1[!is.na(x1)];y5
[1]10-362-412345
Çäåñüôîðìèðóåòñÿâåêòîð
y5
òîëüêîèçòåõêîìïîíåíòâåêòîðà
x1
,êîòîðûå
íåÿâëÿþòñÿ
NA
è
NaN
.
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�x1[8]=NaN;x1
[1]10-362-4NA1NaN345
�y6=x1[!is.nan(x1)];y6
[1]10-362-4NA1345
Êîìàíäà
�x[is.na(x)]-0
çàìåíÿåòçíà÷åíèÿ
NA
è
NaN
íóëÿìè.
Âûðàæåíèå
5
Ñìîòðèðàçäåë3.2.1ãëàâû3
81
�x1=c(10,-3,6,2,-4,NA,1:5);x1
[1]10-362-4NA12345
�z=(x1+1)[(!is.na(x1))&x1�0];z
[1]117323456
ñîçäà¼òâåêòîð
z
èðàçìåùàåòâíåìýëåìåíòûâåêòîðà
x1+1
(êêàæäîìó
ýëåìåíòóâåêòîðà
õ1
ïðèáàâëåíà1),óäîâëåòâîðÿþùèåçàäàííûìóñëîâèÿì.
5.2.7Ôóíêöèèwhich(),duplicated()èunique()
Ïðèðàáîòåñìàññèâàìèáûâàåòíóæíîîïðåäåëèòüäëÿíåêîòîðîãîýëå-
ìåíòà(îâ)âåêòîðàåãîèíäåêñ.Äëÿýòîãîñëóæèòôóíêöèÿ
which()
,ðàáîòóñ
êîòîðîéðàññìîòðèìíàïðèìåðå.
Ïðèìåð27.
Ïóñòüçàäàíâåêòîð
x
.
�x
[1]1047978614101058111120
�length(x)
[1]15
Îïðåäåëèìíîìåðàýëåìåíòîââåêòîðà
x
,êîòîðûåáîëüøå10.
��which(x10)
[1]8131415
Òåïåðü,åñëèíóæíîââåêòîðå
x
íàéòè,êïðèìåðó,âòîðîéýëåìåíò,ïðåâîñ-
õîäÿùèé10,òîäîñòàòî÷íîóêàçàòü
�x[13]
[1]11
ëèáî
��y=which(x10)
�x[y[2]]
[1]11
Äðóãîéïðèìåð.
Ïðèìåð28.
Íàéä¼ìââåêòîðå
x
âòîðîéýëåìåíò,êðàòíûé5.
�x
[1]1047978614101058111120
�y=which(x\%\%5==0);y
[1]19101115
�x[y[2]]
[1]10
82
Ñôóíêöèåé
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]1314
èâûâåäåìèõíàýêðàí.
�x[y]
[1]1111
Ôóíêöèÿ
duplicated()
ïîçâîëÿåòäëÿâåêòîðàíàéòèâñåðàçëè÷íûåýëå-
ìåíòû.Îïðåäåëÿåò,ÿâëÿåòñÿ
i
-éýëåìåíòâåêòîðàïîâòîðåíèåìýëåìåíòà(îâ),
ðàñïîëîæåííîãîðàíåå(ò.å.ÿâëÿåòñÿïîâòîðåíèåìýëåìåíòàñìåíüøèìçíà÷å-
íèåìèíäåêñà).
duplicated(x,incomparables=FALSE,fromLast=FALSE,...)
Àðãóìåíòû:

x
âåêòîð.

incomparables
âåêòîðçíà÷åíèé,êîòîðûåíåñðàâíèâàþòñÿ.Çíà÷åíèå
FALSE
ýòîãîàðãóìåíòàîçíà÷àåò,÷òîñðàâíèâàþòñÿâñåýëåìåíòûìàñ-
ñèâà.

fromLast
ëîãè÷åñêèéàðãóìåíòíóæíîëèèñêàòüïîâòîðÿþùèåñÿ
ýëåìåíòûñêîíöàìàññèâà,àíåñíà÷àëà.

...
äîïîëíèòåëüíûéàðãóìåíò.
Ðåçóëüòàòðàáîòûôóíêöèè-ëîãè÷åñêèéâåêòîð,÷üÿäëèíàñîâïàäàåòñ
äëèíîéâåêòîðà
õ
.Ýëåìåíòûâåêòîðà:
TRUE
åñëèýëåìåíòïîâòîðÿþùèéñÿ,
FALSE
ýëåìåíòâåêòîðà
õ
âñòðå÷àåòñÿâïåðâûå.
Ôóíêöèÿ
83
unique(x,incomparables=FALSE,fromLast=FALSE,...)
ñàðãóìåíòàìè

x
èñõîäíûéâåêòîð,

incomparables
âåêòîðçíà÷åíèé,êîòîðûåíåñðàâíèâàþòñÿ.Çíà÷åíèå
FALSE
ýòîãîàðãóìåíòàîçíà÷àåò,÷òîñðàâíèâàþòñÿâñåýëåìåíòûìàñ-
ñèâà;

fromLast
ëîãè÷åñêèéàðãóìåíòíóæíîëèèñêàòüïîâòîðÿþùèåñÿ
ýëåìåíòûñêîíöàìàññèâà,àíåñíà÷àëà;

...
äîïîëíèòåëüíûéàðãóìåíò;
îïðåäåëÿåòâñåóíèêàëüíûå(ò.å.âñòðå÷àþùèåñÿòîëüêîðàç)ýëåìåíòûâûáîð-
êè.
Ïðèìåð30.
Ñíîâàâîñïîëüçóåìñÿâåêòîðîì
x
èçïðèìåðà27èíàéä¼ìâñå
ðàçëè÷íûåýëåìåíòûâåêòîðà
�x=c(10,4,7,9,7,8,6,14,10,10,5,8,11,11,20)
�y=duplicated(x);y
[1]FALSEFALSEFALSEFALSETRUEFALSEFALSEFALSETRUETRUEFALSETRUE
[13]FALSETRUEFALSE
�x1=x[!y];x1
[1]10479861451120
�x2=unique(x);x2
[1]10479861451120
5.3Ìàòðèöû
5.3.1Çàäàíèåìàòðèöû
×èñëîâóþìàòðèöóìîæíîñîçäàòüèç÷èñëîâîãîâåêòîðàñïîìîùüþôóíê-
öèè
matrix()
matrix(x,nrow,ncol,byrow,dimnames)
Äëÿçàäàíèÿìàòðèöûíåîáõîäèììàññèâäàííûõ
õ
,íóæíîóêàçàòü÷èñëîñòðîê
nrow=m
è/èëè÷èñëîñòîëáöîâ
ncol=n
(ïîóìîë÷àíèþ,÷èñëîñòðîêðàâíÿ-
åòñÿ÷èñëóñòîëáöîâèðàâíî1);îïðåäåëèòüêàêýëåìåíòûâåêòîðà
õ
çàïîëíÿþò
ìàòðèöóïîñòðîêàìèëèïîñòîëáöàì(ïîóìîë÷àíèþìàòðèöàçàïîëíÿåòñÿ
ïîñòîëáöàì).Âðåçóëüòàòåýëåìåíòûèçâåêòîðàáóäóòçàïèñàíûâìàòðèöó
84
óêàçàííûõðàçìåðîâ.Àðãóìåíò
dimnames
ñïèñîêèçäâóõêîìïîíåíò,ïåð-
âàÿèçêîòîðûõçàäà¼òíàçâàíèÿñòðîê,àâòîðàÿíàçâàíèÿñòîëáöîâ(ïî
óìîë÷àíèþèìåíàñòðîêèñòîëáöîâíåçàäàþòñÿ).
�matrix(1:6,nrow=2,ncol=3)
[,1][,2][,3]
[1,]135
[2,]246
�matrix(1:6,nrow=2,ncol=3,byrow=T)
[,1][,2][,3]
[1,]123
[2,]456
�matrix(1:6,nrow=2,ncol=3,byrow=T,list(c(1,2),c('A','B','C')))
ABC
1123
2456

Ôîðìàëüíîíóæíî,÷òîáûäëèíàâåêòîðà
õ
áûëàêðàòíàïðîèçâåäåíèþòðå-
áóåìîãî÷èñëàñòðîêíàòðåáóåìîå÷èñëîñòîëáöîâ,
matrix(1:2,nrow=2,ncol=3)
[,1][,2][,3]
[1,]111
[2,]222
matrix(1,nrow=2,ncol=3)
[,1][,2][,3]
[1,]111
[2,]111
íîýòîíåîáÿçàòåëüíî:
�matrix(1:12,nrow=5,ncol=3)
[,1][,2][,3]
[1,]1611
[2,]2712
[3,]381
[4,]492
[5,]5103
Ïðåäóïðåæäåíèå
Inmatrix(1:12,nrow=5,ncol=3):
äëèíàäàííûõ[12]íåÿâëÿåòñÿìíîæèòåëåìêîëè÷åñòâàñòðîê[5]
85
Åñëèóêàçûâàåòñÿòîëüêîîäíàèçðàçìåðíîñòåé(íàïðèìåð,òîëüêî÷èñëî
ñòîëáöîâ),òîæåëàòåëüíî,÷òîáûäëèíàâåêòîðàáûëàêðàòíàýòîéðàçìåðíî-
ñòè.Âòîðàÿðàçìåðíîñòüáóäåòîïðåäåëåíàêàêîòíîøåíèåäëèíûâåêòîðàê
ïåðâîéðàçìåðíîñòè.
�matrix(1:12,ncol=3)
[,1][,2][,3]
[1,]159
[2,]2610
[3,]3711
[4,]4812
Åñëèæå÷èñëîñòîëáöîâ(ñòðîê)íåÿâëÿåòñÿäåëèòåëåìäëèíûâåêòîðà
õ
,
òîìàòðèöàâñ¼ðàâíîáóäåòïîñòðîåíà(ïðàâäàñïðåäóïðåæäåíèåì).Âòîðàÿ
ðàçìåðíîñòüáóäåòîïðåäåëåíàêàêáëèæàéøååáîëüøååöåëîå÷èñëîêîñòàòêó
îòäåëåíèÿäëèíûâåêòîðàíàçàäàííóþðàçìåðíîñòü.
�A=matrix(1:12,ncol=5);A
Ïðåäóïðåæäåíèå
Inmatrix(1:12,ncol=5):
äëèíàäàííûõ[12]íåÿâëÿåòñÿìíîæèòåëåìêîëè÷åñòâàñòîëáöîâ[5]
[,1][,2][,3][,4][,5]
[1,]147101
[2,]258112
[3,]369123
�B=matrix(1:12,nrow=5);B
Ïðåäóïðåæäåíèå
Inmatrix(1:12,nrow=5):
äëèíàäàííûõ[12]íåÿâëÿåòñÿìíîæèòåëåìêîëè÷åñòâàñòðîê[5]
[,1][,2][,3]
[1,]1611
[2,]2712
[3,]381
[4,]492
[5,]5103
Ôóíêöèè
nrow(A)
,
ncol(A)
è
dim(A)
âîçâðàùàþò÷èñëîñòðîê,÷èñëî
ñòîëáöîâèðàçìåðíîñòüìàòðèöû
A
ñîîòâåòñòâåííî.
�nrow(A)
[1]3
�ncol(B)
[1]3
86
�dim(A)
[1]35
Ôóíêöèÿ
cbind(A,B)
�Ñ=cbind(A,B)
ñîçäà¼òìàòðèöóèçìàòðèö(âåêòîðîâ),ïðèïèñûâàÿñïðàâàê
A
ìàòðèöó(âåê-
òîð)
B
(äëÿýòîãî÷èñëîñòðîêó
A
è
B
äîëæíîñîâïàäàòü).
�A=matrix(1:12,nrow=3);A
[,1][,2][,3][,4]
[1,]14710
[2,]25811
[3,]36912
�B=matrix(13:24,nrow=3);B
[,1][,2][,3][,4]
[1,]13161922
[2,]14172023
[3,]15182124
�Z=cbind(A,B);Z
[,1][,2][,3][,4][,5][,6][,7][,8]
[1,]1471013161922
[2,]2581114172023
[3,]3691215182124
Ôóíêöèÿ
rbind(A,B)
�A=rbind(A,B)
ñîçäà¼òìàòðèöó,ïðèïèñûâàÿñíèçóêìàòðèöå
A
ìàòðèöó
B
(äëÿýòîãî÷èñ-
ëîñòîëáöîâóèñõîäíûõìàòðèöäîëæíîñîâïàäàòü).Çàìåòèì,÷òîâñïèñêàõ
àðãóìåíòîâôóíêöèé
cbind()
è
rbind
ìîæíîóêàçàòüáîëååäâóõìàòðèö.
�Z=rbind(A,B);Z
[,1][,2][,3][,4]
[1,]14710
[2,]25811
[3,]36912
[4,]13161922
[5,]14172023
[6,]15182124
×òîáûçàäàòüäèàãîíàëüíóþìàòðèöóäîñòàòî÷íîâîñïîëüçîâàòüñÿôóíê-
öèåé
diag(x,nrow,ncol)
.
87
�diag(1,3,3)
[,1][,2][,3]
[1,]100
[2,]010
[3,]001
Äëÿïîñòðîåíèÿêâàäðàòíîéåäèíè÷íîéìàòðèöûíóæíîçàäàòüòîëüêî÷èñëî
ñòðîê
nrow
âìàòðèöå(åñëèçàäàòü÷èñëîñòîëáöîâ
ncol
,òîáóäåòâûâåäåíî
ñîîáùåíèåîáîøèáêå).
�diag(nrow=4)
[,1][,2][,3][,4]
[1,]1000
[2,]0100
[3,]0010
[4,]0001
Åñëèàðãóìåíò
Õ
ôóíêöèè
diag(Õ)
åñòüìàòðèöà,òîâðåçóëüòàòåïðè-
ìåíåíèÿôóíêöèèáóäåòïîñòðîåíâåêòîðèçýëåìåíòîâ
Õ
,ðàñïîëîæåííûõíà
ãëàâíîéäèàãîíàëè.
�X=matrix(1:16,nrow=4);X
[,1][,2][,3][,4]
[1,]15913
[2,]261014
[3,]371115
[4,]481216
�diag(X)
[1]161116
Ïîñëåòîãî,êàêìàòðèöàñîçäàíà,ååìîæíîèçìåíÿòü,ïðèñâàèâàÿå¼ýëå-
ìåíòàìíîâûåçíà÷åíèÿ(ñì.èíäåêñû).Åñòüèäðóãîéñïîñîáðåäàêòèðîâàíèÿ
ìàòðèöû.Âìåíþêîíñîëèíàäîâûáðàòü
Ïðàâêà
,çàòåìíàæàòü
Ðåäàêòîð
äàííûõ
èââûâåäåííîìîêíåçàäàòüèìÿíóæíîéìàòðèöû(òîæåñàìîåìîæ-
íîñäåëàòüèïðèïîìîùèôóíêöèè
x(èìÿîáúåêòà)
).Âðåçóëüòàòåáóäåò
âûâåäåíîíîâîåðàáî÷ååîêíî,ïîõîæååíàñòðàíèöó
MSExcel
.
Ïðèìåð31.
Ñîçäàäèììàòðèöó
A
A=matrix(1:16,nrow=4);A
[,1][,2][,3][,4]
[1,]15913
[2,]261014
[3,]371115
[4,]481216
88
èèçìåíèìå¼âðåäàêòîðå.
Ðèñ.5.1:Ðåäàêòîðìàòðèö
Âðåäàêòîðåòàêæåìîæíîïðèñâàèâàòü(ìåíÿòü)èìåíàñòðîêàìèñòîëá-
öàì.
5.3.2Îïåðàöèèíàäìàòðèöàìè
Àðèôìåòè÷åñêèåîïåðàöèèíàäìàòðèöàìèîñóùåñòâëÿþòñÿïîýëåìåíòíî,
ïîýòîìó,÷òîáû,êïðèìåðó,ñëîæèòüäâåìàòðèöû,îíèäîëæíûèìåòüîäèíà-
êîâûåðàçìåðû:
A=matrix(1:9,nrow=3);A
[,1][,2][,3]
[1,]147
[2,]258
[3,]369
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]
[1,]024
[2,]-202
[3,]-4-20
Âïðî÷åì,ìîæíîîñóùåñòâëÿòüñìåøàííûåîïåðàöèè,êîãäàîäèíèçîïå-
ðàíäîâìàòðèöà,àäðóãîéâåêòîð(â÷àñòíîñòè,ñêàëÿð).Âýòîìñëó÷àå
ìàòðèöàðàññìàòðèâàåòñÿêàêâåêòîð,ñîñòàâëåííûéèçååýëåìåíòîâ,çàïè-
ñàííûõïîñòîëáöàì,èäåéñòâóþòòåæåïðàâèëà,÷òîèäëÿàðèôìåòè÷åñêèõ
îïåðàöèéíàäâåêòîðàìè:
�A+3
[,1][,2][,3]
[1,]4710
[2,]5811
[3,]6912
�B+3
[,1][,2][,3]
[1,]210
[2,]-1-2-3
[3,]-4-5-6
�(1:3)*A
[,1][,2][,3]
[1,]147
[2,]41016
[3,]91827
�A*(1:3)
[,1][,2][,3]
[1,]147
[2,]41016
[3,]91827
�(1:9)+A
[,1][,2][,3]
[1,]2814
[2,]41016
[3,]61218
�B^2
[,1][,2][,3]
[1,]149
[2,]162536
[3,]496481
90
Åñëèäëèíûîáúåêòîâíåêðàòíû,òîâûâîäèòñÿïðåäóïðåæäåíèå
�(0:3)*A
[,1][,2][,3]
[1,]01214
[2,]2024
[3,]660
Ïðåäóïðåæäåíèå
In(0:3)*A:
äëèíàáîëüøåãîîáúåêòàíåÿâëÿåòñÿïðîèçâåäåíèåìäëèíûìåíüøåãîîáúåêòà
Ýëåìåíòàðíûåìàòåìàòè÷åñêèåôóíêöèèòàêæåïðèìåíÿþòñÿïîýëåìåíòíî:
�sqrt(A)
[,1][,2][,3]
[1,]1.0000002.0000002.645751
[2,]1.4142142.2360682.828427
[3,]1.7320512.4494903.000000
�log(abs(B))
[,1][,2][,3]
[1,]0.0000000.69314721.098612
[2,]1.3862941.60943791.791759
[3,]1.9459102.07944152.197225
Ôóíêöèÿ
outer(x,y,¾îïåðàöèÿ¿)
ïðèìåíÿåòçàäàííóþ
îïåðàöèþ
ê
êàæäîéïàðåýëåìåíòîââåêòîðîâ
x
è
y
.Ïîëó÷èììàòðèöó,ñîñòàâëåííóþèç
ðåçóëüòàòîââûïîëíåíèÿýòîéîïåðàöèè.×èñëîñòðîêìàòðèöûäëèíàâåê-
òîðà
x
,à÷èñëîñòîëáöîâäëèíàâåêòîðà
y
.
�x=1:5;x
[1]12345
�y=-2:3;y
[1]-2-10123
�outer(x,y,"*")
[,1][,2][,3][,4][,5][,6]
[1,]-2-10123
[2,]-4-20246
[3,]-6-30369
[4,]-8-404812
[5,]-10-5051015
�outer(x,y,"^")
[,1][,2][,3][,4][,5][,6]
[1,]1.00000001.00000001111
91
[2,]0.25000000.50000001248
[3,]0.11111110.333333313927
[4,]0.06250000.2500000141664
[5,]0.04000000.20000001525125
Âìåñòî
outer(x,y,"*")
(âíåøíååïðîèçâåäåíèåâåêòîðîâ)ìîæíîèñïîëüçî-
âàòü
x%o%y
:
�x%o%y
[,1][,2][,3][,4][,5][,6]
[1,]-2-10123
[2,]-4-20246
[3,]-6-30369
[4,]-8-404812
[5,]-10-5051015
Òðàíñïîíèðîâàíèåìàòðèöûîñóùåñòâëÿåòôóíêöèÿ
t(A)
,àìàòðè÷íîå
ïðîèçâåäåíèåîïåðàöèÿ
%*%
:
�A=matrix(1:9,nrow=3);
�B=matrix(-(1:9),ncol=3,byrow=T)
�A%*%B
[,1][,2][,3]
[1,]-66-78-90
[2,]-78-93-108
[3,]-90-108-126
�B%*%A
[,1][,2][,3]
[1,]-14-32-50
[2,]-32-77-122
[3,]-50-122-194
Äëÿðåøåíèÿñèñòåìûëèíåéíûõóðàâíåíèé
Ax=b
ñêâàäðàòíîéíåâû-
ðîæäåííîéìàòðèöåé
A
åñòüôóíêöèÿ
solve(A,b)
:
�A=matrix(c(3,4,4,4),nrow=2);A
[,1][,2]
[1,]34
[2,]44
�b=c(1,0)
�solve(A,b)
[1]-11
92
Ñèñòåìûëèíåéíûõóðàâíåíèé,÷üèìàòðèöûêîýôôèöèåíòîâèìåþòâåðõíèé
òðåóãîëüíûéèëèíèæíèéòðåóãîëüíûéâèä(ò.å,ëèáîâñåýëåìåíòûïîäãëàâ-
íîéäèàãîíàëüþðàâíûíóëþ,ëèáîíàäãëàâíîéäèàãîíàëüþ)ìîæíîðåøàòüñ
ïîìîùüþôóíêöèé
backsolve(A,b)
èforwardsolve(Bb),ãäå
A
è
B
âåðõíÿÿ
òðåóãîëüíàÿèíèæíÿÿòðåóãîëüíàÿìàòðèöû,
b
âåêòîðñâîáîäíûõêîýôôè-
öèåíòîâ.
�A=matrix(c(3,0,4,4),nrow=2);A
[,1][,2]
[1,]34
[2,]04
�b=c(1,1)
�backsolve(A,b)
[1]0.000.25
�B=matrix(c(3,4,0,4),nrow=2);B
[,1][,2]
[1,]30
[2,]44
�forwardsolve(B,b)
[1]0.33333333-0.08333333
Ôóíêöèÿ
[1,]123
[2,]211
[3,]412
Àðãóìåíòûôóíêöèé:
X
èñõîäíûé÷èñëîâîéìàññèâ(ìàòðèöà),
na.rm

ëîãè÷åñêèéàðãóìåíò,íóæíîëèóáèðàòüèçðàññìîòðåíèÿ
NA
(ïîóìîë÷àíèþ
na.rm=FALSE
).
�A=matrix(1:12,nrow=3);A
[,1][,2][,3][,4]
[1,]14710
[2,]25811
[3,]36912
�colSums(A)
[1]6152433
�rowSums(A)
[1]222630
�colMeans(A)
[1]25811
�rowMeans(A)
[1]5.56.57.5
Ñîáñòâåííûåâåêòîðàèñîáñòâåííûå÷èñëàìàòðèö
6
íàõîäÿòñÿïðèïîìîùè
6
Íàïîìíèì,÷òîñîáñòâåííûìèçíà÷åíèÿìèìàòðèöû
A
íàçûâàþòñÿêîðíè

i
õàðàêòåðè-
ñòè÷åñêîãîóðàâíåíèÿ
j
A

I
j
=0
,ñîáñòâåííûåâåêòîðû
x
âåêòîðû,óäîâëåòâîðÿþùèå
Ax
=

i
x
95
[1,]0.59176950.7343437-0.01899586
[2,]0.3865001-0.2573049-0.83006716
[3,]0.7074083-0.62811910.55733982
Âîçâðàùàåòâåêòîðñîáñòâåííûõçíà÷åíèé,ðàñïîëîæåííûõâïîðÿäêåóáûâà-
íèÿèõìîäóëåé(ñîáñòâåííûåçíà÷åíèÿìîãóòáûòüèêîìïëåêñíûìè)èìàò-
ðèöó,÷üèñòîëáöûåñòüñîáñòâåííûåâåêòîðûèñõîäíîéìàòðèöû.
Ôóíêöèè
lower.tri(X,diag=FALSE)
è
upper.tri(X,diag=FALSE)
ñòðîÿòëîãè÷åñêèåìàòðèöû(÷üèðàçìåðíîñòèñîâïàäàþòñðàçìåðíîñòüþìàò-
ðèöû
X
),âêîòîðûõíàäèàãîíàëÿõíèæåãëàâíîé(
lower.tri(X,diag=
FALSE)
)èëèâûøåãëàâíîé(
upper.tri(X,diag=FALSE)
)ñòîÿòëîãè÷å-
ñêèåïåðåìåííûå
TRUE
.Àðãóìåíò
diag
îòâå÷àåòçàãëàâíóþäèàãîíàëü.
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)
�A=array(1:60,c(3,5,4))
Ïðèâûçîâåìàññèââûâîäèòñÿïîñëîéíî.
�A
,,1
[,1][,2][,3][,4][,5]
[1,]1471013
[2,]2581114
[3,]3691215
,,2
[,1][,2][,3][,4][,5]
[1,]1619222528
[2,]1720232629
[3,]1821242730
,,3
[,1][,2][,3][,4][,5]
[1,]3134374043
[2,]3235384144
[3,]3336394245
,,4
[,1][,2][,3][,4][,5]
[1,]4649525558
[2,]4750535659
[3,]4851545760
Ìîæíîçàäàòüèìåíàðàçìåðíîñòÿì:
�dim1=c('A','B','C')
�dim2=c('X1','X2','X3','X4','X5')
�dim3=c('Çèìà','Âåñíà','Ëåòî','Îñåíü')
�dimnames(A)=list(dim1,dim2,dim3)
�A
,,Çèìà
X1X2X3X4X5
A1471013
B2581114
C3691215
,,Âåñíà
X1X2X3X4X5
A1619222528
B1720232629
98
C1821242730
,,Ëåòî
X1X2X3X4X5
A3134374043
B3235384144
C3336394245
,,Îñåíü
X1X2X3X4X5
A4649525558
B4750535659
C4851545760
èïîñëåýòîãîîáðàùàòüñÿêýëåìåíòóïîèìåíèåãîñòðîêè,ñòîëáöà,ñëîÿèò.
ä.
�A[,,"Îñåíü"]
X1X2X3X4X5
A4649525558
B4750535659
C4851545760
�A[,'X2',"Çèìà"]
ABC
456
Îòìåòèì,÷òîïîäîáíûìîáðàçîìïðîèñõîäèòðàáîòàñìàññèâàìè,ñîñòîÿ-
ùèìèèçñèìâîëüíûõñòðîê,ëîãè÷åñêèìèìàññèâàìèèìàññèâàìè,ïîëó÷åí-
íûìèíàîñíîâåñïèñêîâ.
5.5Ñïèñêè
Ñïèñêèâ
R
ýòîêîëëåêöèèîáúåêòîâ,äîñòóïêêîòîðîììîæíîïðîèçâî-
äèòüïîíîìåðóèëèèìåíè.
Ñïèñîêìîæåòñîäåðæàòüîáúåêòû(êîìïîíåíòû)ðàçíûõòèïîâ,÷òîîò-
ëè÷àåòñïèñêèîòâåêòîðîâ.Êîìïîíåíòàìèñïèñêàìîãóòáûòüâòîì÷èñëå
âåêòîðûèäðóãèåñïèñêè.
Ôóíêöèÿ
�list(îáúåêò1,îáúåêò2,...)
ñîçäà¼òñïèñîê,ñîäåðæàùèéóêàçàííûåîáúåêòû.
Ïðèìåð33.
Ñîçäàäèìñïèñîê
writer
ñóêàçàííûìèïîëÿìè:ôàìèëèÿ,èìÿ
ãîäðîæäåíèÿ,ãîäñìåðòè,ëîãè÷åñêîåïîëåñåìåéíîãîïîëîæåíèÿ,ïðîôåññèÿ,
99
ÔÈÎæåíû(ìóæà)(åñëèíåò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]"Îòåëëî"
&#xNA00;$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.
Äëÿìàòðèöû
Õ
ñïîìîùüþôóíêöèè
7
Ñì.ðàçäåë5.3.2
105
Ïðèïðåîáðàçîâàíèèñïèñêàââåêòîðó÷èòûâàåòñÿèåðàðõèÿòèïîâäàííûõ
(
logical

integer

real

complex

character
).Ñîîòâåòñòâåííî,âðå-
çóëüòàòåïðåîáðàçîâàíèÿáóäåòïîëó÷åíâåêòîð,÷åéòèïäàííûõîïðåäåëÿåòñÿ
íàèáîëüøèìñîãëàñíîèåðàðõèèòèïîìêîìïîíåíòûñïèñêà.
Â
R
ïðåäóñìîòðåíàèîáðàòíàÿôóíêöèÿ
relist()
,ïîçâîëÿþùàÿâîññîçäàòü
ñïèñîê.Àðãóìåíòûôóíêöèè
relist()
:

esh
âåêòîð,êîòîðûéíóæíîïðåîáðàçîâàòüâñïèñîê;

Ïðèìåð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]ÀíäðåéÐóáëåâÈâàíîâîäåòñòâîÑòàëêåð
6Levels:ÀíäðåéÐóáëåâÇåðêàëîÈâàíîâîäåòñòâîíåçíàþ...Ñòàëêåð
èñíîâàïðîâåðèìíàóïîðÿäî÷åííîñòü.
�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]157321573215732157321573215732
�y=factor(x);y
[1]157321573215732157321573215732
109
Levels:12357
�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]111222333444555
Levels:12345

length
öåëî÷èñëåííûéàðãóìåíòäëèíàñîçäàâàåìîãîêàòåãîðèàëü-
íîãîâåêòîðà(ïîóìîë÷àíèþ
length=n

k
).Åñëèçàäàííàÿäëèíàìåíü-
øåïðîèçâåäåíèÿ
n

k
,òîáóäåòñîçäàíóñå÷¼ííûéâåêòîð,åñëèçàäàííàÿ
äëèíàáîëüøå,÷åì
n

k
,òîêàòåãîðèàëüíûéâåêòîðäëèíû
n

k
ïîâòîðÿ-
åòñÿäîòåõïîð,ïîêàåãîäëèíàíåñîâïàä¼òñçàäàííîé.
�gl(5,3,15)
[1]111222333444555
Levels:12345
�gl(5,3,12)
[1]111222333444
Levels:12345
�gl(5,3,25)
[1]1112223334445551112223334
Levels:12345
110

labels
ñèìâîëüíûéàðãóìåíò,âåêòîðäëèíû
n
,çàäàþùèéíàçâàíèÿ
êàòåãîðèé(ïîóìîë÷àíèþçàäàþòñÿ÷èñëîâûåêàòåãîðèè
labels=1:n
).
Êàòåãîðèèóïîðÿäî÷åíû.Åñëèâïåðâîíà÷àëüíîììàññèâåäàííûõáûëè
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]"ÀíäðåéÐóáëåâ""Èâàíîâîäåòñòâî""Ñòàëêåð"NA
[33]NANANANA
[37]NA"NaN""NaN""NaN"
[41]"NaN""NaN""NaN""NaN"
[45]"NaN""NaN"
Ïîñòðîèìòàáëèöó,îòáðàñûâàÿ
NA
è
NaN
�table(opros2)
opros2
ÀíäðåéÐóáëåâÇåðêàëîÈâàíîâîäåòñòâîíåçíàþÑîëÿðèñ
55555
Ñòàëêåð
6
èó÷èòûâàÿèõ
�table(opros2,exclude=NULL)
opros2
NaNÀíäðåéÐóáëåâÇåðêàëîÈâàíîâîäåòñòâîíåçíàþ
95555
ÑîëÿðèñÑòàëêåð&#xNA00;NA
566
Ïðèíàäëåæíîñòüîáúåêòàêêëàññó
table()
ïðîâåðÿåòñÿïðèïîìîùè
is.table()
,àïåðåâîäâýòîòêëàññ
as.table()
.
Ïðèìåð40.
Ïðîâåðèì,ïðèíàäëåæàòëèâåêòîðèôàêòîðêêëàññó
table()
è÷òîáóäåò,åñëèïîïûòàòüñÿïåðåâåñòèâåêòîðèôàêòîðâêëàññ
table()
.
112
�x=rep(c(1,5,7,3,2),6);x
[1]157321573215732157321573215732
�y=factor(x);y
[1]157321573215732157321573215732
Levels:12357
Ñîçäàëèâåêòîðèíàîñíîâåýòîãîâåêòîðàôàêòîð.Ïðîâåðèìèõíàïðèíàä-
ëåæíîñòüêêëàññó
table()
.
�is.table(x)
[1]FALSE
�is.table(y)
[1]FALSE
Ïîïðîáóåìðàññìàòðèâàòüâåêòîð
õ
êàê
table()
.
�z=as.table(x);z
ABCDEFGHIJKLMNOPQRSTUVWXYZ
15732157321573215732157321
A1B1C1D1
5732
�is.table(z)
[1]TRUE
Àíàëîãè÷íîèñôàêòîðîì
y
.
�z=as.table(y);z
Îøèáêàâas.table.default(y):íåìîãóïðåîáðàçîâàòüâòàáëèöó
�is.table(z)
[1]FALSE
Êàêïîêàçàíîâïðèìåðåâåêòîðà,ôàêòîðûèòàáëèöû(
table()
)ýòîðàç-
íûåñòðóêòóðû.Âåêòîðàìîæíîðàññìàòðèâàòüêàêòàáëèöû(ïðèýòîìêàæ-
äûéýëåìåíòâåêòîðàýòîñêîëüêîðàçâñòðå÷àåòñÿíåêàÿêàòåãîðèÿ,èìåíà
êàòåãîðèéîáðàçóþòñÿïðèïîìîùèëàòèíñêîãîàëôàâèòà),àôàêòîðûíåëü-
çÿ.
Òàáëèöóìîæíîïðåîáðàçîâàòüââåêòîð:
�as.vector(z)
[1]157321573215732157321573215732
Çàìåòèì,÷òîâýòîìñëó÷àå(âîòëè÷èåîòïðåîáðàçîâàíèÿôàêòîðîâââåêòî-
ðû)÷èñëàíåñòàíîâÿòñÿñèìâîëàìè.
113
5.7Òàáëèöûäàííûõ
Òàáëèöû(ôðåéìûäàííûõ)(dataframes)îäèíèçñàìûõâàæíûõòèïîâ
äàííûõâ
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
ãîäðîæäåíèÿãîäïîñòóïëåíèÿ
Èâàíîâ19882005
Óëüÿíîâ19872005
Êðàñíîâà19892006
Óñòþãîâ19892005
Äîáàâèìíîâûåñòîëáöûåñòüëèóñòóäåíòàçàäîëæåííîñòèèíàêàêîì
êóðñå.
114
n=c(FALSE,TRUE,FALSE,FALSE)
y=c(2,1,3,2)
Y1=data.frame(Y,n,y)
Y1
ãîäðîæäåíèÿãîäïîñòóïëåíèÿny
Èâàíîâ19852002FALSE2
Óëüÿíîâ19872004TRUE1
Êðàñíîâà19862003FALSE3
Óñòþãîâ19842001FALSE2
Ïåðåèìåíóåìïîñëåäíèåñòîëáöûèñíîâàâûâåäåìòàáëèöó
colnames(Y1)[3]="çàäîëæåííîñòü"
colnames(Y1)[4]="êóðñ"
Y1
ãîäðîæäåíèÿãîäïîñòóïëåíèÿçàäîëæåííîñòüêóðñ
Èâàíîâ19852002FALSE2
Óëüÿíîâ19872004TRUE1
Êðàñíîâà19862003FALSE3
Óñòþãîâ19842001FALSE2
Åñëèñðåäèàðãóìåíòîâôóíêöèè
data.frame()
åñòüñïèñîê,òîêàæäàÿåãî
êîìïîíåíòàïðåâðàùàåòñÿâñòîëáåöôðåéìàèèìåíàñòîëáöîâôîðìèðóþòñÿ
ñàìîñòîÿòåëüíî,÷òîâèäíîèçïðèìåðà.
�lst=list(1:4,FALSE,c("A","B"))
�data.frame(lst)
X1.4FALSE.c..A....B..
11FALSEA
22FALSEB
33FALSEA
44FALSEB
Äîñòóïêýëåìåíòàìòàáëèöûîñóùåñòâëÿåòñÿäâóìÿñïîñîáàìè:â¾ìàò-
ðè÷íîì¿èëè¾ñïèñêîâîì¿ñòèëÿõ.Âïåðâîìñëó÷àåóêàçûâàþòñÿíîìåðàèëè
èìåíàñòðîêèèñòîëáöà.Âîâòîðîìòàáëèöàðàññìàòðèâàåòñÿêàêñïèñîê,ýëå-
ìåíòàìèêîòîðîãîÿâëÿþòñÿñòîëáöûòàáëèöû,è÷òîáûîáðàòèòüñÿêåãîýëå-
ìåíòóíóæíîóêàçàòüèìÿèëèíîìåðýòîãîýëåìåíòà(ò.å.èìÿèëèíîìåð
ñòîëáöà),àçàòåìèìÿèëèíîìåðñòðîêè.
Ïðèìåð42.
Âåðí¼ìñÿêïðèìåðó41.
Y1["Èâàíîâ","êóðñ"]
115
[1]2
Y1["Óñòþãîâ",]
ãîä.ðîæäåíèÿãîä.ïîñòóïëåíèÿçàäîëæåííîñòüêóðñ
Óñòþãîâ19892005FALSE2
Y1[4,3]
[1]FALSE
Y1[,c(2,4)]
ãîä.ïîñòóïëåíèÿêóðñ
Èâàíîâ20052
Óëüÿíîâ20051
Êðàñíîâà20063
Óñòþãîâ20052
Y1$êóðñ
[1]2132
Y1[["çàäîëæåííîñòü"]][3]
[1]FALSE
Y1[["çàäîëæåííîñòü"]][2]
[1]TRUE
Ðåäàêòèðîâàòüòàáëèöûäàííûõìîæíîèâðåäàêòîðå(ñì.ðàçäåë5.3.1ýòîé
ãëàâû).
116
Ãëàâà6
ÂâîäèâûâîääàííûõâR
Âïàêåòå
R
ðåàëèçîâàíîáîëüøîåêîëè÷åñòâîôóíêöèé,ïîçâîëÿþùèõñ÷è-
òûâàòüäàííûåèçôàéëîâ,àòàêæåçàïèñûâàòüäàííûå.Âýòîéãëàâåáóäóò
ðàññìîòðåíûòîëüêîîñíîâíûåèçíèõ(ñîñòàëüíûìèôóíêöèÿìèââîäà/âûâîäà
æåëàþùèåìîãóòñàìîñòîÿòåëüíîîçíàêîìèòüñÿ).
6.1ÂâîääàííûõâR
Âýòîé÷àñòèðàññìîòðèìñëåäóþùèåôóíêöèè,ïîçâîëÿþùèåââîäèòüäàí-
íûåâ
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,

le
èìÿôàéëà,îòêóäàñ÷èòûâàþòñÿäàííûå;åñëè
le=
,òîïðîèçâî-
äèòñÿââîäñêëàâèàòóðû;åñëèçàäàíîòîëüêîèìÿôàéëà,òîîí(ôàéë)
èùåòñÿâòåêóùåéäèðåêòîðèè;èíà÷åçàäà¼òñÿïîëíûéïóòüêôàéëó;
ìîæåòáûòüèURL.

what
çàäà¼òñÿòèïñ÷èòûâàåìûõäàííûõ:
logical
,
integer
,
numeric
,
complex
,
character
,
list
;åñëèñ÷èòûâàåòñÿñïèñîê,òîñòðîêèâôàéëå
âîñïðèíèìàþòñÿêàêïîëÿñïèñêàóêàçàííûõâûøåòèïîâ;

nmax
öåëîåïîëîæèòåëüíîå÷èñëîìàêñèìàëüíîå÷èñëîäàííûõäëÿ
÷òåíèÿèëèìàêñèìàëüíîå÷èñëîçàïèñåéâñïèñêå;ïðèïðîïóñêåýòîãî
àðãóìåíòàèëèïðèíåïðàâèëüíîìåãîçàäàíèèôàéëñ÷èòûâàåòñÿäîêîí-
öà;

n
öåëîåïîëîæèòåëüíîå÷èñëîìàêñèìàëüíîå÷èñëîäàííûõäëÿ÷òå-
íèÿ;íåïðàâèëüíûåçíà÷åíèÿèëèíåòèïà
integer
èãíîðèðóþòñÿ

sep
ðàçäåëèòåëüïîëåé;ïîóìîë÷àíèþ-ïðîáåë;

quote
âèäêàâû÷åê(äâîéíûåèëèîäèíàðíûå);

dec
äåñÿòè÷íûéðàçäåëèòåëü(òî÷êàèëèçàïÿòàÿ);

skip
öåëîåïîëîæèòåëüíîå÷èñëî÷èñëîñòðîêôàéëà,êîòîðûåñëå-
äóåòïðîïóñòèòüïåðåä÷òåíèåì;

nlines
öåëîåïîëîæèòåëüíîå÷èñëîìàêñèìàëüíîå÷èñëîñòðîêäëÿ
ñ÷èòûâàíèÿ;

na.strings
ñèìâîëüíûéâåêòîðåãîýëåìåíòûèíòåðïðåòèðóþòñÿêàê
ïðîïóùåííûåçíà÷åíèÿ
NA
;ïóñòûåïîëÿïîóìîë÷àíèþñ÷èòûâàþòñÿ
êàê
NA
;

ush
ëîãè÷åñêèéàðãóìåíòçíà÷åíèå
TRUE
ïîçâîëÿåòäîáàâëÿòü
êîììåíòàðèèêñ÷èòûâàåìûìäàííûìïîñëåïîñëåäíåãîñ÷èòàííîãîïîëÿ
(íîíåáîëååîäíîãî);

ll
ëîãè÷åñêèéàðãóìåíòçíà÷åíèå
TRUE
äîáàâëÿþòñÿïóñòûåïîëÿ
êñòðîêàì,âêîòîðûõêîëè÷åñòâîïîëåéäàííûõìåíüøåîïðåäåë¼ííîãî
ïàðàìåòðîì
what
;

strip.white
ëîãè÷åñêèéâåêòîð;èñïîëüçóåòñÿòîëüêîåñëèçàäàíïà-
ðàìåòð
sep
,óäàëÿåòïóñòîåïðîñòðàíñòâî(ïðîáåë)ïåðåäñèìâîëüíûìè
ïåðåìåííûìèèïîñëåíèõ;
118

read.csv2(file,header=TRUE,sep=";",quote="\"",dec=",",
fill=TRUE,comment.char="",...)
read.delim(file,header=TRUE,sep="\t",quote="\"",dec=".",
fill=TRUE,comment.char="",...)
read.delim2(file,header=TRUE,sep="\t",quote="\"",dec=",",
fill=TRUE,comment.char="",...)
êîòîðûåîñîáåííîïîëåçíûïðèñ÷èòûâàíèèäàííûõèçôàéëîâ
Excel
.Äëÿ
ýòîãîèñõîäíûéôàéëñîõðàíÿåòñÿâôîðìàòå
.csv
èçàòåìïðî÷èòûâàåòñÿïðè
ïîìîùèîäíîéèç÷åòûð¼õïðèâåä¼ííûõâûøåôóíêöèé(ëó÷øåâîñïîëüçîâàòü-
ñÿ
read.csv2()
).
Àðãóìåíòû:

le
îáÿçàòåëüíûéàðãóìåíò,èìÿôàéëà;

header
ëîãè÷åñêèéïàðàìåòð;ïðèçíà÷åíèèTRUEñ÷èòûâàþòñÿèìåíà
ïåðåìåííûõèçôàéëà;

sep
ðàçäåëèòåëüïîëåé;ïîóìîë÷àíèþ-ïðîáåë;

quote
âèäêàâû÷åê(äâîéíûåèëèîäèíàðíûå);

dec
äåñÿòè÷íûéðàçäåëèòåëüâ÷èñëàõ(òî÷êàèëèçàïÿòàÿ);

row.names
âåêòîðèì¼íñòðîê;ïðåäñòàâëÿåòñîáîéëèáîâåêòîðñèìå-
íàìèñòðîêèòîãîâîéòàáëèöû;ëèáî÷èñëîíîìåðñòîëáöàèñõîäíîé
òàáëèöûñíàçâàíèÿìèñòðîê;ëèáîèìÿñòîëáöàñ÷èòûâàåìîéòàáëèöû,
ãäåïðèâåäåíûíàçâàíèÿñòðîê;åñëèýòîòïàðàìåòðíåçàäàí,òîñòðîêè
âèòîãîâîéòàáëèöåáóäóòïðîíóìåðîâàíû;

col.names
âåêòîðèì¼íñòîëáöîââèòîãîâîéòàáëèöå;ïîóìîë÷àíèþ

¾V

íîìåðñòîëáöà

¿
;

as.is
íóæíîëèñèìâîëüíûåïåðåìåííûå,íåïðåîáðàçîâàííûåâ÷èñëî-
âûåèëèëîãè÷åñêèå,ïåðåâîäèòüâôàêòîðû.
as.is
ëèáîëîãè÷åñêèé,ëè-
áî÷èñëîâîéâåêòîð,îïðåäåëÿþùèéñòîëáöû,íåêîíâåðòèðóåìûåâôàê-
òîðû.

colClasses
ñèìâîëüíûéâåêòîð;îïðåäåëÿåòêëàññûäàííûõâñòîëá-
öàõ(ñèìâîëüíûå,ëîãè÷åñêèå,÷èñëîâûå,äàòû).Âîçìîæíûåçíà÷åíèÿ:
NA
àâòîìàòè÷åñêàÿêîíâåðòàöèÿòèïîâäàííûõ,
NULL
ñòîëáåö
ïðîïóñêàåòñÿ(äàííûåíåïðåîáðàçîâûâàþòñÿ),
òèïäàííûõ
âêîòîðûé
áóäóòïåðåâåäåíûýëåìåíòûñòîëáöà,
factor
;
120

na.strings
ñèìâîëüíûéâåêòîð,ýëåìåíòûêîòîðîãîïðè÷òåíèèèñõîä-
íîéòàáëèöûâôàéëåáóäóòèíòåðïðåòèðîâàòüñÿêàê
NA
;

nrows
öåëî÷èñëåííûéàðãóìåíò;îïðåäåëÿåòìàêñèìàëüíîå÷èñëîñ÷è-
òûâàåìûõñòðîê;

skip
ïîëîæèòåëüíûéöåëî÷èñëåííûéàðãóìåíò;îïðåäåëÿåò÷èñëî
ñòðîê,ïðîïóñêàåìûõïåðåä÷òåíèåì;

check.names
ëîãè÷åñêèéàðãóìåíò;ïðèçíà÷åíèè
TRUE
èìåíàïåðå-
ìåííûõáóäóòïðîâåðåíûíàñèíòàêñè÷åñêóþïðàâèëüíîñòüèîòñóòñòâèå
äóáëèðîâàíèÿ;

ll
ëîãè÷åñêèéàðãóìåíò;ïðèçíà÷åíèè
TRUE
ñòðîêèðàçíîéäëèíû
áóäóòïðèâåäåíûêåäèíîé(ìàêñèìàëüíîé)äîáàâëåíèåìïóñòûõïîëåé;

strip.white
ëîãè÷åñêèéàðãóìåíò;èñïîëüçóåòñÿòîëüêîåñëèîïðåäåë¼í
ðàçäåëèòåëü
sep
,ïîçâîëÿåòóáèðàòüïðîáåëûïåðåäèïîñëåñèìâîëüíûõ
ïåðåìåííûõ;
Ïðèìå÷àíèÿ:

Ôóíêöèÿ
read.table()
ÿâëÿåòñÿîñíîâíîéäëÿñ÷èòûâàíèÿäàííûõèç
òàáëèö.

Ïîëåòàáëèöûñ÷èòàåòñÿïóñòûì,åñëèâí¼ìíè÷åãîíåò(äîçíàêàêîì-
ìåíòàðèÿèëèäîñèìâîëàîêîí÷àíèÿñòðîêè).

Åñëèïàðàìåòð
row.names
íåîïðåäåë¼í(ò.å.íåçàäàíûèìåíàñòðîêðå-
çóëüòèðóþùåéòàáëèöû),àäëèíàçàãîëîâêàíàåäèíèöóìåíüøå÷èñëà
ñòîëáöîâ,òîïåðâûéñòîëáåöáóäåòðàññìàòðèâàòüñÿêàêñòîëáåöñíà-
çâàíèÿìèñòðîê.

×èñëîñòîëáöîââñ÷èòûâàåìîéòàáëèöåîïðåäåëÿåòñÿàâòîìàòè÷åñêèïî-
ñëåïðî÷òåíèÿïåðâûõïÿòèñòðîê.

Âñ¼,÷òîíàõîäèòñÿâèñõîäíîéòàáëèöåïîñëåçíàêîâêîììåíòàðèÿ,íå
ñ÷èòûâàåòñÿ.

Çàäàíèåïàðàìåòðà
nrows
(ïóñòüäàæåñî÷åíüèçáûòî÷íûìèçíà÷åíèÿ-
ìè)ïîçâîëÿåòóìåíüøèòüçàòðàòûïàìÿòè.

Äëÿ÷òåíèÿáîëüøèõìàòðèöïðåäïî÷òèòåëüíååèñïîëüçîâàòüôóíêöèþ
scan()
.
121
6.2ÂûâîääàííûõâR
Çäåñüìûðàññìîòðèìñëåäóþùèåôóíêöèè:

write()
;

cat()
;

write.table()
,
write.csv()
è
write.csv()
.
6.2.1Ôóíêöèÿwrite()
Ôóíêöèÿ
write()
ïðåäíàçíà÷åíàäëÿçàïèñèäàííûõ(âîñíîâíîììàòðèö)
â
R
.żâèä:
write(x,file="data",
ncolumns=if(is.character(x))1else5,
append=FALSE,sep="")
Àðãóìåíòû:

x
äàííûå,êîòîðûåíóæíîçàïèñàòü;

le
èìÿôàéëà,êóäàáóäåòçàïèñàíàèíôîðìàöèÿ;

ncolumns
÷èñëîñòîëáöîâ,âêîòîðûõáóäåòçàïèñàíàèíôîðìàöèÿ;

append
ëîãè÷åñêèéàðãóìåíòåñëèçíà÷åíèå
TRUE
,òîäàííûåäî-
ïèøóòñÿâèñõîäíûéôàéë,åñëèæå
FALSE
,òîôàéëáóäåòïåðåïèñàí
çàíîâî;

sep
ðàçäåëèòåëüñòîëáöîâ(
n
t
òàáóëÿöèÿ)
Åñëèâêà÷åñòâåàðãóìåíòàôóíêöèèçàäàòüòîëüêîäàííûå
write(x)
,òî
îíèáóäóòâûâåäåíûíàýêðàí.
6.2.2Ôóíêöèÿcat()
Ôóíêöèÿ
cat()
èìååòâèä
cat(...,file="",sep="",fill=FALSE,labels=NULL,
append=FALSE)
żàðãóìåíòû:

...
îáúåêòû
R
,êîòîðûåáóäóòçàïèñàíûâôàéë.
122

le
èìÿôàéëà,êóäàáóäåòçàïèñàíàèíôîðìàöèÿ;åñëèýòîòàðãóìåíò
îòñóòñòâóåò,òîäàííûåáóäóòâûâåäåíûíàýêðàí;

sep
ðàçäåëèòåëüýëåìåíòîâçàïèñûâàåìîãîîáúåêòà;

ll
ëîãè÷åñêèéèëèïîëîæèòåëüíûéöåëî÷èñëåííûéàðãóìåíòêîí-
òðîëèðóåòñîçäàíèåíîâûõñòðîêâçàïèñûâàåìîìôàéëå.Åñëè
ll
÷èñ-
ëîâîé,òîçàäà¼òñÿäëèíàñòðîêè(êîëè÷åñòâîñèìâîëîââñòðîêå).Åñëè
ll
ëîãè÷åñêèéèåãîçíà÷åíèå
FALSE
,òîíîâûåñòðîêèñîçäàþòñÿ
òîëüêîïðèíàëè÷èèâçàïèñûâàåìûõäàííûõñèìâîëà

n
n

;åñëèçíà÷å-
íèå
TRUE
,òîçàäà¼òñÿäîïîëíèòåëüíûéàðãóìåíò
width
,îïðåäåëÿþ-
ùèéäëèíóñîçäàâàåìîéâôàéëåñòðîêè;

labels
ñèìâîëüíûéâåêòîð,çàäàþùèéíàçâàíèÿñòðîê.Èãíîðèðóåòñÿ,
åñëèåñëèàðãóìåíò
ll=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,

le
èìÿôàéëà,âêîòîðûéáóäóòçàïèñàíûäàííûå.

append
ëîãè÷åñêèéàðãóìåíòèñïîëüçóåòñÿ,åñëèòîëüêîçàäàíîèìÿ
ôàéëà.Åñëèçíà÷åíèåàðãóìåíòà
TRUE
,òîíîâûåäàííûåäîáàâëÿþòñÿ
êèñõîäíîìóôàéëó,åñëè
FALSE
çàïèñûâàþòñÿâìåñòîñòàðûõ.

quote
ëîãè÷åñêèéèëè÷èñëîâîéàðãóìåíò.Åñëè
quote
ÿâëÿåòñÿëî-
ãè÷åñêèìàðãóìåíòîìèåãîçíà÷åíèå
TRUE
,òîâñåñèìâîëüíûåïåðå-
ìåííûåèôàêòîðûáóäóòçàïèñàíûâôàéëâäâîéíûõêàâû÷êàõ.Åñëè
çíà÷åíèåàðãóìåíòà
FALSE
ñèìâîëüíûåïåðåìåííûåèôàêòîðûçàïè-
ñûâàþòñÿáåçêàâû÷åê.Åñëè
quote
÷èñëîâîéâåêòîð,òîåãîýëåìåíòû
çàäàþòíîìåðàñòîëáöîâ,âêîòîðûõäàííûåäîëæíûáûòüçàïèñàíûâ
êàâû÷êàõ.Íàçâàíèÿñòîëáöîâèñòðîêïîóìîë÷àíèþáóäóòçàïèñàíûâ
êàâû÷êàõ.

sep
àðãóìåíò,îïðåäåëÿþùèéðàçäåëèòåëüïîëåé;çíà÷åíèÿâêàæäîé
ñòðîêåèñõîäíûõäàííûõðàçäåëÿþòñÿýòèìñèìâîëîì.

eol
ñèìâîëîêîí÷àíèÿñòðîêè;äëÿWindowsèUnix-
n
n
,
n
r
;äëÿ
MacOS-
n
r
.

na
ñèìâîëüíûéàðãóìåíòäëÿîáîçíà÷åíèÿîòñóòñòâóþùèõýëåìåíòîâ
âèñõîäíûõäàííûõ.

dec
äåñÿòè÷íûéðàçäåëèòåëüâ÷èñëàõ(òî÷êàèëèçàïÿòàÿ).

row.names
àðãóìåíò,çàäàþùèéíàçâàíèÿñòðîêâôàéëå.Àðãóìåíò
ëèáîëîãè÷åñêèé(åñëèçíà÷åíèå
TRUE
,òîèñïîëüçóþòñÿèìåíàñòðîê,
óêàçàííûåâèñõîäíûõäàííûõ),ëèáîïðåäñòàâëÿåòñîáîéñèìâîëüíûé
âåêòîð,íåïîñðåäñòâåííîçàäàþùèéèìåíàñòðîê.

col.names
àíàëîãè÷íûéàðãóìåíò,çàäàþùèéèìåíàñòîëáöîâ.


Äåéñòâèòåëüíûåèêîìïëåêñíûå÷èñëàçàïèñûâàþòñÿñìàêñèìàëüíîâîç-
ìîæíîéòî÷íîñòüþ.

Åñëèâèñõîäíîéçàïèñûâàåìîéòàáëèöåäàííûõèìåëèñüñòîëáöûñìàò-
ðè÷íîéñòðóêòóðîé,òîïðèçàïèñèêàæäûéèçñòîëáöîâýòîéìàòðèöû
áóäåòïðåäñòàâëåíââèäåîòäåëüíîãîñòîëáöà;âñâÿçèñýòèìàðãóìåíòû
col.names
è
quote
äîëæíûñîîòâåòñòâîâàòü÷èñëóñòîëáöîâðåçóëüòè-
ðóþùåéòàáëèöû,àíåèñõîäíîé.

Êàæäûéñòîëáåöâòàáëèöåäàííûõ,ÿâëÿþùèéñÿñïèñêîìèëèäàòîé,
áóäåòïåðåêîíâåðòèðîâàíâñèìâîëû.

Òîëüêîñèìâîëüíûåñòîëáöû(èëèñòîëáöû,ïåðåêîíâåðòèðîâàííûåâñèì-
âîëû)áóäóòïðèçàïèñèçàêëþ÷åíûâêàâû÷êè(åñëèýòîóêàçàíîâàðãó-
ìåíòå
quote
)

Àðãóìåíò
dec
ïðèìåíÿåòñÿòîëüêîêòåìñòîëáöàì,êîòîðûåíåáûëè
ïåðåêîíâåðòèðîâàíûâñèìâîëüíûå(ò.å.òîëüêîê÷èñëîâûìäàííûì).
*.CSVôàéëû
.
Ôóíêöèè
write.csv()
è
write.csv2()
èñïîëüçóþòñÿäëÿçàïèñèôàéëîââ
ôîðìàòå
*.csv
.
Åñëèïàðàìåòð
row.names=TRUE
,òîçíà÷åíèÿïàðàìåòðîâ
Ãëàâà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
$lheight
$ljoin
$lmitre
$lty
$lwd
$mai
$mar
$mex
$mfcol
$mfg
$mfrow
$mgp
$mkh
$new
$oma
$omd
$omi
$pch
$pin
$plt
$ps
$pty
$smo
$srt
129
$tck
$tcl
$usr
$xaxp
$xaxs
$xaxt
$xpd
$yaxp
$yaxs
$yaxt
Âòîðîéàðãóìåíòôóíêöèè
par(...,no.readonly=FALSE)

no.readonly
ëîãè÷åñêèéàðãóìåíò,åñëèåãîçíà÷åíèå
TRUE
èíåçàäàíû
äðóãèåàðãóìåíòû,òîáóäóòâûâåäåíûòîëüêîòåïàðàìåòðûôóíêöèè
par()
,
êîòîðûåóñòàíîâëåíûäëÿòåêóùåãîãðàôè÷åñêîãîîêíàèìîãóòáûòüóñòàíîâ-
ëåíû(èçìåíåíû)âäàëüíåéøåì.Òàêèõïàðàìåòðîâ65.
Òàêèìîáðàçîì,òîëüêî5àðãóìåíòîâôóíêöèè
par()
íåìîãóòáûòüçàäàíû
è(èëè)èçìåíåíûïîëüçîâàòåëåì.Ýòîñëåäóþùèåàðãóìåíòû:

cin
ðàçìåð(øèðèíà,âûñîòà)ñèìâîëîââäþéìàõ,

cra
ðàçìåð(øèðèíà,âûñîòà)ñèìâîëîââðàñòðàõ(ïèêñåëÿõ)(íåêî-
òîðûåãðàôè÷åñêèåóñòðîéñòâàóñòàíàâëèâàþòïðîèçâîëüíûéðàçìåð
ïèêñåëÿ,îáû÷íîîêîëî
1
=
72
äþéìà),

csi
âûñîòàñèìâîëàâäþéìàõ,

cxy
ðàçìåðñèìâîëàâïîëüçîâàòåëüñêèõåäèíè-
öàõ(åäèíèöûðàçìåðà,îïðåäåë¼ííûåïîëüçîâàòåëåì
par(cxy)=par(cin)/par(pin)
)è,íàêîíåö,

din
ðàçìåðûãðàôè÷åñêîãîóñòðîéñòâàâäþéìàõ.
Èçîñòàâøèõñÿ65àðãóìåíòîâ(ïàðàìåòðîâãðàôè÷åñêîãîîêíà)ñëåäóþùèå
(21)ìîãóòáûòüçàäàíûòîëüêîâôóíêöèè
par()
:

ask
ëîãè÷åñêèéàðãóìåíò(ïîóìîë÷àíèþçíà÷åíèå
FALSE
);ïðè
çíà÷åíèè
TRUE
âèíòåðàêòèâíîìðåæèìåçàïðàøèâàåòðàçðåøåíèåíà
ïîñòðîåíèåíîâîãîèçîáðàæåíèÿ;

g
÷èñëîâîéâåêòîð
c(x1,x2,y1,y2)
çàäà¼òêîîðäèíàòûîáëàñòè
âûâîäèìîãîðèñóíêàíàãðàôè÷åñêîìîáúåêòå(ïîóìîë÷àíèþñòðîèòñÿ
íîâûéîáúåêò,÷òîáûäîáàâèòüñîçäàâàåìûéêóæåñóùåñòâóþùåìóíåîá-
õîäèìîçàäàòüïàðàìåòð
new=T
);
n=c(width,height)
ïàðàìåò-
ðûîáëàñòè,íàêîòîðîéñòðîèòñÿèçîáðàæåíèå,âäþéìàõ(ïîóìîë÷àíèþ
ñòðîèòñÿíîâûéîáúåêòñçàäàííûìïàðàìåòðîì);

lheight
÷èñëîâîéàðãóìåíò,çàäàþùèéìíîæèòåëüâûñîòûñòðîêè
âûâîäèìîãîòåêñòà(âûñîòàñòðîêè
=
âûñîòàñèìâîëà(ïîóìîë÷àíèþ)

ìíîæèòåëüâûñîòûñèìâîëà

ìíîæèòåëüâûñîòûñòðîêè),èñïîëüçóåòñÿ
â
text()
;
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êî-
îðäèíàòûíîðìèðîâàííûåêîîðäèíàòûóñòðîéñòâà(normalizeddevice
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
Ðèñ.7.1:
sin
(
x
)
ïðèðàçëè÷íûõçíà÷åíèÿõàðãóìåíòà
type
äâîéíîéøòðèõ(ñëåäóåòîòìåòèòü,÷òîäîëæåíáûòüçàäàíïàðàìåòð
type=l
).Ðåçóëüòàòûèñïîëüçîâàíèÿýòîãîïàðàìåòðàïðåäñòàâëå-
íûíàðèñ.7.2
xlab
,
ylab
,
main
è
sub
ñèìâîëüíûåïåðåìåííûåíàçâàíèÿîñåé,
îñíîâíîãîèäîïîëíèòåëüíîãîçàãîëîâêîâãðàôèêà(áîëååïîäðîáíî
ñì.ðàçäåëû7.2.2è7.2.3ýòîéãëàâû).
col
ñèìâîëüíûéèëè÷èñëîâîéàðãóìåíòöâåòãðàôèêà.Ïîäðîá-
íååñì.ðàçäåë7.1.6äàííîéãëàâû.
bg
ñèìâîëüíàÿïåðåìåííàÿ,îòâå÷àþùàÿçàöâåòôîíàãðàôè÷å-
ñêîãîîêíà.Ïîóìîë÷àíèþáåëûé
bg=white
.
133
Ðèñ.7.2:
sin
(
x
)
ïðèðàçëè÷íûõçíà÷åíèÿõàðãóìåíòà
type
col.axis
ñèìâîëüíàÿïåðåìåííàÿöâåòîñåé.Ïîóìîë÷àíèþ÷¼ð-
íûé
black
.
col.main
ñèìâîëüíàÿïåðåìåííàÿöâåòîñíîâíîãîçàãîëîâêà.Ïî
óìîë÷àíèþ÷¼ðíûé
black
.
col.sub
ñèìâîëüíàÿïåðåìåííàÿöâåòäîïîëíèòåëüíîãîçàãî-
ëîâêà.Ïîóìîë÷àíèþ÷¼ðíûé
black
.
fg
ñèìâîëüíàÿïåðåìåííàÿöâåòðàìêèâîêðóããðàôèêà.
font.axis
,
font.lab
,
font.main
è
font.sub
÷èñëîâûåàðãóìåíòû
òèïøðèôòàäëÿíàçâàíèÿîñåé,îñíîâíîãîèäîïîëíèòåëüíîãî
çàãîëîâêîâ.Âîçìîæíûåçíà÷åíèÿ:
1
ïðîñòîé(ïîóìîë÷àíèþ),
2
æèðíûé;
3
êóðñèâè
4
æèðíûéêóðñèâ.
134
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
Ðèñ.7.3:Ãðàôèêôóíêöèè
y
=cos(
x
)
íàîòðåçêå
[

2

;2

]

axes
ëîãè÷åñêèéïàðàìåòðâûâîäèòüîáåîñèèëèíåò(çíà÷åíèå
axes=NULL
òàêæåïîäàâëÿåòâûâîäîñåé).

xaxt
è
yaxt
ñèìâîëüíûåàðãóìåíòûóáèðàþòîñü
0x
(
xaxt='n'

îñü
0y
(
yaxt='n'
).

frame.plot
ëîãè÷åñêèéïàðàìåòð;ïðèçíà÷åíèè
TRUE
ñòðîèòñÿðàì-
êàâîêðóããðàôèêà.

panel.rst
âûðàæåíèåîöåíèâàåòñÿïîñëåïîñòðîåíèÿîñåéíàãðà-
ôèêå,íîäîïîñòðîåíèÿñàìîãîãðàôèêà.Ïîëåçíîïðèïîñòðîåíèèñåòêè
íàãðàôèêå.

panel.last
âûðàæåíèåîöåíèâàåòñÿïîñëåâûâîäàãðàôèêà.

asp
ñîîòíîøåíèå
y=x
.

lab
÷èñëîâîéâåêòîðâèäà
c(x,y,len)
,ãäå
x
è
y
÷èñëîìåòîêíàñî-
îòâåòñòâóþùèõîñÿõ,
len
äëèíàïîäïèñåéêìåòêàì(íåèñïîëüçóåìûé
ïàðàìåòð).Çíà÷åíèåàðãóìåíòàïîóìîë÷àíèþ
lab=c(5,5,7)
.

las
÷èñëîâîéàðãóìåíòñòèëüíàçâàíèÿîñåéîòíîñèòåëüíîñàìèõ
îñåé.Âîçìîæíûåçíà÷åíèÿ:
0
íàçâàíèÿâûâîäÿòñÿïàðàëëåëüíîîñÿì
(ïîóìîë÷àíèþ),
1
ãîðèçîíòàëüíîðàñïîëîæåíèå,
2
ïåðïåíäèêóëÿð-
íî,
3
âåðòèêàëüíî.
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()
óäàëåíèåâñåõâûáðàííûõïîäîêîí.
Àðãóìåíòûýòèõôóíêöèé:

gs
÷èñëîâîéâåêòîð,ñîñòîÿùèéèçäâóõýëåìåíòîâ÷èñëîïîäîêîí
ïîñòðîêàìèïîñòîëáöàì(
gs=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]12345
Èòîãî5îêîí:äâàáîëüøèõèòðèìàëåíüêèõ÷àñòèâòîðîãîáîëüøîãî
îêíà.
Âûáåðåìïåðâîåîêíîèïîñòðîèìâí¼ìãðàôèê.
screen(1)
plot(cos,-pi,pi)
Òåïåðüïîñòðîèìãðàôèêâ4îêíå.
138
Ðèñ.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="ylab4")
Âîçâðàùàåìñÿêïåðâîìóîêíó,íîíåñòèðàåìåãî.
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)),
Ðèñ.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
(
x
i
;y
j
))
ij
.

nlevels
÷èñëîêîíòóðíûõóðîâíåé,åñëèíåçàäàíàðãóìåíò
levels
.

levels
÷èñëîâîéâåêòîðóðîâíåé,âêîòîðûõñòðîÿòñÿêîíòóðíûåëèíèè.

labels
ñèìâîëüíûéâåêòîðìåòêèêîíòóðíûõëèíèé.

labcex
ðàçìåðêîíòóðíûõëèíèé.

drawlabels
ëîãè÷åñêèéàðãóìåíòâûâîäìåòîêêîíòóðíûõëèíèé.

'simple'
ìåòêèâûâîäÿòñÿíàêðàþãðàôèêàèíàêëàäûâàþòñÿíàêîí-
òóðíóþëèíèþ.
'edge'
ìåòêèâûâîäÿòñÿíàêðàþãðàôèêà,âïèñûâà-
þòñÿâêîíòóðíóþëèíèþ,ðàçëè÷íûåìåòêèíåïåðåñåêàþòñÿ.
'attest'
ìåòêèâñòðàèâàþòñÿâïëîñêîé÷àñòèêîíòóðíîéëèíèè,ìåòêèíåïåðå-
ñåêàþòñÿ.Çàäàíèåâòîðîãîèëèòðåòüåãîìåòîäàìîæåòïðèâåñòèêòîìó,
÷òîíåâñåìåòêèáóäóòâûâåäåíû.

vfont
çàäàíèåñåìåéñòâàøðèôòîâäëÿìåòîê.Åñëèçíà÷åíèå
NULL
èñïîëüçóåòñÿñåìåéñòâîøðèôòîâïîóìîë÷àíèþ.

xlim
,
ylim
è
zlim
ïðåäåëüíûåçíà÷åíèÿäëÿ
x
,
y
è
z
.

axes
è
frame.plot
ëîãè÷åñêèåàðãóìåíòûíóæíîëèñòðîèòüîñèè
ðàìêóâîêðóããðàôèêà.

col
ñèìâîëüíûéàðãóìåíòöâåòêîíòóðíûõëèíèé.

lty
òèïëèíèè.

lwd
òîëùèíàëèíèè.

add
ëîãè÷åñêèéàðãóìåíòíóæíîëèäîáàâèòüêîíòóðíûåëèíèèê
óæåñóùåñòâóþùåìóãðàôèêó(
add=TRUE
)èëèíàäîñîçäàòüíîâûé.

:::
äîïîëíèòåëüíûåàðãóìåíòû,îòâå÷àþùèåçàâèäîñåé,çàãîëîâêîâ
èïð.
Ïðèìåð47.
Èçîáðàçèì¾óçîðïåðñèäñêîãîêîâðà¿(ðèñ.7.6).
Çàäàäèìçíà÷åíèÿâåêòîðîâ
x
è
y
x-y-seq(-4*pi,4*pi,len=27)
èîïðåäåëèìíàíèõôóíêöèþ.
r-sqrt(outer(x^2,y^2,"+"))
Ðàçîáü¼ìãðàôè÷åñêîåîêíîíà4÷àñòèèçàäàäèìðàçìåðûîòñòóïîâîòãðà-
ôèêîâ.
opar-par(mfrow=c(2,2),mar=rep(0,4))
Òåïåðüäëÿðàçëè÷íûõçíà÷åíèéïàðàìåòðà
f
=
i;
=
0
;
3
ïîñòðîèìãðà-
ôèêôóíêöèè
z
=cos(
r
2
)

e

r=f
,ãäå
r
=
p
x
2
+
y
2
.
for(finpi^(0:3))
contour(cos(r^2)*exp(-r/f),
drawlabels=FALSE,axes=FALSE,frame=TRUE)
142
Ðèñ.7.6:Óçîðïåðñèäñêîãîêîâðà
Ïðèìåð48.
Ïîñòðîèìêîíòóðíûéãðàôèêïðèðàçëè÷íûõçíà÷åíèÿõàðãó-
ìåíòà
j
x
j
.Ïîñòðîèìñíà÷àëàçàêðàøåí-
íóþñåòêóïðèïîìîùèôóíêöèè
image(x,x,z)
,íàêîòîðîéóæåçàäàäèì
êîíòóðíûåëèíèè.
z-outer(x,sqrt(abs(x)),FUN="/")
image(x,x,z)
Òðåòèéãðàôèê:
Ðèñ.7.7:Êîíòóðíûåëèíèèïðèðàçëè÷íûõçíà÷åíèÿõàðãóìåíòà

expr
âûðàæåíèåêàêôóíêöèÿîòõèëèèìÿôóíêöèè,ãðàôèêêîòîðîé
íóæíîïîñòðîèòü.

from
è
to
÷èñëîâûåàðãóìåíòûãðàíèöûîòðåçêà,íàêîòîðîìñòðî-
èòñÿãðàôèê.Åñëèíåçàäàíûýòèïàðàìåòðû,òîãðàíèöûîïðåäåëÿþòñÿ
çíà÷åíèåìàðãóìåíòà
x
çàäàííîãîâûðàæåíèÿ.Åñëèæåè
x
íåîïðåäåë¼í,
òîãðàôèêñòðîèòñÿíàîòðåçêå
[0;1]
.

n
÷èñëîâîéàðãóìåíò÷èñëîòî÷åê,âêîòîðûõîïðåäåëÿåòñÿçíà÷åíèå
expr
.

add
ëîãè÷åñêèéàðãóìåíòíóæíîëèäîáàâëÿòüñòðîÿùèéñÿãðàôèê
êóæåñóùåñòâóþùåìó(
add=TRUE
)èëèíóæíîñîçäàòüíîâîåãðàôè-
÷åñêîåîêíî(
add=FALSE
).
Ïðèìåð49.
Ïîñòðîèìãðàôèêèíåñêîëüêèõôóíêöèé
f
1
(
x
)=
x
2

3
x
,
f
2
(
x
)=
x
2

2
,
f
3
(
x
)=cos(
x
)
è
f
4
(
x
)=sin

cos(
x
)

e

x=
2

ïðèïîìîùè
curve()
,
ðåçóëüòàòïðåäñòàâëåííàðèñ.7.8.
par(mfrow=c(2,2))
curve(x^3-3*x,-2,2)
Ðèñ.7.8:Èñïîëüçîâàíèåôóíêöèè
curve()
[9]"beige""bisque""bisque1""bisque2"
[13]"bisque3""bisque4""black""blanchedalmond"
[17]"blue""blue1""blue2""blue3"
[21]"blue4"
Èëèïðîùå
�colors()[1:5]
[1]"white""aliceblue""antiquewhite""antiquewhite1"
[5]"antiquewhite2"
�colors()
[1]"white""aliceblue""antiquewhite"
[4]"antiquewhite1""antiquewhite2""antiquewhite3"
[7]"antiquewhite4""aquamarine""aquamarine1"
[10]"aquamarine2""aquamarine3""aquamarine4"
[13]"azure""azure1""azure2"
[16]"azure3""azure4""beige"
[19]"bisque""bisque1""bisque2"
[22]"bisque3""bisque4""black"
[25]"blanchedalmond""blue""blue1"
146
[28]"blue2""blue3""blue4"
[175]"gray22""gray23""gray24"
[178]"gray25""gray26""gray27"
[181]"gray28""gray29""gray30"
[184]"gray31""gray32""gray33"
[187]"gray34""gray35""gray36"
[190]"gray37""gray38""gray39"
[193]"gray40""gray41""gray42"
[196]"gray43""gray44""gray45"
[199]"gray46""gray47""gray48"
[202]"gray49""gray50""gray51"
[205]"gray52""gray53""gray54"
[208]"gray55""gray56""gray57"
[211]"gray58""gray59""gray60"
[214]"gray61""gray62""gray63"
[217]"gray64""gray65""gray66"
[220]"gray67""gray68""gray69"
[223]"gray70""gray71""gray72"
[226]"gray73""gray74""gray75"
[229]"gray76""gray77""gray78"
[232]"gray79""gray80""gray81"
[235]"gray82""gray83""gray84"
[238]"gray85""gray86""gray87"
[241]"gray88""gray89""gray90"
[244]"gray91""gray92""gray93"
[247]"gray94""gray95""gray96"
[250]"gray97""gray98""gray99"
[253]"gray100""green""green1"
[256]"green2""green3""green4"
[259]"greenyellow""grey""grey0"
[262]"grey1""grey2""grey3"
[265]"grey4""grey5""grey6"
[268]"grey7""grey8""grey9"
[271]"grey10""grey11""grey12"
[274]"grey13""grey14""grey15"
[277]"grey16""grey17""grey18"
[280]"grey19""grey20""grey21"
[283]"grey22""grey23""grey24"
[286]"grey25""grey26""grey27"
[289]"grey28""grey29""grey30"
[292]"grey31""grey32""grey33"
[295]"grey34""grey35""grey36"
[298]"grey37""grey38""grey39"
[301]"grey40""grey41""grey42"
[304]"grey43""grey44""grey45"
[307]"grey46""grey47""grey48"
[310]"grey49""grey50""grey51"
[313]"grey52""grey53""grey54"
[316]"grey55""grey56""grey57"
[319]"grey58""grey59""grey60"
148
[322]"grey61""grey62""grey63"
[325]"grey64""grey65""grey66"
[328]"grey67""grey68""grey69"
[331]"grey70""grey71""grey72"
[334]"grey73""grey74""grey75"
[337]"grey76""grey77""grey78"
[340]"grey79""grey80""grey81"
[343]"grey82""grey83""grey84"
[346]"grey85""grey86""grey87"
[349]"grey88""grey89""grey90"
[352]"grey91""grey92""grey93"
[355]"grey94""grey95""grey96"
[358]"grey97""grey98""grey99"
[361]"grey100""honeydew""honeydew1"
[364]"honeydew2""honeydew3""honeydew4"
[367]"hotpink""hotpink1""hotpink2"
[370]"hotpink3""hotpink4""indianred"
[373]"indianred1""indianred2""indianred3"
[376]"indianred4""ivory""ivory1"
[379]"ivory2""ivory3""ivory4"
[382]"khaki""khaki1""khaki2"
[385]"khaki3""khaki4""lavender"
[388]"lavenderblush""lavenderblush1""lavenderblush2"
[391]"lavenderblush3""lavenderblush4""lawngreen"
[394]"lemonchiffon""lemonchiffon1""lemonchiffon2"
[397]"lemonchiffon3""lemonchiffon4""lightblue"
[400]"lightblue1""lightblue2""lightblue3"
[403]"lightblue4""lightcoral""lightcyan"
[406]"lightcyan1""lightcyan2""lightcyan3"
[409]"lightcyan4""lightgoldenrod""lightgoldenrod1"
[412]"lightgoldenrod2""lightgoldenrod3""lightgoldenrod4"
[415]"lightgoldenrodyellow""lightgray""lightgreen"
[418]"lightgrey""lightpink""lightpink1"
[421]"lightpink2""lightpink3""lightpink4"
[424]"lightsalmon""lightsalmon1""lightsalmon2"
[427]"lightsalmon3""lightsalmon4""lightseagreen"
[430]"lightskyblue""lightskyblue1""lightskyblue2"
[433]"lightskyblue3""lightskyblue4""lightslateblue"
[436]"lightslategray""lightslategrey""lightsteelblue"
[439]"lightsteelblue1""lightsteelblue2""lightsteelblue3"
[442]"lightsteelblue4""lightyellow""lightyellow1"
[445]"lightyellow2""lightyellow3""lightyellow4"
[448]"limegreen""linen""magenta"
[451]"magenta1""magenta2""magenta3"
[454]"magenta4""maroon""maroon1"
[457]"maroon2""maroon3""maroon4"
[460]"mediumaquamarine""mediumblue""mediumorchid"
[463]"mediumorchid1""mediumorchid2""mediumorchid3"
[466]"mediumorchid4""mediumpurple""mediumpurple1"
149
[469]"mediumpurple2""mediumpurple3""mediumpurple4"
[472]"mediumseagreen""mediumslateblue""mediumspringgreen"
[616]"steelblue1""steelblue2""steelblue3"
[619]"steelblue4""tan""tan1"
[622]"tan2""tan3""tan4"
[625]"thistle""thistle1""thistle2"
[628]"thistle3""thistle4""tomato"
[631]"tomato1""tomato2""tomato3"
[634]"tomato4""turquoise""turquoise1"
[637]"turquoise2""turquoise3""turquoise4"
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
Ðèñ.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
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
ñïèñîêèçäâóõ
êîìïîíåíò(ïåðâàÿêîìïîíåíòàêîîðäèíàòûïîîñè

,âòîðàÿêîîðäèíàòû
ïîîñè

)èëèìàòðèöàèçäâóõñòîëáöîâ(ïåðâûéñòîëáåöêîîðäèíàòûïî
îñè

,âòîðîéêîîðäèíàòûïîîñè

).
155
Åñëèàðãóìåíò
x
÷èñëîâîéâåêòîðêîîðäèíàòûïîîñè

,òîäîëæåí
áûòüçàäàíàðãóìåíò
y
êîîðäèíàòûïîîñè

.
Ñðåäèçíà÷åíèéàðãóìåíòîâ
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
Ðèñ.7.11:Ñîåäèíåíèåòî÷åêëèíèÿìè
Îòìåòèìðÿääîïîëíèòåëüíûõïàðàìåòðîâ:

pch
÷èñëîâîéàðãóìåíòòèïðèñóåìîãîñèìâîëàâòî÷êåñçàäàííû-
ìèêîîðäèíàòàìè.Âîçìîæíûåçíà÷åíèÿîò0äî255,èçíèõ:
0


,
1


,
2

4
,
3

+
,
4


,
5


,
6

r
,
7


,
8


,
9
ðîìáñ
êðåñòîìâíóòðè,
10


,
11
äâàïåðåñåêàþùèõñÿòðåóãîëüíèêà,
12


,
13


,
14
êâàäðàòñîâïèñàííûìòðåóãîëüíèêîì;ñèìâîëû
15

25
çàêðàøèâàåìûåãåîìåòðè÷åñêèåôèãóðû;
26

31
íåèñïîëüçóþòñÿè
èãíîðèðóþòñÿïðèçàäàíèè;
32

127
ASCIIñèìâîëû;
128

255
ðàç-
ëè÷íûåñèìâîëû.×àñòüèçíèõ(
0

127
)ïðåäñòàâëåíûíàðèñ.7.12.

cex
÷èñëîâîéàðãóìåíòâîñêîëüêîðàçóâåëè÷èòüðàçìåðâûâîäè-
ìîãîñèìâîëà.

bg
ñèìâîëüíûéèëè÷èñëîâîéàðãóìåíòöâåòôîíà,êîòîðûììîæíî
çàêðàñèòüñèìâîëû
15

25
.

col
ñèìâîëüíûéèëè÷èñëîâîéàðãóìåíòöâåòñèìâîëà.
Ïðèìåð52.
Âûâåäåìâñåñèìâîëûäëÿôóíêöèè
points()
.
157
x=0:20
y=0:20
plot(x,y,type='n')
for(iin0:14)
points(x[i+1],y[20],pch=i,cex=1.5)
for(iin15:25)
points(x[i+1-15],y[18],pch=i,cex=1.5,col=i,bg=i)
for(iin32:51)
points(x[i+1-32],y[16],pch=i,cex=1.5)
for(iin52:71)
points(x[i+1-52],y[14],pch=i,cex=1.5)
for(iin72:91)
points(x[i+1-72],y[12],pch=i,cex=1.5)
for(iin92:111)
points(x[i+1-92],y[10],pch=i,cex=1.5)
for(iin112:127)
points(x[i+1-112],y[8],pch=i,cex=1.5)
Ðèñ.7.12:Ïðèìåðûðàçëè÷íûõñèìâîëîâ,âûâîäèìûõôóíêöèåé
points()
158
Ôóíêöèÿarrows()
Ôóíêöèÿ
arrows(x0,y0,x1=x0,y1=y0,length=0.25,angle=30,code=2,
col=par("fg"),lty=par("lty"),lwd=par("lwd"),...)
ïîçâîëÿåòðèñîâàòüñòðåëêèíàóæåñîçäàííîìãðàôèêå.
żàðãóìåíòû:

x0
è
y0
êîîðäèíàòûòî÷êè,îòêóäàñòðåëàâûõîäèò(íà÷àëàñòðåëû).
Ìîãóòáûòüâåêòîðàìè.

x1
è
y1
êîîðäèíàòûòî÷êè,êóäàñòðåëêàâõîäèò(êîíåöñòðåëû).Ìîãóò
áûòüâåêòîðàìè.

length
äëèíàñòðåëêèâäþéìàõ.

angle
óãîëìåæäóäðåâêîìñòðåëûèíàêîíå÷íèêîì(âãðàäóñàõ).

code
ïàðàìåòð,îïðåäåëÿþùèéòèïñòðåëû:
1
îñòðèåñòðåëûâòî÷êå
(
x
1
;y
1
)
,
2
îñòðèåñòðåëûâòî÷êå
(
x
0
;y
0
)
,
3
îñòðèåñòðåëûâòî÷êàõ
(
x
1
;y
1
)
è
(
x
0
;y
0
)
.

col
,
lty
è
lwd
ïàðàìåòðû,îïðåäåëÿþùèåöâåòñòðåëû,òèïå¼äðåâêà
èòîëùèíóñòðåëû.
Êàêðèñîâàòüñòðåëûïîêàçàíîâïðèìåðå53.
Ôóíêöèÿsegments()
Ôóíêöèÿ
segments(x0,y0,x1=x0,y1=y0,
col=par("fg"),lty=par("lty"),lwd=par("lwd"),...)
ñîåäèíÿåòïðÿìûìèîòðåçêàìèçàäàííûåòî÷êè.Àðãóìåíòû:

x0
è
y0
íà÷àëüíûåêîîðäèíàòûîòêóäàîòðåçîêâûõîäèò.Ìîãóò
áûòüâåêòîðàìè.

x1
è
y1
êîíå÷íûåêîîðäèíàòûêóäàâõîäèòâõîäèò.Ìîãóòáûòü
âåêòîðàìè.

col
,
lty
è
lwd
ïàðàìåòðû,îïðåäåëÿþùèåöâåòñòðåëû,òèïëèíèèè
òîëùèíó.
159
Ïðèìåð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)#oneshorterthandata
arrows(x[s],y[s],x[s+1],y[s+1],col=1:3,angle=10)
èñåãìåíòû
plot(x,y)
##drawarrowsfrompointtopoint:
s-seq(length(x)-1)#oneshorterthandata
segments(x[s],y[s],x[s+1],y[s+1],col=1:3)
Ðèñ.7.13:Ïîñòðîåíèåñòðåëèñåãìåíòîâ
Ôóíêöèÿpolygon()
Ôóíêöèÿ
160
polygon(x,y=NULL,density=NULL,angle=45,
border=NULL,col=NA,lty=par("lty"),
...,fillOddEven=FALSE)
ñòðîèòìíîãîóãîëüíèêñçàäàííûìèâåðøèíàìèèçàäàííûìèïàðàìåòðàìè.
Àðãóìåíòûôóíêöèè:

x
è
y
êîîðäèíàòûâåðøèíìíîãîóãîëüíèêà.Åñëè
x
ñïèñîêèçäâóõ
êîìïîíåíòèëèìàòðèöàèçäâóõñòîëáöîâ,òîäîñòàòî÷íîòîëüêîýòîãî
àðãóìåíòà.Åñëèñðåäèêîîðäèíàòåñòüõîòÿáûîäíîçíà÷åíèå
NA
,òî
ìíîãîóãîëüíèêðàçáèâàåòñÿíàíåñêîëüêî.

density
÷èñëîâîéàðãóìåíòïëîòíîñòüøòðèõîâêèìíîãîóãîëüíèêà,
÷èñëîëèíèéíàîäèíäþéì.Ïîóìîë÷àíèþ
density=NULL
îòñóò-
ñòâèåøòðèõîâêè.Çíà÷åíèå
density=0
íåáóäåòíèøòðèõîâêè,íè
çàêðàñêèìíîãîóãîëüíèêà.
density=NA
èëèîòðèöàòåëüíîå÷èñëî
øòðèõîâêèíåáóäåò,íîìîæíîçàêðàøèâàòü.

angle
óãîëíàêëîíà(âãðàäóñàõ)ëèíèèøòðèõîâêè.

col
öâåòâíóòðåííåé÷àñòèìíîãîóãîëüíèêà.Åñëè
col=NA
,òîìíî-
ãîóãîëüíèêíåçàêðàøèâàåòñÿ,åñëèíåîïðåäåëåíàøòðèõîâêà.Åñëè
density
ïîëîæèòåëüíîå÷èñëî,òîàðãóìåíò
col
çàäà¼òöâåòøòðèõîâ-
êè.Àðãóìåíò
col
ìîæåòáûòüâåêòîðîìâñëó÷àåïîñòðîåíèÿíåñêîëüêèõ
ìíîãîóãîëüíèêîâ.

border
öâåòãðàíèöûìíîãîóãîëüíèêà.Ïîóìîë÷àíèþ
border=NULL
öâåòãðàíèöûñîâïàäàåòñöâåòîìôîíàãðàôè÷åñêîãî
îêíà.Çíà÷åíèå
border=NA
îçíà÷àåòîòñóòñòâèåãðàíèö.Çíà÷åíèå
border=FALSE
ýêâèâàëåíòíî
border=NA

border=TRUE

border=NULL
.Àðãóìåíò
border
ìîæåòáûòüâåêòîðîìâñëó÷àå
ïîñòðîåíèÿíåñêîëüêèõìíîãîóãîëüíèêîâ.

lty
òèïèñïîëüçóåìîéëèíèè.

llOddEven
àðãóìåíò,îòâå÷àþùèéçàîêðàñèøòðèõîâêóïåðåñåêà-
þùèõñÿ÷àñòåéìíîãîóãîëüíèêîâ.
Íåñêîëüêîâàðèàíòîâïîñòðîåíèÿìíîãîóãîëüíèêîâðàññìîòðåíîâïðèìåðå
54.
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
plot(c(100,200),c(300,450),type="n",xlab="",ylab="")
rect(100,300,125,350)#transparent
rect(100,400,125,450,col="green",border="blue")
rect(115,375,150,425,col=par("bg"),border="transparent")
rect(150,300,175,350,density=10,border="red")
rect(150,400,175,450,density=30,col="blue",
angle=-30,border="transparent")
par(op)
Ðèñ.7.14:Ìíîãîóãîëüíèêè
Ôóíêöèÿsymbols()
Ïðèïîìîùèôóíêöèè
symbols(x,y=NULL,circles,squares,rectangles,stars,
ìîæíîðèñîâàòüíàãðàôèêåêðóãè,êâàäðàòû,ïðÿìîóãîëüíèêè,çâ¼çäû,òåð-
ìîìåòðûè
boxplot
.
Àðãóìåíòû:

x,y
êîîðäèíàòûñîçäàâàåìûõôèãóð.

circles
âåêòîððàäèóñîâñîçäàâàåìûõîêðóæíîñòåé.

squares
âåêòîðäëèíñòîðîíêâàäðàòîâ.

rectangles
ìàòðèöàèçäâóõñòîëáöîâ:ïåðâûéñòîëáåöäëèíûîñíî-
âàíèéïðÿìîóãîëüíèêîâ,âòîðîéâûñîòûïðÿìîóãîëüíèêîâ.

stars
ìàòðèöàèçòð¼õèáîëååñòîëáöîâ-äëèíëó÷åéñîçäàâàåìûõ
çâ¼çä.


xlab,ylab,main,xlim,ylim
àðãóìåíòû,çàäàâàåìûåâñëó÷àåïî-
ñòðîåíèÿíîâîãîãðàôèêà(
add=FALSE
).
Ïðèìåð55.
Èçîáðàçèìíàãðàôèêåðàçëè÷íûåñèìâîëûïðèïîìîùèôóíêöèè
symbols()
.
x=1:100
y=1:100
plot(x,y,type='n')
x1=seq(10,90,by=10);x1
y1=rep(90,9);y1
A=seq(1,5,length=9);A
symbols(x1,y1,circles=A,bg=1:9,add=T,inches=FALSE)
B=seq(5,1,length=9)
symbols(x1,y1-10,squares=B,bg=1:9,add=T,inches=FALSE)
C=cbind(seq(1,5,length=9),seq(5,1,length=9))
symbols(x1,y1-20,rectangles=C,bg=1:9,add=T,inches=FALSE)
D=matrix(3,nrow=9,ncol=3)
symbols(x1,y1-30,stars=D,bg=1:9,add=T,inches=FALSE)
E=matrix(3,nrow=9,ncol=4)
symbols(x1,y1-40,stars=E,fg=1:9,add=T,inches=FALSE)
FF=matrix(3,nrow=9,ncol=5)
symbols(x1,y1-50,stars=FF,bg=1:9,add=T,inches=FALSE)
G=cbind(rep(3,9),rep(6,9),seq(0.1,0.9,by=0.1));G
Ðèñ.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

line
÷èñëîëèíèé,íàêîòîðîåïîñòðîåííàÿîñüìîæåòâûõîäèòüçàîá-
ëàñòüïîñòðîåíèÿãðàôèêà.Çíà÷åíèå
line=NA
,îçíà÷àåò,÷òîîñüñòðî-
èòñÿòîëüêîâîáëàñòèãðàôèêà.

pos
êîîðäèíàòàãðàôè÷åñêîãîîêíà,äîêîòîðîéáóäåòñòðîèòüñÿîñü.
Åñëèçíà÷åíèåàðãóìåíòàîòëè÷íîîò
NA
,òîîòêëþ÷àåòàðãóìåíò
line
.
Çíà÷åíèå
line=NA
,îçíà÷àåò,÷òîîñüñòðîèòñÿòîëüêîâîáëàñòèãðà-
ôèêà.

outer
ëîãè÷åñêèéàðãóìåíòäîëæíàëèîñüâûõîäèòüçàñòàíäàðòíóþ
îáëàñòüãðàôèêà.

font
ñåìåéñòâîøðèôòîâ,èñïîëüçóåìîåäëÿòåêñòà.Âîçìîæíûåçíà-
÷åíèÿ:
1
ïðîñòîé(ïîóìîë÷àíèþ),
2
æèðíûé;
3
êóðñèâè
4

æèðíûéêóðñèâ.

lty
òèïëèíèèäëÿîñèèäåëåíèéíàîñè.Âîçìîæíûåçíà÷åíèÿ:
solid
(
1
)ñïëîøíàÿëèíèÿ;
blank
(
0
)îòñóòñòâóåòëèíèÿ;
dashed
(
2
)
øòðèõîâàÿëèíèÿ;
dotdash
(
4
)øòðèõ-ïóíêòèð;
dotted
(
3
)
ïóíêòèðíàÿëèíèÿ;
longdash
(
5
)äëèííûéøòðèõ;
twodash
(
6
)
äâîéíîéøòðèõ(ñëåäóåòîòìåòèòü,÷òîäîëæåíáûòüçàäàíïàðàìåòð
type=l
).

lwd
òîëùèíàîñè.Îòðèöàòåëüíîåèëèíóëåâîåçíà÷åíèåïîäàâëÿåòïî-
ñòðîåíèåîñè.

lwd.ticks
òîëùèíàëèíèéäëÿäåëåíèéíàîñè.Îòðèöàòåëüíîåèëè
íóëåâîåçíà÷åíèåïîäàâëÿåòïîñòðîåíèåäåëåíèé.

col
öâåòîñè.

col.ticks
öâåòìåòîêíàîñè.

hadj
÷èñëîèëè÷èñëîâîéâåêòîððàñïîëîæåíèåïîäïèñåéêìåòêàì
íàîñÿõ.Âñåïîäïèñèïàðàëëåëüíû(ãîðèçîíòàëüíû)íàïðàâëåíèþ÷òå-
íèÿ.Âîçìîæíûåçíà÷åíèÿ:
0
ïîäïèñèñòàâÿòñÿñëåâàîòìåòêè,
0.5

ïîöåíòðóìåòêè,
1
ñïðàâàîòìåòêè.

padj
÷èñëîèëè÷èñëîâîéâåêòîððàñïîëîæåíèåïîäïèñåéêìåòêàì
íàîñÿõ.Âñåïîäïèñèðàñïîëàãàþòñÿïåðïåíäèêóëÿðíîíàïðàâëåíèþ÷òå-
íèÿ.Âîçìîæíûåçíà÷åíèÿ:
0
íàçâàíèåñïðàâàèëèâûøåìåòêè,
1

íàçâàíèåñëåâàèëèíèæåìåòêè.
Ðàçëè÷íûåâàðèàíòûçàäàíèÿîñåéïðèâåäåíûâïðèìåðå56èðèñ.7.16.
167
Ôóíêöèÿgrid()
Ôóíêöèÿ
grid(nx=NULL,ny=nx,col="lightgray",lty="dotted",
lwd=par("lwd"),equilogs=TRUE)
çàäà¼òïðÿìîóãîëüíóþñåòêóíàãðàôèêå.Àðãóìåíòûôóíêöèè:

nx
è
ny
êîëè÷åñòâîÿ÷ååêñåòêèïîîñÿì

è
0y
,ñîîòâåòñòâåííî.Åñëè
nx=NULL
,òîñåòêàñòðîèòñÿíàîñíîâåäåëåíèéîñè.Åñëè
nx=NA
èëè
ny=NA
,òîñåòêàâñîîòâåòñòâóþùåìíàïðàâëåíèèíåñòðîèòñÿ.

col
öâåòëèíèéñåòêè.

lty
òèïëèíèèñåòêè.

lwd
òîëùèíàëèíèéñåòêè.

equilogs
ëîãè÷åñêèéàðãóìåíò.Èñïîëüçóåòñÿ,åñëèîñèëîãàðèôìè÷å-
ñêèåèñåòêàñòðîèòñÿâïðèâÿçêåêîñÿì.
Ñòîèòçàìåòèòü,÷òîïîäîáíóþñåòêóìîæíîïîñòðîèòüèïðèïîìîùèôóíê-
öèè
abline()
(ñì.ðàçäåë7.2.1äàííîéãëàâû).
Âàðèàíòûðàçëè÷íîãîçàäàíèÿñåòêèïðèâåäåíûâïðèìåðå56èïðåäñòàâ-
ëåíûíàðèñ.7.16.
Ôóíêöèÿbox()
Èïîñëåäíÿÿôóíêöèÿâäàííîìðàçäåëåôóíêöèÿ
box(which="plot",lty="solid",...)
ñòðîÿùàÿïðÿìîóãîëüíèê(ðàìêó)âîêðóããðàôèêà(çàäàííîéîáëàñòè)íóæíî-
ãîöâåòàèòèïàëèíèè.Àðãóìåíòû:

which
ñèìâîëüíûéàðãóìåíò.Âîçìîæíûåçíà÷åíèÿ:
'plot'
ðàìêà
âîêðóããðàôèêà,
'gure'
ðàìêàâîêðóãâñåéãðàôè÷åñêîéîáëàñòè(îê-
íàèëèïîäîêíà),
'inner'
è
'outer'
ðàìêàïîêðàþâñåãîãðàôè÷åñêîãî
îêíà.

lty
òèïëèíèè.

...
äîïîëíèòåëüíûåïàðàìåòðûöâåòðàìêè,å¼òîëùèíà.
Ïðèìåð56.
Ïðèâåä¼ìíåñêîëüêîïðèìåðîâïîñòðîåíèÿîñåé,çàäàíèÿñåòêè
èðàìêè.Ïðåäñòàâèìãðàôè÷åñêîåîêíîââèäåíåñêîëüêèõ
168
op=par(mfrow=c(3,2))
Ïåðâûéâàðèàíòçàäàíèÿîñåé
plot(1:4,rnorm(4),axes=FALSE)
Ðèñ.7.16:Ôóíêöèè
axis()
,
grid()
è
box()
7.2.3Òåêñòâãðàôè÷åñêîìîêíåôóíêöèè
expression(),text(),legend(),mtext()è
title()
Ýòà÷àñòüãëàâûïîñâÿùåíàâûâîäóäîïîëíèòåëüíîéòåêñòîâîéèíôîðìà-
öèèêïîñòðîåííîìóãðàôèêóçàãîëîâêè,ëåãåíäûèòåêñò.
Ôóíêöèÿexpression()
Ñíà÷àëàðàçáåð¼ìôóíêöèþ
expression()
1
,ïîçâîëÿþùóþñîçäàâàòüòåêñò
ñèñïîëüçîâàíèåììàòåìàòè÷åñêèõñèìâîëîâ,ãðå÷åñêèõáóêâ.
Îáðàùåíèåêôóíêöèè:
expression(âûðàæåíèå)
Ñïîìîùüþ
1
Ôóíêöèÿ
expression()
ñîçäà¼òîñîáóþñòðóêòóðóâ
R
¾âûðàæåíèå¿,èñïîëüçóåìóþ
ïðèñèìâîëüíîìäèôôåðåíöèðîâàíèèâ÷àñòíîñòè
170
demo(plotmath)
ìîæíîîçíàêîìèòüñÿñçàïèñüþðàçëè÷íûõìàòåìàòè÷åñêèõñèìâîëîâ.Òàêæå
îíèïðèâåäåíûíàðèñ.7.177.21
Ðèñ.7.17:Ðàçëè÷íûåìàòåìàòè÷åñêèåîïåðàòîðûèñèìâîëû
171
Ðèñ.7.18:Ðàçëè÷íûåìàòåìàòè÷åñêèåîïåðàòîðûèñèìâîëû
Ðèñ.7.19:Ðàçëè÷íûåìàòåìàòè÷åñêèåîïåðàòîðûèñèìâîëû
Ôóíêöèÿtext()
Ôóíêöèÿ
172
Ðèñ.7.20:Ðàçëè÷íûåìàòåìàòè÷åñêèåîïåðàòîðûèñèìâîëû
Ðèñ.7.21:Ðàçëè÷íûåìàòåìàòè÷åñêèåîïåðàòîðûèñèìâîëû
text(x,y=NULL,labels=seq_along(x),adj=NULL,
173
mtext(text,side=3,line=0,outer=FALSE,at=NA,
adj=NA,padj=NA,cex=NA,col=NA,font=NA,...)
îòâå÷àåòçàâûâîäòåêñòàñíèçóèëèñâåðõó,èëèñëåâà,èëèñïðàâàîòãðàôèêà.
Ðàññìîòðèìå¼àðãóìåíòû(÷àñòüèçíèõóæåçíàêîìû):

text
ñèìâîëüíûéâåêòîðèëèâåêòîð,ýëåìåíòàìèêîòîðîãîÿâëÿþòñÿ
expression
âûâîäèìûéòåêñò.

side
ñòîðîíàãðàôèêà,îòíîñèòåëüíîêîòîðîéâûâîäèòñÿòåêñò.Çíà÷å-
íèÿ:
1
ñíèçó,
2
ñëåâà,
3
ñâåðõó,
4
ñïðàâà.

line
÷èñëîâîéïàðàìåòðêîëè÷åñòâîëèíèé,íàêîòîðîåíàäîîòñòó-
ïèòüîòãðàíèöûîáëàñòèðèñóíêà(îòðèöàòåëüíîåçíà÷åíèåïåðåõîä
âíóòðüîáëàñòèðèñóíêà).

outer
ëîãè÷åñêèéàðãóìåíòìîæåòëèòåêñòâûõîäèòüçàîáëàñòü
ãðàôè÷åñêîãîîêíà.

at
ðàñïîëîæåíèåòåêñòàâïîëüçîâàòåëüñêèõêîîðäèíàòàõ(îò0äî1).

adj
ïîëîæåíèåñèìâîëüíîéñòðîêè,ïàðàëëåëüíîéíàïðàâëåíèÿ÷òåíèÿ.
adj=0
òåêñòíàõîäèòñÿñíèçóèëèñëåâà,
adj=1
ñïðàâàèëèñâåðõó.

padj
ïîëîæåíèåñèìâîëüíîéñòðîêè,ïåðïåíäèêóëÿðíîéíàïðàâëåíèÿ
÷òåíèÿ.
padj=0
òåêñòíàõîäèòñÿñïðàâàèëèñâåðõó,
padj=1
ñëåâà
èëèñíèçó.

cex
ðàçìåðâûâîäèìîãîòåêñòà(ìîæåòáûòüâåêòîðîì).

col
öâåòâûâîäèìîãîòåêñòà(ìîæåòáûòüâåêòîðîì).

font
òèïøðèôòà(ìîæåòáûòüâåêòîðîì).
Ôóíêöèÿlegend()
Ôóíêöèÿ
legend(x,y=NULL,legend,fill=NULL,col=par("col"),
border="black",lty,lwd,pch,
angle=45,density=NULL,bty="o",bg=par("bg"),
box.lwd=par("lwd"),box.lty=par("lty"),box.col=par("fg"),
pt.bg=NA,cex=1,pt.cex=cex,pt.lwd=lwd,
xjust=0,yjust=1,x.intersp=1,y.intersp=1,
adj=c(0,0.5),text.width=NULL,text.col=par("col"),
175
merge=do.lines&&has.pch,trace=FALSE,
plot=TRUE,ncol=1,horiz=FALSE,title=NULL,
ñèìâîëüíûéàðãóìåíòðàìêàâîêðóãîáëàñòèñëåãåíäîé.Äâà
çíà÷åíèÿ:
'o'
ðàìêàñòðîèòñÿ(ïîóìîë÷àíèþ)è
'n'
ðàìêàíåñòðî-
èòñÿ.
176

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
ñèìâîëüíûéàðãóìåíòçàãîëîâîêëåãåíäû.

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("10ofthem")
for(sin1: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")
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),#only1stistaken
xlab=expression(paste("PhaseAngle",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
Ðèñ.7.22:Âûâîäòåêñòàíàãðàôèêå
181
Ãëàâà8
Ðåøåíèåíåëèíåéíûõóðàâíåíèéè
ñèñòåìíåëèíåéíûõóðàâíåíèé.
Èíòåãðèðîâàíèåè
äèôôåðåíöèðîâàíèå.Ýêñòðåìóìû
ôóíêöèé
Âýòîéãëàâåáóäóòðàññìîòðåíûôóíêöèè,ïîçâîëÿþùèåðåøàòüíåëèíåé-
íûåóðàâíåíèÿèñèñòåìûóðàâíåíèÿ,âû÷èñëÿòüèíòåãðàëûèíàõîäèòüïðîèç-
âîäíûå(âòîì÷èñëåè÷àñòíûå)ôóíêöèé,çíà÷åíèÿïðîèçâîäíûõâçàäàííûõ
òî÷êàõ,àòàêæåýêñòðåìóìûôóíêöèé.
8.1Ðåøåíèåíåëèíåéíûõóðàâíåíèéè
ñèñòåìíåëèíåéíûõóðàâíåíèé
Çäåñüáóäóòðàññìîòðåíûòðèôóíêöèè:ôóíêöèÿ
uniroot
áàçîâîãîïàêåòà
base
èôóíêöèè
uniroot.all
è
multiroot
ïàêåòà
rootSolve
.
8.1.1Ôóíêöèÿuniroot
uniroot(f,interval,...,
lower=min(interval),upper=max(interval),
f.lower=f(lower,...),f.upper=f(upper,...),
tol=.Machine$double.eps^0.25,maxiter=1000)
Àðãóìåíòû:
182

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.
Íàéä¼ìðåøåíèåóðàâíåíèÿ
5
x
2

10
x
=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.all
1
óñòðàíÿåòíåäîñòàòêè
uniroot
,ïîçâîëÿÿâû÷èñëÿòü
íåñêîëüêîêîðíåéíàçàäàííîìèíòåðâàëå.
uniroot.all(f,interval,lower=min(interval),upper=max(interval),
tol=.Machine$double.eps^0.2,maxiter=1000,n=100,...)
Îòëè÷èåçàêëþ÷àåòñÿâââåäåíèèàðãóìåíòà
n
÷èñëîïîäèíòåðâàëîâ,íàêî-
òîðûåäåëèòñÿèñõîäíûéèíòåðâàë,èíàêàæäîìèçýòèõïîäèíòåðâàëîâèùåòñÿ
íóëüôóíêöèè.Ðåçóëüòàòâûçîâàôóíêöèèâåêòîðñíàéäåííûìèðåøåíèÿìè.
Ïðèìåð59.
Âåðí¼ìñÿêïðåäûäóùåìóïðèìåðóèíàéä¼ìðåøåíèåóðàâíåíèÿ
5
x
2

10
x
=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.0000002.000000
Íàéäåíûîáàðåøåíèÿ.Îòìåòèì,÷òîåñëèçàäàòüñëèøêîìáîëüøîå÷èñëî
ïîäèíòåðâàëîâïîèñêà,òîâîçìîæíûïîãðåøíîñòèâðåøåíèè.
1
Ïàêåò
rootSolve
184
�uniroot.all(f,c(0,3))
[1]0.0000001.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

10

28
.

abs.tol
òðåáóåìàÿàáñîëþòíàÿòî÷íîñòü.
186

stop.on.error
ëîãè÷åñêèéàðãóìåíò.Ïðèâîçíèêíîâåíèèîøèáêèïðå-
êðàùàåòâû÷èñëåíèå,âïðîòèâíîìñëó÷àåâûäà¼òñÿðåçóëüòàòñïðåäó-
ïðåæäåíèÿìè.

ïîñëåäíèåäâààðãóìåíòà:
keep.xy
è
aux
,íåèñïîëüçóþòñÿ,ââåäåíû
äëÿñîâìåñòèìîñòèñÿçûêîì
S
.
Ðåçóëüòàòîìðàáîòûáóäåòñïèñîê.ñîñòîÿùèéèçñëåäóþùèõïîëåé:

value
ðåçóëüòàòèíòåãðèðîâàíèÿ.

abs.error
îöåíêàìîäóëÿàáñîëþòíîéîøèáêè.

subdivisions
÷èñëîïîäèíòåðâàëîâèíòåãðèðîâàíèÿ.

message
âûâîäñîîáùåíèÿ
¾OK¿
ëèáîñèìâîëüíàÿñòðîêàññîîáùå-
íèåìîáîøèáêå.
Ðàññìîòðèìíåñêîëüêîïðèìåðîââû÷èñëåíèÿèíòåãðàëîâ.
Ïðèìåð60.
Âû÷èñëèìâåðîÿòíîñòüïîïàäàíèÿíîðìàëüíîðàñïðåäåë¼ííîé
ñëó÷àéíîéâåëè÷èíûñïàðàìåòðàìè

=0
è

=1
íàèíòåðâàë
[0;
1
)
.
�integrate(dnorm,0,Inf)
0.5withabsoluteerror4.7e-05
Òåïåðüâêà÷åñòâåâåðõíåãîïðåäåëàðàññìîòðèìêîíå÷íûåçíà÷åíèÿ:2,20,
200,2.000è20.000.
�integrate(dnorm,0,2)
0.4772499withabsoluteerror5.3e-15
�integrate(dnorm,0,20)
0.5withabsoluteerror3.7e-05
�integrate(dnorm,0,200)
0.5withabsoluteerror1.6e-07
�integrate(dnorm,0,2000)
0.5withabsoluteerror4.4e-06
�integrate(dnorm,0,20000)
0withabsoluteerror0
Ïîñëåäíèéâàðèàíò(âåðõíèéïðåäåëèíòåãðèðîâàíèÿðàâåí20000)ïðèâîäèò
êîøèáêå.
Âêà÷åñòâåäðóãîãîïðèìåðàíàéä¼ìèíòåãðàëîòôóíêöèè
f
(
x
)=
1
(
x
+1)
p
x
.
Ïðåäåëèíòåãðèðîâàíèÿîò0äî
1
.Ñíà÷àëàçàäàäèìñàìóïîäèíòåãðàëü-
íóþôóíêöèþ.
187
integrand=function(x){1/((x+1)*sqrt(x))}
Òåïåðüâû÷èñëèèíòåãðàë
�integrate(integrand,lower=0,upper=Inf)
3.141593withabsoluteerror2.7e-05
Íàêîíåö,âêà÷åñòâåïîñëåäíåãîïðèìåðàðàññìîòðèìñëó÷àé,êîãäàóèí-
òåãðèðóåìîéôóíêöèèíåñêîëüêîïàðàìåòðîâ:
f
(
x
)=
1
a
(1+(
x

b
)
2
)
,ãäå
a
=2
;b
=
2
.
�integrand=function(x,a,b){1/(a*(1+(x-b)^2))}
�integrate(integrand,lower=0,upper=Inf,a=2,b=2)
1.338973withabsoluteerror5.5e-08
Êðîìåòîãî,âáàçîâîéóñòàíîâêå
R
ðåàëèçîâàíûòðèôóíêöèè,êîòîðûåâ
ñèìâîëüíîìâèäåâû÷èñëÿþòïðîèçâîäíûå(âòîì÷èñëåè÷àñòíûå)çàäàííûõ
âûðàæåíèé.
Âèäôóíêöèé:
D(expr,name)
deriv(expr,namevec,function.arg=NULL,tag=".expr",
hessian=FALSE,...)
deriv3(expr,namevec,function.arg=NULL,tag=".expr",
hessian=TRUE,...)
Ôóíêöèÿ
D()
ïîçâîëÿåòâû÷èñëÿòüïðîèçâîäíóþôóíêöèèïîîäíîìóàð-
ãóìåíòó.Ôóíêöèè
deriv()
è
deriv3()
ïîçâîëÿþòâû÷èñëèòü÷àñòíûåïðîèç-
âîäíûå.
Àðãóìåíòû:

expr
ëèáîâûðàæåíèå(
expression
)èëè(çàèñêëþ÷åíèåìôóíêöèè
D
)
ôîðìóëà(
formula
).

name
,
namevec
ñèìâîëüíûéâåêòîð,çàäàþùèéèìåíàïåðåìåííûõ
(òîëüêîîäíàïåðåìåííàÿäëÿ
D()
),ïîêîòîðûìáåðóòñÿïðîèçâîäíûå.

function.arg
åñëèäàííûéàðãóìåíòîïðåäåë¼íèíåðàâåí
NULL

ñèìâîëüíûéâåêòîðàðãóìåíòîâðåçóëüòèðóþùåéôóíêöèèèëèôóíê-
öèÿ(ñïóñòûìòåëîì),èëèëîãè÷åñêèéàðãóìåíò
TRUE
,îïðåäåëÿþ-
ùèéèñïîëüçîâàíèåôóíêöèèñàðãóìåíòàìè,èìåíàêîòîðûõîïðåäåëåíû
namevec
.

tag
ñèìâîëüíûéàðãóìåíòïðåôèêñ,èñïîëüçóåìûéäëÿîáîçíà÷åíèÿ
ñîçäàííûõëîêàëüíûõïåðåìåííûõïðèâûâîäåðåçóëüòàòà.
188

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
�deriv3(trig,c("x","y"),func=TRUE)
function(x,y)
{
.expr2-x+y^2
.expr3-cos(.expr2)
.expr4-sin(.expr3)
.expr5-cos(.expr3)
.expr6-sin(.expr2)
.expr14-2*y
.expr15-.expr6*.expr14
.expr16-.expr4*.expr15
.expr18-.expr3*.expr14
.value-.expr4
.grad-array(0,c(length(.value),2L),list(NULL,c("x",
"y")))
.hessian-array(0,c(length(.value),2L,2L),list(NULL,
c("x","y"),c("x","y")))
.grad[,"x"]--(.expr5*.expr6)
.hessian[,"x","x"]--(.expr4*.expr6*.expr6+.expr5*
.expr3)
.hessian[,"x","y"]-.hessian[,"y","x"]--(.expr16*
.expr6+.expr5*.expr18)
.grad[,"y"]--(.expr5*.expr15)
.hessian[,"y","y"]--(.expr16*.expr15+.expr5*(.expr18*
.expr14+.expr6*2))
attr(.value,"gradient")-.grad
attr(.value,"hessian")-.hessian
.value
}
Ïîëó÷åíû÷àñòíûåïðîèçâîäíûåèìàòðèöàâòîðûõïðîèçâîäíûõ-ìàòðèöàÃåññå(ãåññèàí).
Èñïîëüçóÿâñòðîåííûåôóíêöèè,ñîçäàäèìôóíêöèþäëÿâû÷èñëåíèÿïðî-
èçâîäíûõñòàðøåãîïîðÿäêà(ïîîäíîéïåðåìåííîé).
DD=function(expr,name,order=1){
if(order1)stop("'order'mustbe&#x-515;-51;sto;&#xp"'o;&#xrd1e;&#xr'-5;mu;&#xst-5;ᒾ&#x-514;=1")
if(order==1)D(expr,name)
elseDD(D(expr,name),name,order-1)
}
Âûçîâôóíêöèèèðåçóëüòàò:
191
�DD(expression(sin(cos(x+y^2))),'x',3)
-((sin(cos(x+y^2))*cos(x+y^2)-cos(cos(x+y^2))*sin(x+
y^2)*sin(x+y^2))*sin(x+y^2)+sin(cos(x+y^2))*
sin(x+y^2)*cos(x+y^2)+(sin(cos(x+y^2))*sin(x+
y^2)*cos(x+y^2)-cos(cos(x+y^2))*sin(x+y^2)))
8.2.2Íàõîæäåíèåýêñòðåìóìîâôóíêöèè.Ðåøå-
íèåçàäà÷îïòèìèçàöèè
Â
R
,êàêïðàâèëî,ðåøàåòñÿçàäà÷àïîèñêàìèíèìóìàôóíêöèèíàíåêîòî-
ðîìèíòåðâàëåñíåêîòîðûìèóñëîâèÿìèèëèáåç.
Ôóíêöèÿ
optim()
ÿâëÿåòñÿîñíîâíîéâðåøåíèèçàäà÷íàõîæäåíèÿýêñòðå-
ìóìîâ.
optim(par,fn,gr=NULL,...,
'L-BFGS-B'
ìîäèôèêàöèÿìåòîäà
'BFGS'
,èñïîëüçóþùàÿîãðà-
íè÷åíèÿíàïåðåìåííûå.
'SANN'
ñòîõàñòè÷åñêèéîïòèìèçàöèîííûéìåòîä.Èñïîëüçóåò
òîëüêîñàìóôóíêöèþ,íîìåäëåííîñõîäèòñÿ.Ïðèìåíèìäëÿíåäèô-
ôåðåíöèðóåìûõôóíêöèé.Ñèëüíîçàâèñèòîòêîíòðîëüíûõïàðà-
ìåòðîâ.

lower
è
upper
ãðàíèöûäëÿïåðåìåííûõ,èñïîëüçóþòñÿòîëüêîïðè
ìåòîäå
L-BFGS-B
.

control
ñïèñîê,âêîòîðûéìîãóòâõîäèòüñëåäóþùèåóïðàâëÿþùèå
ïàðàìåòðû:
trace
íåîòðèöàòåëüíîåöåëîå÷èñëî.Åñëèïîëîæèòåëüíîå,òîâû-
âîäèòñÿèíôîðìàöèÿîõîäåîïòèìèçàöèè.×åìáîëüøåçíà÷åíèå
ïðèíèìàåòêîíòðîëüíûéïàðàìåòð
trace
,òåìáîëååïîäðîáíàÿèí-
ôîðìàöèÿâûâîäèòñÿ.
fnscale
ìàñøòàáèðóþùèéïàðàìåòðäëÿîïòèìèçèðóåìîéôóíê-
öèè.Åñëèïðèíèìàåòîòðèöàòåëüíîåçíà÷åíèå,òîðåøàåòñÿçàäà-
÷àìàêñèìèçàöèè.Òàêèìîáðàçîì,îïòèìèçàöèÿïðîèñõîäèòäëÿ
fn(par)/fnscale
.
parscale
÷èñëîâîéâåêòîð,ìàñøòàáèðóþùèéïàðàìåòðûîïòèìè-
çàöèè.Òàêèìîáðàçîì,îïòèìèçàöèÿïðîèçâîäèòñÿïî
par/parscale
ïàðàìåòðàì.
ndeps
âåêòîð,îïðåäåëÿþùèéðàçìåðøàãàîïòèìèçàöèè,ïîóìîë-
÷àíèþ
10

3
.
maxit
ìàêñèìàëüíîå÷èñëîèòåðàöèé.Çíà÷åíèÿïîóìîë÷àíèþ:
100äëÿìåòîäîâ
'BFGS'
,
'CG'
è
'L-BFGS-B'
,500
'Nelder-
Mead'
,10000äëÿ
'SANN'
.
abstol
àáñîëþòíàÿòî÷íîñòüñõîäèìîñòè,ïðèìåíèìàòîëüêîäëÿ
íåîòðèöàòåëüíûõôóíêöèé.
reltol
îòíîñèòåëüíàÿòî÷íîñòüñõîäèìîñòè.Çíà÷åíèåïîóìîë÷à-
íèþ
10

8
.
alpha
,
type
êîíòðîëüíûéïàðàìåòðäëÿìåòîäàñîïðÿæ¼ííûõãðà-
äèåíòîâ.Âûáîððàçíîâèäíîñòèìåòîäà.Çíà÷åíèå1âàðèàíò

hessian
ìàòðèöàîöåíêàìàòðèöûâòîðûõïðîèçâîäíûõâíàéäåí-
íîéòî÷êå(âûâîäèòñÿòîëüêîåñëèàðãóìåíò
hessian
ôóíêöèè
optim()
ïðèíèìàåòçíà÷åíèå
TRUE
).
Ïðèìåð62.
Âêà÷åñòâåïðèìåðàðàññìîòðèìôóíêöèþÐîçåíáåðãà
f
(
x;y
)=
100(
y

x
2
)+(1

x
)
2
èíàéä¼ìååìèíèìóì.
Ñíà÷àëàçàäàäèìñàìóôóíêöèþ(
x
[1]=
x;x
[2]=
y
):
fr-function(x){
x1-x[1]
x2-x[2]
100*(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.0002601.000506
$value
[1]8.825241e-08
$counts
functiongradient
195NA
$convergence
[1]0
$message
NULL
$counts
functiongradient
11043
$convergence
[1]0
$message
NULL
[1]1.0186841.037829
$value
[1]0.0003491976
$counts
functiongradient
389101
$convergence
[1]1
$message
NULL
èíòåðâàëå.
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
)=
(
x

5)
2
sin
x
(
x
+2)
2
íà
èíòåðâàëàõ
[

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
[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
)=
(
x

5)
2
sin
x
(
x
+2)
2
íàèíòåðâà-
ëàõ
[

1;4]
,
[0;3]
è
[1
;
3]
.
�f=function(x,a,b){(x-a)^2*sin(x)/((x+b)^2)}
�nlm(f,0,a=5,b=2)
$minimum
[1]-1.635157e+15
$estimate
[1]-2
$gradient
[1]8.10962e+20
$code
[1]2
$iterations
[1]17
Ïîìåíÿåìíà÷àëüíîåçíà÷åíèå
x
�f=function(x,a,b){(x-a)^2*sin(x)/((x+b)^2)}
�nlm(f,4,a=5,b=2)
$minimum
201
[1]-0.02779574
$estimate
[1]3.642274
$gradient
[1]4.610341e-09
$code
[1]1
$iterations
[1]7
Êàêâèäíî,ìåíÿÿíà÷àëüíûåçíà÷åíèÿ,ìîæíîíàéòèðàçíûåòî÷êèìè-
íèìóìà.
Ôóíêöèÿ
nlminb()
èñïîëüçóåòñÿâðåøåíèÿçàäà÷îïòèìèçàöèè(ïîèñêìè-
íèìóìàôóíêöèè)ïðèíàëè÷èè(îòñóòñòâèè)îãðàíè÷åíèéíàïàðàìåòðûñ
ïîìîùüþïðîöåäóð
PORT
eval.max
ìàêñèìàëüíîåäîïóñòèìîå÷èñëîâûçîâîâöåëåâîé
ôóíêöèè.Çíà÷åíèåïîóìîë÷àíèþ200.
iter.max
ìàêñèìàëüíîäîïóñòèìîå÷èñëîèòåðàöèé.Ïîóìîë÷à-
íèþ150.
trace
âûâîäçíà÷åíèÿöåëåâîéôóíêöèèèïàðìåòðîâîïòèìèçà-
öèèíàêàæäîéèòåðàöèè.Çíà÷åíèå
0
îçíà÷àåò,÷òîèíôîðìàöèÿ
âûâîäèòüñÿíåáóäåò.
abs.tol
àáñîëþòíàÿòî÷íîñòü.Ïîóìîë÷àíèþ0.AÅñëèöåëåâàÿ
ôóíêöèÿíåîòðèöàòåëüíàÿ,òîëó÷øåèñïîëüçîâàòüçíà÷åíèå
1e-20
.
rel.tol
îòíîñèòåëüíàÿòî÷íîñòü.Ïîóìîë÷àíèþ
1e-10
.
x.tol
òî÷íîñòüîòíîñèòåëüíîàðãóìåíòîâ.Ïîóìîë÷àíèþ
1.5e-
8
.
step.min
ìèíèìàëüíûéðàçìåðøàãàèòåðàöèè.Ïîóìîë÷àíèþ

2.2e-14
.

lower
è
upper
âåêòîðàíèæíÿÿèâåðõíÿÿãðàíèöûäëÿàðãóìåíòîâ
îïòèìèçàöèè.
Ðåçóëüòàòîìðàáîòûôóíêöèèÿâëÿåòñÿñïèñîê,âêëþ÷àþùèéâñåáÿñëåäó-
þùèåïîëÿ:

par
îïòèìàëüíûéíàáîðàðãóìåíòîâôóíêöèè(ò.å.òî÷êàìèíèìóìà).

objective
çíà÷åíèåôóíêöèèâíàéäåííîéòî÷êåìèíèìóìà.

convergence
êîäçàâåðøåíèÿðàáîòûôóíêöèè.
0
óñïåøíîåçàâåð-
øåíèå.

message
ñòðîêàñäîïîëíèòåëüíîéèíôîðìàöèåé(ëèáî
NULL
).

iterations
÷èñëîèòåðàöèé.N

evaluations
÷èñëîâûçîâîâöåëåâîéôóíêöèèèôóíêöèèãðàäèåíòîâ.
Ïðèìåð65.
Ñíîâàíàéä¼ììèíèìóìôóíêöèè
f
(
x
)=
(
x

5)
2
sin
x
(
x
+2)
2
íàèíòåðâà-
ëàõ
[

1;4]
,
[0;3]
è
[1
;
3]
.
�f=function(x,a,b){(x-a)^2*sin(x)/((x+b)^2)}
�nlminb(4,f,a=5,b=2,)
$par
[1]3.642276
$objective
203
[1]-0.02779574
$convergence
[1]0
$message
[1]"relativeconvergence(4)"
$iterations
[1]7
$evaluations
functiongradient
910
Çàäàäèìîãðàíè÷åíèÿîòíîñèòåëüíî
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]"relativeconvergence(4)"
$iterations
[1]7
$evaluations
functiongradient
810
Ïîñëåäíÿÿôóíêöèÿ,êîòîðàÿáóäåòðàññìîòðåíàâýòîìðàçäåëåýòî
ôóíêöèÿ
constrOptim
,äëÿêîòîðîéìîæíîçàäàâàòüîãðàíè÷åíèÿââèäåëè-
íåéíûõíåðàâåíñòâ.

grad
ãðàäèåíòôóíêöèè
f
(òîæåôóíêöèÿ)ëèáî
NULL
.

ui
ìàòðèöàîãðàíè÷åíèéðàçìåðà
k

p
.

ci
âåêòîðîãðàíè÷åíèéäëèíû
k
.

mu
ïàðàìåòðîòëàäêèîïòèìèçàöèè.

control
,
Òåïåðüíàéä¼ìå¼ìèíèìóìâîáëàñòè
f
x

0
:
9
;y

x�
0
:
1
g
ñïîìîùüþ
ôóíêöèè
constrOptim()
�constrOptim(c(.5,0),fr,grr,ui=rbind(c(-1,0),c(1,-1)),ci=c(-0.9,0.1))
$par
[1]0.88913350.7891335
$value
[1]0.01249441
$counts
functiongradient
25648
$convergence
[1]0
$message
NULL
$outer.iterations
[1]4
$barrier.value
[1]-7.399944e-05
Àòåïåðüìèíèìóìýòîéæåôóíêöèè,íîóæåñäðóãèìèîãðàíè÷åíèÿìè
f
x

1
;y

1
g
�constrOptim(c(-1.2,0.9),fr,grr,ui=rbind(c(-1,0),c(0,-1)),ci=c(-1,-1))
$par
[1]0.99997610.9999521
$value
[1]5.734117e-10
$counts
functiongradient
29794
$convergence
[1]0
$message
NULL
$outer.iterations
[1]12
$barrier.value
[1]-0.0001999195
206
Ëèòåðàòóðà
[1]RDevelopmentCoreTeam(2009).
R:Alanguageandenvironmentfor
statisticalcomputing.
RFoundationforStatisticalComputing,Vienna,
Austria.ISBN3-900051-07-0,URLhttp://www.R-project.org.
[2]BrianS.EverittandTorstenHothorn(2005).
AHandbookofStatistical
AnalysesUsingR
,LondonandErlangen,http://www.R-project.org.
[3]MichaelJ.Crawley(2007).
TheRBook
,JohnWiley&SonsLtd,The
Atrium,SouthernGate,Chichester.ISBN-13:978-0-470-51024-7.
[4]JohnM.Chambers(2008).
SoftwareforDataAnalysis.Programming
withR
,SpringerScience+BusinessMedia,LLC,USA,ISBN:978-0-387-
75935-7
[5]PhilSpector(2008).
DataManipulationwithR
,Springer
Science+BusinessMedia,LLC,USA,ISBN978-0-387-74730-9
[6]YosefCohenandJeremiahY.Cohen(2008).
StatisticsandDatawith
R
,JohnWiley&SonsLtd,TheAtrium,SouthernGate,Chichester,ISBN
978-0-470-75805-2
[7]J.H.Maindonald(2001).
UsingRforDataAnalysisandGraphicsAn
Introduction.
StatisticalConsultingUnitoftheGraduateSchool,Australian
NationalUniversity.
[8]JohnVerzani.
simpleRUsingRforIntroductoryStatistics
,
http://cran.R-project.org/other-docs.html.
[9]JimLemon.
KickstartingR
,http://cran.R-project.org/other-docs.html.
[11]ÇîëîòûõÍ.Þ.,ÏîëîâèíêèíÀ.Í.(2007).
Ìàøèííîåîáó÷åíèå.Ëàáî-
ðàòîðíûéïðàêòèêóì
,http://www.uic.unn.ru/zny/ml/
[12]ÌåðåòèëîâÌ.À.(2006).
Ìåòîäè÷åñêèåóêàçàíèÿêëàáîðàòîð-
íûìðàáîòàìïîêóðñó¾Ìåòîäûàíàëèçàäàííûõ¿
,http://gis-

Приложенные файлы

  • pdf 4746841
    Размер файла: 1 MB Загрузок: 0

Добавить комментарий