Professional Documents
Culture Documents
R Tips
R Tips
R S
OS
S-plus
R
R
R ()
()
The R Book ()
Introductory Statistics with R Peter Dalgaard (Springer)
R ( HP ) http : //aoki2.si.gunma u.ac.jp/R/
R
3
() ()
()
()
() \ Y Y R \ Y \
UNIX LINUX Mac OS \ Windows
Y OS
*1
10
11
1.0
R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1
R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.1
1.1.1
Windows R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.1.2
Mac OS X R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.1.3
Linux R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
16
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.1.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.1.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.1.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.1.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.1.6
R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.2
2.1.7
2.1.8
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.1.9
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.2.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
1
. . . . . . . . . . . . . . . . . . . . . . . . 27
2 rgl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3 rgl . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
4 rgl
. . . . . . . . . . . . . . . . . . . . . . . . 28
5 rgl unload . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
2.2.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Windows R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Mac OS X R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
II
32
33
3.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.1.6
3.2
3.3
4
NULL
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
NA NaN Inf . . . . . . . . . . . . . . . . . . . . 33
numeric . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
complex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
character . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
logical . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2.1
print()
3.2.2
cat() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2.3
sprintf() . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.2.4
str() . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.2.5
comment() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.2.6
sink() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
38
4.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.3
4.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.8
4.9
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.7.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.7.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.7.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
4.7.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . 44
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.8.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.8.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
4.8.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
NULLNANaNInf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.9.1
NULLNANaNInf . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.9.2
NA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
4.9.3
NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
48
5.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
5.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
5.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
5.5
5.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.5.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.5.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.5.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
5.5.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
5.5.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.6.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
5.6.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.6.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
5.6.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.6.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
58
6.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
6.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
6.3
apply() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
6.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
6.5
names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
III
7
7.1
7.2
7.3
7.4
7.5
67
68
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
7.1.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.1.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
7.1.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
7.2.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
7.2.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
7.2.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
7.3.1
if else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
7.3.2
switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.3.3
&& || . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.4.1
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.4.2
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.4.3
break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
7.4.4
repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
7.5.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
7.5.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
7.5.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
7.5.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.5.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.5.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.5.7
<< . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
7.5.8
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.5.9
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.5.10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
7.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
7.6.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
7.6.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
missing()
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
7.6.3
7.6.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
7.6.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
7.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.8
7.9
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.9.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
7.9.2
readline() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
7.9.3
menu() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
86
8.0
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
8.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
8.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
8.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
8.8
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
8.9
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8.10
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8.11
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
IV
92
93
9.1
9.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9.1.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
9.1.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.2.0
9.2.1
. . . . . . . . . . . . . . . . . . . . . . . . . 95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.2.2
EXCEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
csv . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
EXCEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
xls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
9.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
9.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
9.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
9.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
9.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
9.7.1
9.7.2
9.7.3
R . . . . . . . . . . . . . . . . . . . . . . 105
9.7.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
9.7.5
scan()
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
. . . . . . . . . . . . . . . . . . . . . 107
scan() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
scan() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
9.7.6
10
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
110
10.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
10.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
10.3
EXCEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
10.4
113
11
114
11.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
11.2
11.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
11.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
11.5
plot()
11.6
plot() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
11.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.8
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.9
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
11.10
11.11
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
12
12.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
121
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
12.1.5 curve()
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
12.5.1 image() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
12.5.2 persp() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
12.5.3 contour() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
12.5.4 scatterplot3d() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
12.5.5 lattice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
12.5.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
12.5.7 grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
13
131
13.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
13.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
13.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
13.4
eps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
140
14.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
14.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
14.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
14.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
14.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
14.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
14.3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
14.3.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
15
147
15.1
locator() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
15.2
identify() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
15.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
15.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
15.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
layout() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
split.screen() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
15.3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
VI
153
16
154
16.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
16.1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
16.1.2 . . . . . . . . . . . . . . . . . . . . . . . . 155
16.1.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
16.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
16.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
16.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
16.3
R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
16.3.1 set.seed() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
16.3.2 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
16.3.3 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
3 t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
16.3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
16.3.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
16.3.6 2 x 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
16.4
17
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
163
17.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
17.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
17.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
17.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
17.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
17.6
lowess() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
18
18.1
18.2
170
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
18.1.1 (1) t
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
18.1.2 (2) t
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
18.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
18.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
18.2.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
18.2.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
18.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
t
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
U . . . . . . . . . . . . . . . . . . . 176
F . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
. . . . . . . . . . . . . . . . . . . . . . . . 176
18.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
18.4.1 2 () . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
18.4.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
( 2 2 )
. . . . . . . . . . . . . . . . 178
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
( 2 2 ) . . . . . . . . . . . . . . . . . . . . . . . . 179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
18.4.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
18.4.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
. . . . . . . . . . . . . . . . . . . . . 183
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
. . . . . . . . . . . . . . . . . . . . . . . . . 183
18.5
19
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
185
19.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
19.2
lsfit() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
19.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
19.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
20
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
193
20.1
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
20.2
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
20.3
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
20.4
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
20.5
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
20.6
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
20.7
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
20.8
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
200
200
11
1.0 R
RWeb ()http : //r.nakama.ne.jp/Rweb jp/ R R
1.1 R
R 2.1.0 *1 R
1.1.1 Windows R
!
!
#
#
"Next> $[I accept the agreement] "Next> $
*2
1.1
1.2
"Next> $ 1.4
[Chinese/Japanese/Korean installation]
*1
*2
R 2.1.1
R 2.1.1 CRANhttp : //cran.md.tsukuba.ac.jp/bin/windows/base/rpatched.html
12
1.3
1.4
1.5
1.6
1.7 R
.Rdata
R R
"Next> $ 1.8
1.7
1.8
1.9 "Finish $
R etc
13
1.9
1.10
Rconsole Rdevga
Rconsole
17 font = TT Courier New -> font = TT MS Gothic
18 points = 10
-> points = 14
Rdevga12 15
TT Arial : plain
TT Arial : bold
TT Arial : italic
R
[R] [R 2.1.0 patched] [R 2.1.0 patched]
R
14
1.1.2 Mac OS X R
R-2.1.0.dmg (Mac OS X 10.3 ) R-2.0.1.dmg (Mac OS X 10.2) The R Project
http : //cran.md.tsukuba.ac.jp/bin/macosx/
R 2.1.0 mpkg
1.11
1.12
%
'
2 & (
1.13
1.14
"Continue $
Agree
&
1.16
1.15
'
%
&
'
'
%
&
'
&
15
1.17
1.18
1.19 [Finish]
1.19
1.20
R
[R] R
1.20
1.1.3 Linux R
Linux R RjpWikihttp : //www.okada.jp.org/RW iki/index.php?RjpW iki
RjpWiki R
Wiki
Linux R
16
2.1
2.1.1
R OS
Windows [R 2.1.0 Patched]
[R] [R 2.1.0 Patched]
R R
R : Copyright 2005, The R Foundation for Statistical Computing
Version 2.1.0 Patched (2005-04-20), ISBN 3-900051-07-0
R
license() licence()
R
contributors()
R R
citation()
demo()
help()
help.start() HTML
q() R
>
!
#
! #
" $ "Enter $
*1 R
>
*1
17
! #
> 1+2 " $
[1] 3
! #
> sin(pi/2) " $
[1] 1
>
R *2
(1) R
(2) R
! #
" $ R
!
#
> 1 + 2 "Enter $
> 1 + 2 R
2.1.2
2 R
! #
> q() " $
! #
> quit() " $
Windows [] []
%
' %
'
&
( &
2.2
2.1
Mac OS X [R] [R ]
%
' %
'
&
( &
*2
1 + 2 3 [1]
18
2.4
2.3
save.image()
! #
> save.image("C:/filename.RData") " $
2.1.3
R ( )
%/%
%%
! #
> (1 + 2 - 3 * 4) / 5^6 " $
[1] -0.000576
sqrt(2) sqrt()
! #
> sqrt(2) " $
[1] 1.414213
! #
> sqrt(100) + round(100) / log10(100) " $
[1] 60
R x 2.0 0.5
sin(x)
cos(x)
tan(x)
sinh(x)
cosh(x)
tanh(x)
sin
cos
tan
sinh
cosh
tanh
asin(x)
acos(x)
atan(x)
asinh(x)
acosh(x)
atanh(x)
sin
cos
tan
sinh
cosh
tanh
log(x)
log10(x)
log2(x)
log1p(x)
exp(x)
expm1(x)
log(1+x)
exp(x)1
sqrt(x)
round(x)
trunc(x)
floor(x)
ceiling(x)
signif(x,a)
x a
19
2.1.4
R (object)
*3 = ;
x 2
!
> x <- 2 "
! #
> x " $
[1] 2
!
> x + 3 "
[1] 5
!
#
$
#
$
x < 2 x + 3
2 + 3
! #
> x <- 2 " $
! #
> x <- 3 " $
> x
!
!
! #
> y <- x " $
[1] 3
< *4
<
>
assign(, )
()
! #
> (x <- 1:4) " $
[1] 1 2 3 4
R *5
else
for
function
if
in
next
2.1.5
! #
! #
" $
repeat
! #
" $
return
while
TRUE
FALSE
" $
! #
" $
break
*6
*3
A3 3A
*4 y < x < 10
*5 objects()
x x rm() rm(list=ls(all=TRUE))
*6 history()
20
2.1.6 R
R 1 R
1
R
Windows
[] [] R
2.6
2.5
[] [ R ]
!
# ! # ! #
"Ctrl $+"R $ "F5 $*7
2.7
*7
R
!
#! #
"Ctrl $+"R $
2.8
#! #
"Ctrl $+"A $
21
Mac OS X
[] [] R
2.9
2.10
!
# ! #
[] [] "Command $+" $ *8
2.12
2.11
Linux
vi Emacs R
Emacs XEmacs R ESS (Emacs
! #
" $
R R R > +
> (1 + 2 - 3 * 4) / 5^6
[1] -0.000576
!
#! #
R "Command $+"A $
!
#! #
"Command $+" $
9
*8
http : //stat.ethz.ch/ESS/
22
#
# *10
> (1 + 2 - 3 * 4) / 5^6
[1] -0.000576
(1 + 2 3 4) / 56
2.1.7
( R )
*11 R
*12
> setwd("c:/usr")
> getwd()
[1] "c:/usr"
Windows [] [...]
2.14
2.13
Mac OS X [] [...]
*10
R
Windows Ysetwd(c:Yusr) setwd(c:/usr)
*12 dir()
*11
23
2.15
2.16
2.1.8
solve() 2 solve
*13
> help(solve)
> ?solve
R
> ?Syntax
for [ (> ?for )
> help("for")
solve help.search(
) *14
> help.search("solve")
2.1.9
(a) R x X R
(b) sin
2
> 1
[1] 3
> 1 + 2
[1] 3
*13
*14
args(solve)
RSiteSearch() R-help mailing list R R
24
(c) ;
> 1+2;
3-4
[1] 3
[1] -1
(d) R
> +
> 1 +
+ 2
[1] 3
2 + +1 + +2>
!
+1 + 2
"Esc $
() S-PLUS S
+ Continue string: +
R
(e) R x
> x
[1] 1 2
(f) (
> x <- c(1, 2)
[1] 1 2
> f <- function(x) y <- 2*x
> f( c(1, 2) )
[1] 2 4
(g) R
solve()
> example(solve)
example()
25
(h) Windows R [
] [] 2.17 [
] [] 2.18
2.17
2.18
.r.q
2.19
Mac OS X R
[] [] 2.20
[] [] 2.21
26
2.20
2.21
2.22
source.txt
> source("C:/source.txt")
data.txt
R Windows
22
C:YProgram FilesYRYrw2010pat Mac OS X /Users/*15
*15
27
2.2
R
library()
> library()
base
boot
RS-Plus
class
Classification
cluster
foreign
R ( SAS )
grid
KernSmooth
lattice
MASS
methods
R ,
mgcv
(GCV,GAM)
nlme
nnet
rpart
regression trees
spatial
splines
stats
stats4
S4
survival
tcltk
Tcl/Tk
tools
utils
2.2.1
500 2005 6
> library(help="<>")
( base )
library()
1
> search()
28
2 rgl
> library(rgl)
3 rgl
> example(rgl.surface)
>
> n <- 100
> rgl.clear()
> rgl.bg(color=c("white", "black"))
> rgl.spheres(rnorm(n), rnorm(n), rnorm(n), radius=0.2, color=rainbow(10))
> rgl.bbox(color="#333377")
2.23
2.24
volcano
rgl
rgl.open()
rgl.set()
rgl.cur()
rgl.close()
rgl.clear()
rgl.pop()
rgl.quit()
rgl.viewpoint()
rgl.light()
rgl.bg()
rgl.points()
rgl.sprites()
rgl.lines()
rgl.triangles()
rgl.quads()
rgl.texts()
rgl.surface()
rgl.spheres()
rgl.snapshot()
rgl.bbox()
4 rgl
> library(help="rgl")
> help(package="rgl")
29
5 rgl unload
> detach("package:rgl")
CRAN update.packages()
Windows R R rgl
rgl R
rgl
> help.search("solve")
# solve
> help(package="MASS")
# MASS
> base::log
# base log
> base::"+"
# base "+"
> grid::grid.newpage()
# grid
> data()
# base
# USArrests
2.2.2
R-Project CRAN
RjpWiki CRAN CRAN
CRAN URL CRAN.packages() install.packages()
xtable
> options(CRAN="http://cran.r-project.org")
> install.packages("xtable")
install.packages()
xtable 1.2-3.zip
*16
*17
installed.packages()
CRAN.packages()
30
2.25
2.26 CRAN
"OK $
2.27
[] [] 2.28
"OK $ 2.29
2.28
2.27
2.29
Windows R R
--internet2C:YProgram
FilesYRYrw2010YbinYRgui.exe --internet2
2.30
31
Mac OS X R
Mac OS X R GUI
%
'
[] []
&
(
2.31
2.32 CRAN
"OK $
2.33
2.34
[] []
'
&
2.35
II
33
3.1
R
3.1.1 NULL
R NULL 0 NULL 0
names for
NULL
+
> NA
[1] NA
> 0/0
[1] NaN
> 1/0
# NaN Inf
[1] Inf
+
3.1.3 numeric
)
> 123
[1] 123
> 1e10
[1] 1e+10
+
# 10^(10)
,
34
3.1.4 complex
1 1+0i 1 + i 1+1i 1+i i
)
*1
> 1 + 1i
[1] 1+1i
> 1 + 0i
# 1
[1] 1+0i
+
3.1.5 character
1 *2
)
> "abc"
[1] "abc"
# " 2
> ""
[1] ""
+
# " " ()
3.1.6 logical
TRUE () FALSE () TF *3 TRUE
FALSE
)
> T
[1] TRUE
> TRUE
[1] TRUE
+
*1
2 2+0i
> log(-2)
[1] NaN
> log(-2+0i)
[1] 0.6931472+3.141593i
*2
*3
Y
T F TRUE FALSE T F TRUE FALSE
35
3.2
3.2.1 print()
print() *4
> print(x)
[1] "one"
> print(x, quote=F)
# ""
[1] one
3.2.2 cat()
cat() print()
cat() *5 YYYt Yn Yf
Y*6 Y 3 8
3.2.3 sprintf()
sprintf() *7
sprintf(%5.1f, )
3 1 1
sprintf(%f, )
f 6
sprintf(%10f, )
% f + 0
sprintf(%3d, as.integer())
3
%3i
sprintf(%e, )
g G
sprintf(%s, as.character())
*4
as.character()
page()
*6 OS Y \ Y
*7 formatC()
*5
36
%5.0f 5
3.2.4 str()
str()
summary()
> str(x)
chr "12345"
> summary(x)
Length
Class
Mode
1 character character
3.2.5 comment()
comment()
comment() str()
3.2.6 sink()
R sink()
dump()dget()dput()capture.output()
> sink("output.txt")
# output.txt
> sink()
37
3.3
options() options() *8
continue
device
digits
expressions
500
prompt
>
scipen
show.error.messages
TRUEFALSE
timeout
60
warn
0
1 2
width
80
CRAN
7 digits ()
> options(digits=10)
2
# 10
> options(scipen=0);
print(1e5)
[1] 1e+05
> options(scipen=1);
print(1e5)
[1] 100000
3
width
*8
help(options)
38
> 1 + 2
[1] 3
1 + 2 3 [1] 1 1
4.1
c() *1
# 5
[1] 1 2 3 4 5
< c()
c
# 5
> length(x)
[1] 5
c()
# 1
> 1:5
[1]
> 3:-3
[1] 3 2 1 0 -1 -2 -3
*1
5 1
# 3 -3 1
39
# (1 2 3) 8
# 1 10 5
# 1:length(c(2,8,5))
> seq(along=c(2,8,5))
[1] 1 2 3
a:b
a b (a < b) 1 a > b 1
seq(a, b, length = n)
a, b n
seq(a, b, by = c)
a b c
sequence(c(3,2))
(1 2 3 1 2)
rep(a:b, times = c)
a b 1 c c
rep(a:b, length = c)
a b 1 c
unique(x)
numeric(n)
0 n
4.2
()
+ () () () / () %/% ()%% () ()
[1] 5 7 9
> c(1.0, 2.0) * c(3.0, 4.0)
# c(1*3, 2*4)
[1] 3 8
[1] 4 4 2 2
> c(1.0, 2.0, 3.0, 4.0, 5.0) - 1
# 1
[1] 0 1 2 3 4
#
> 1 / 2:5
[1] 0.5000000 0.3333333 0.2500000 0.2000000
< == >
== 3
...
TRUE FALSE
# 1 == 1, 20 == 2, 30
60 ...
40
4.3
18
> sqrt(1:5)
[1] 1.000000 1.414214 1.732051 2.000000 2.236068
(sqrt() cos() )
NA NA
sum()
mean()
var()
median()
cor()
max()
min()
prod()
cumsum()
sd()
sort()
rev()
pmax()
pmin()
range()
match()
diff()
rank()
order()
*2
> rev(x)
[1] 5 4 3 2 1
> y <- cumsum(x)
# y x
> mean(y)
# y
[1] 7
> mean(y, 0.5)
# y
[1] 6
> sort(y)
[1]
6 10
# y
> order(y)
[1] 1 2 3 4 5
# y
> rank(y)
[1] 1 2 3 4 5
> z <- c(100, 20, 4, 2, 1)
# x, y, z
> pmax(x, y, z)
[1] 100
20
> pmin(x, y, z)
10
# x, y, z
[1] 0 1 3 2 1
*2
cumsum() cumsum() ,
cumprod(), cummax(), cummin()
41
4.4
union(x, y)
intersect(x, y)
setdiff(x, y)
setequal(x, y)
is.element(x, y)
x y x%in%y
y <- 1:4
#
> intersect(x, y)
[1] 1 2 3 4
> setequal(x, y)
[1] FALSE
# x y
> is.element(x, y)
[1]
TRUE
TRUE
TRUE
TRUE FALSE
4.5
1, 2, x
x[k]
k 0 0
x[k] < a
k a
x[]
x[]
x[]
TRUE
x[]
x[]
names
*3
# 3
[1] 3
> x[5] <- 0
# 5 0
> x
# x
[1] 1 2 3 4 0
*3
Mathematica Maple R
42
*4
> x[2:5]
[1] 2 3 4 5
# 1 3
> x[c(-1,-3)]
[1] 2 4 5
# 30
# 2 & |
[1] 2 3
which() *5
# 5.2
> x[num]
[1] 5
4.6
replace()
# x <- 1:5
# x 2, 4 0
[1] 1 0 3 0 5
> ( z <- replace(x, c(2,4), NA) )
[1]
1 NA
3 NA
1 10
# NA
4 10
7 10
# z NA 10
2 c() 2 append()
append()
z <- c(7, 8, 9)
> c(x, y, z)
[1] 1 2 3 4 5 6 7 8 9
> ( w <- append(x, z) )
# c(x, z)
[1] 1 2 3 7 8 9
> append(w, y, after=3)
# w 3 y
[1] 1 2 3 4 5 6 7 8 9
*4
& | == 70 [ ]
NA
*5 which.max(), which.min()
43
4.7
4.7.1
*6
# 1+i 1+1i
# 1+i i
# re
im
# modarg
1+0.000000e+00i -2+2.449213e-16i
Re(z)
Im(z)
Mod(z)
abs(z)
Arg(z)
Conj(z)
4.7.2
TRUEFALSETF
& () | () ! () xor ()
>
[1]
TRUE FALSE
TRUE
> !c(T, F, T)
[1] FALSE
TRUE FALSE
TRUE
any()
TRUE TRUE
all()
TRUE TRUE
# x 1 0.8
[1] TRUE
> all(x < 0.8)
# x 0.8
[1] FALSE
*6
44
4.7.3
"TOKYO"
"NAGOYA"
"OSAKA"
"HAKATA"
ASCII
4.7.4
factor() levels()
str()
[1] L M H L M H L M H L M H L M H L M H L M H L M H L M H
Levels: H L M
# "L", "M", "H" ...
> levels(fc)
[1] "H" "L" "M"
levels
> str(fc)
Treat
Treat
Treat
Treat
Treat
Treat
Treat
45
4.8
4.8.1
paste()
*7
"Jan 1 st"
"Feb 2 nd"
"Mar 3 rd"
"Apr 4 th"
[7]
"Jul 7 th"
"Aug 8 th"
2 1 sep (
) collapse ()
sep
# /
[1] "/usr/local/bin"
> ( x <- paste(1:4) )
[1] "1" "2" "3" "4"
> paste(x, collapse="abc")
[1] "1abc2abc3abc4"
> paste(x, collapse="")
[1] "1234"
4.8.2
substring()
> substring("abcdefg", 2, 5)
# substring( , , )
[1] "bcde"
4.8.3 R
eval(parse(text=)) eval.parent(parse(text=))
x <- 1:5
*7
46
charmatch()
chartr()
grep(), match()
gsub(), sub()
nchar()
pmatch()
toupper()
tolower()
()
strsplit()
strwrap()
substr()
()
4.9 NULLNANaNInf
4.9.1 NULLNANaNInf
NULL () NA () NaN () Inf () NA
NAN NaN NaN
==
# NA ...
> x == NA
# NA NA
[1] NA NA NA
4 NA
is.null()
is.na()
is.nan()
is.finite()
is.infinite()
complete.cases()
NULL
NA
NaN
> is.na(NA)
[1] TRUE
> is.nan(NA)
[1] FALSE
> x <- c(1, NA, 3, 4)
> is.na(x)
[1] FALSE TRUE FALSE FALSE
4.9.2 NA
NA NA
# a NA
[1] 1 2 4 5
> a <- ifelse(is.na(a), 0, a)
> a
[1] 1 2 0 4 5 0
# NA 0
47
NA NA NA
# NA x
> y
# y
[1] 1
9 10
4.9.3 NULL
NULL
NULL NA NA NULL
> length(NULL)
# "NULL"
[1] 0
# 0
> names(a)
NULL
NULL
# 0 x
# x i^2
> x
[1]
*8
16
25
36
49
64
81 100
48
5.1
R *1
)
(2) matrix(, , )
+
!
"
1 3 5
(1, 2, 3, 4, 5, 6)
2 4 6
(1)
[2,]
# nrow ncol
# matrix(1:6, 2, 3)
() (
) byrow=T
# matrix(1:6, 2, 3, byrow=T)
[2,]
5.2
1, 2, 1, 2, *2
x [ ]
[2,]
*1
R 2 R 2
array()
*2 x[k] 1 1, 2, 2
49
> x[1, 2]
[1] 3
# 1 2 1,3
x[2, ]
rowSums()
x[ ,2]
colSums()
x[1, 2]
1 2
rowMeans()
x[c(1,2), 2]
1,2 2
colMeans()
x[c(1,2), c(1,3)]
1, 2 1, 3
x[,c(1, 3)]
1, 3
x[, c(T,F,T)]
1, 3
[ ]
> x[matrix(c(1,2,2,3), 2, 2)]
[1] 3 6
# x[cbind(c(1,2), c(3,4))]
> x[1:4]
[1] 1 2 3 4
# 4
drop=FALSE
()
# 2
[2,]
5.3
rbind()
cbind()
[2,]
[2,]
[3,]
50
3
> ( x <- rbind(1:3, c(4, 5, 6), 7:9) )
# 3
[2,]
[3,]
# 3
[2,]
[3,]
5.4
*3
# 2 * 2
# 2 * 2
> a + b - a %*% b
> a * b
> a %o% b
# outer(x,y)
> a %x% b
%%
# persp(x,y,z,expand=0.5) 3
# 2 * 2
> a * (1:2)
# (1:2) * a
[,1] [,2]
[1,]
[2,]
> 1/a
[,1]
[,2]
[1,]
1.0 0.3333333
[2,]
0.5 0.2500000
*3
kronecker()
51
5.5
R
matrix(0,nrow=2,ncol=3)
2 3 matrix(1,nrow=2,ncol=3)
diag(3)
diag(1:3)
(1 2 3) 3 3
diag(X) < 1
X 1
2 2 X (1 2)
X
X[upper.tri(X)] < 0
() 0 0 up-
per.tri(X,diag=TRUE)
X[lower.tri(X)] < 0
() 0
sum(X2)
sum(diag(t(X)%%X))
solve(X)
ginv(X)
X library(MASS)
crossprod(X)
X t(X)%%X
crossprod(X,Y)
X Y t(X)%%Y
eigen(X)
qr(X)
X QR
chol(X)
X X = t(A)
%% A A
svd(X)
X X = U %% D %% t
X D V
U
det(X)
X prod(svd(X)$d)
5.5.1
[2,]
> y
# y
[,1] [,2]
[1,]
[2,]
52
5.5.2
A.x = b x solve()
> a <- matrix(c(0,1,2,3,4,5,6,7,9), 3,3)
3y + 6z =
x + 4y + 7z =
> solve(a,b)
# 2x + 5y + 9z = -2
[,1]
[1,] -2.333333
[2,]
2.333333
[3,] -1.000000
backsolve() backsolve()
0
forwardsolve()
# x + 2y + 3z = 8
c(0,1,1),
c(0,0,2))
y +
z = 4
2z = 2
> r %*% y
# (8
# (8 -12 -5)
2)
5.5.3
( ) eigen()
5.3722813 -0.3722813
#
> z$vectors[,1]
5.3722813
> z$vectors[,2]
[1] -0.9093767
0.4159736
53
5.5.4
svd()
# A
> A
# A
[,1] [,2] [,3]
[1,]
[2,]
[3,]
2
# B
> B
[,1]
[,2]
[,3]
> B%*%B
[,1] [,2] [,3]
[1,]
[2,]
[3,]
5.5.5
A 2 A2 2 A%*%A
A An A A = V %% D %% t(V)
An = V %% (Dn) %% t(V)
D Dn D n
matpow() *4
y <- eigen(x)
+ }
> matpow(diag(1:2))
[,1] [,2]
[1,]
[2,]
*4
matpow() RjpWiki
54
matexp()
y <- eigen(x)
+ }
> matexp(matrix(0, 2, 2))
[,1] [,2]
[1,]
[2,]
> matexp(diag(2))
[,1]
5.6
5.6.1
dim ( ) 2
dim dim() nrow() ncol()
# 2 * 3
> dim(x)
# dim
[1] 2 3
# dim(x)[1]
> nrow(x)
[1] 2
# dim(x)[2]
> ncol(x)
[1] 3
matrix() dim
dim matrix()
> x <- matrix(1:6, nrow=2, ncol=3)
# 2 * 3
# 3 * 2
# 2 * 6
[2,]
6
# matrix() 1
[2,]
#
# as.vector()
55
5.6.2
matrix() nrow ncol
matrix()
# ncol = 2
# nrow = 3
[2,]
[2,]
[2,]
# 2 * 3
> row(x)
[2,]
2
#
> col(x)
[,1] [,2] [,3]
[1,]
[2,]
# 3 * 4
10
[2,]
11
[3,]
12
# 2 * 2
56
> x
[,1] [,2] [,3] [,4]
[1,]
333
777
[2,]
555
999
[3,]
12
which() arr.ind=TRUE
arr.ind=FALSE 2
[2,]
[2,]
[3,]
[2,]
4
# 2 x
> x[x%%2==0]
[1] 2 4
> x[x%%2==0] <- 0
# 2 x 0
> x
[,1] [,2]
[1,]
[2,]
5.6.3
apply(X, MARGIN, , ...) ()
MARGIN
[2,]
# -> rowSums(x)
57
mean sum
mean(x[i, ])
mean(x[, i])
mean(x[ , ])
apply(x, 1, sum)
colSums(x)
apply(x, 2, sum)
rowSums(x)
5.6.4
order()
max.col()
[,2]
> order(x[,2])
[1] 2 1
# 2
> x[order(x[,2]),]
[,1]
[,2]
> x[,order(x[1,])]
[,1]
[,2]
5.6.5
image()
0.0
0.2
0.4
0.6
0.8
1.0
0.0
5.1
0.2
0.4
0.6
0.8
1.0
58
6.1
2 3 4 ...
(2) array(, )
+
(1)
array() dim k
k dim()
# 3
, , 1
[,1] [,2] [,3] [,4]
[1,]
, , 2
[,1] [,2] [,3] [,4]
[1,]
8
# x[1 2 ]
> x[1, 4, ]
[1] 4 8
57 3 x i mean(x[i,i, ])
11
[2,]
10
12
59
> x
# 4
# xt x
6.2
R 1 a 1
1 1
list()
[[1]]
[1] 1 2 3 4 5
# 2 1
[[2]]
[[2]][[1]]
[1] "Its my list."
# 2 2
[[2]][[2]]
[1]
TRUE FALSE
TRUE
length(x)
x < as.list(NULL)
x[1]
x 1
x[1:2]
1 2
x[-1]
x[c(T,F,T)]
1 3
x[[1]]
x 1
x[2] < 10
2 10
x[[2]] < 10
2 10
3 NULL
[[ ]] [ ]
*1
[ ]
[[1]]
[1] 1 2 3
*1
[[ ]]
[ ]
60
> x[[1]]
[1] 1 2 3
> x[1:2]
# 1 2
[[1]]
# () x[[1:2]] x[[1]][[2]] ()
[1] 1 2 3
[[2]]
[1] 4 5 6
c() append()
TRUE FALSE
TRUE
# rep()
1 unlist()
split()
8 10
$odd
[1] 1 3 5 7 9
> unlist(x)
even1 even2 even3 even4 even5
2
odd1
odd2
odd3
odd4
odd5
10
6.3 apply()
apply() apply(), mapply(), lapply(), sapply(), tapply()
m n X
1 R (for while) X < X+1
apply() C JAVA
apply()
MARGIN
MARGIN = 1 MARGIN = 2
MARGIN = c(1,2)
lapply(X, , ...)
sapply(X, , ...)
61
INDEX X
()
mapply( F , x, y, z, ... )
sapply() x, y, z,
F(x, y, z, ...) mapply(sum, 1:3, 4:6)
[ sum(1,4), sum(2,5), sum(3,6) ] = [ 5, 7, 9 ] mapply(sum, 1:3,
sweep(X, MARGIN, ,
MARGIN
FUN=, ...)
scale() FUN=+
apply() sweep()
[2,]
8
#
7 11 15
#
[,2]
[,3]
[,4]
-3
-1
[2,]
-3
-1
lapply()sapply() lapply()
sapply()
*2
$a
# 1
[1] 5.5
$beta
# 2
[1] 4.535125
$logic
# (TRUE=1, FALSE=0)
[1] 0.5
*2
62
beta
logic
tapply()
> data(warpbreaks)
# 1
> attach(warpbreaks)
> levels(fc)
6.4
paste()
mode()
is.numeric()
is.integer()
is.complex()
is.character()
is.logical()
storage.mode()
as.numeric()
as.integer()
as.complex()
as.character()
as.logical()
> x
[1] 1+0i 0+0i 1+0i 0+0i 1+0i
> mode(x) <- "logical"
> x
[1]
TRUE FALSE
TRUE FALSE
TRUE
63
*3
TRUE 1 ,
FALSE 0
0 TRUE
*4
0 TRUE ,
0 FALSE
is.vector()
is.matrix()
is.array()
is.list()
is.data.frame()
is.factor()
is.ordered()
as.vector()
as.matrix()
as.array()
as.list()
as.data.frame()
as.factor()
as.ordered()
# x
> is.list(x)
[1] FALSE
> x <- as.list(x)
# x
> is.list(x)
# x ...
[1] TRUE
#
> is.vector(x)
[1] TRUE
6.5 names
names
names
# BMI
> mydata
# mydata
weight height
57
*3
*4
173
BMI
19
if (2.718) TRUE if
64
names
> mydata["BMI"]
BMI
19
> mydata["BMI"][[1]]
[1] 19
2
names names
# BMI
> x
$weight
[1] 57
$height
[1] "173"
$BMI
[1] 1 9 0 4 5 0 7
# ( x[["weight"]] )
> x$weight
[1] 57
[[ ]] $
> temp <- "height"
# temp "height"
> x[[temp]]
# [[ ]]
[1] "173"
# x[["height"]]
> x$temp
# $
NULL
# x$temp NULL
names
# 2 * 3
> x
left center right
up
down
65
> x["up","right"]
[1] 5
dimnames dimnames
row2
dimnames NULL
dimnames
# x, y
# x, y ()
x y
[1,] 1 4
[2,] 2 5
[3,] 3 6
5
dimnames names k
dimnames k NULL
9 11 13 15
[2,] 10 12 14 16
66
dimnames NULL
dimnames
2 ( names )
mode length names ( factor ) levels dim
mode length attributes()
mode as. () attr()
x dim x 2 2
# x
> attributes(x)
# mode length
III
68
7.1
R date()
> date()
[1] "Mon Jul 21 16:31:52 2003"
log(x) x x
> x <- 10
# x 10
> log(x)
# x log10(x)
[1] 2.302585
2
> x <- 10
# x 10
# base 10
[1] 1
7.1.1
log()
base e
> x <- 10
> log(x, 10)
# log(x, base=10)
69
> x <- 10
> log(base=2.718281828, x)
[1] 1
> x <- 10
###
> log(x)
###
7.1.2
order() *1
> order
*2 methods() getS3method()
# methods
> methods(mean)
[1] mean.data.frame mean.Date
[3] mean.default
mean.difftime
[5] mean.POSIXct
mean.POSIXlt
# default
# default method
# mean
{
...
()
7.1.3
{ } *3 { }
1
1 2
*4
>
x <- 2 ; y <- 50
z <- 10
log(x*y, base=z)
*1
R
*3
*4 C JAVA
*2
70
[1] 2
7.1.4
)
<- function( 1, , n ) {
<>
}
+
rm()
> myfunc(1)
> sin(1)
[1] 1
[1] 1
> myfunc(-2)
> rm(sin)
[1] -1
> sin(1)
#
#
[1] 0.841471
7.2
7.2.1
== = ( = )
ASCII
==
!=
>=
>
<=
<
>
is.nan()
is.null()
is.finite()
<
is.na()
is.infinite()
complete.cases()
NA
NaN
NULL
x == y , x != y x, y
identical()
all.equal()
# R 1 1.0
# as.integer(1) FALSE
[1] FALSE
> x <- 1;
y <- 1.0;
z <- 1.00000001;
> if (x == y)
[1] TRUE
71
7.2.2
&&
&
||
xor(x, y)
( )
> x <- 7
# 2 4
# 2 8 ...
7.2.3
*5 ( )
$ [[ ]] mylist$element , mylist[[1]]
[ ] x[2]
[ [[
, **
()
1
% %%% , %in%
/
< , >
7.3
7.3.1 if else
if else TRUE FALSE
*5
RjpWikiisac S version 4
72
x <- 2
if (x > 0) {
# if ( )
#
TRUE
x <- -x
FALSE
sum(1:x)
sum(1:x)
} else {
}
if else 34 } else
56 R if () { }
else if else
R if
else
> {
> if (a >= 0) {
if (a == 0) b <- "zero"
if (a == 0) b <- "zero"
else b <- a
else b <- a
+ }
+ else b <- -a
if (a >= 0) {
else b <- -a
+ }
if (a >= 0) {
if (a == 0) b <- "zero"
else b <- a
else b <- -a
return(c(a,b))
#
#
+ }
2
if if x < 2
0 if TRUE
> x <- -3
> if (x<-2) print(x)
# x<-2 x -2
[1] 2
# 0 TRUE
()
73
if else
> if (a < 0) {
+
print(0)
if (a < 0)
+ } else if (a < 1) {
else print(2)
print(1)
+ } else print(2)
print(0)
+ }
ifelse(, 1, 2)
NaN
NA
7.3.2 switch
1 2 3
switch switch
switch
1 ... n NULL
> a <- 1
> a <- 2
+ switch(a,
# switch(,
"1" = print("one"),
"2" = print("two"),
print("?")
+ )
+ switch(a,
# switch(,
"1"
print("one"),
"2"
print("two")
+ )
[1] "two"
[1] "one"
7.3.3 && ||
2 && () || ()
> a <- 2
> a <- 2
> b <- -3
> b <- -3
c <- a*b
c <- -a*b
> c
> c
[1] 6
[1] 6
74
7.4
7.4.1 for
for for ( in )
for
for *6
)
x 1 5
> x <- 0
> for (i in 1:5) {
# for ( in )
x <- x + 1
+ }
> x
[1] 5
+
for for
x i x 1 5
> x
> x
[1] 1 2 3 4 5 1 2 3 4 5
2
[1] 1 2 3 4 5 1 2 3 4 5
for
plot() plot()
> x <- 0
> par(mfrow=c(2,2))
> for (i in x) {
> x
[1] 10
+ }
plot(i, xlim=c(0,2*pi))
7.4.2 while
while
TRUE
x 1 5
*6
75
> x <- 0
> while (x <= 5) {
# while ( )
# TRUE
x <- x + 1
# FALSE 1
+ }
> x
[1] 6
7.4.3 break
break for while *7 next for while
*8
> x <- 0
> x <- 0
x <- x + 1
next
# x 1
if (x == 3) break
# x 3
x <- x + 1
# for
+ }
+ }
> x
> x
[1] 3
[1] 0
7.4.4 repeat
repeat break
> x <- 0
> repeat {
+
# break
else
# x > 5 repeat
break
+ }
> x
[1] 6
7.5
7.5.1
2 mydouble()
return( 2*x )
+ }
> mydouble(2)
[1] 4
*7
*8
break
next
76
7.5.2
R %+=% %=%
7.5.3
R
return() return()
return()
2 mydouble()
NA return()
return()
if (!is.numeric(x)) return(NA)
if (!is.numeric(x)) return(NA)
return(2*x)
2*x
+ }
+ }
return()
return()
*9
y <- 2*x
y <- 2*x
return(x, y)
return( list(x, y) )
+ }
+ }
> mydouble(2)
> mydouble(2)
$x
[[1]]
[1] 2
[1] 2
$y
[[2]]
[1] 4
[1] 4
...
*9
77
+ }
> result <- mymultiply(2)
# result(y) 2*x
> result(3)
[1] 6
7.5.4
invisible()
return( invisible(2*x) )
+ }
> mydouble(3)
> y
[1] 6
7.5.5
stop()
warning()
if (!is.numeric(x)) {
if (!is.numeric(x)) {
return(2*x)
+ }
return(2*x)
> mydouble("a")
+ }
> mydouble("a")
[1] NA
not numeric ! in: mydouble("a")
7.5.6
return(2*x)
# 2*x
+ }
> result <- lapply(1:3, function(x){ try(mydouble(x), TRUE) } )
78
> result
7.5.7 <<
< <
*10
> x <- 10
# x
# y
x <- y + 999
# x myfunc()
return(x)
+ }
> x
# x 10
[1] 10
> myfunc(1)
[1] 1000
# x
> x
[1] 10
<< <<
> x <- 10
# x
x <- 777
# myfunc() x
x <<- 99
# x 99
print(x)
# x
+ }
> myfunc()
[1] 777
# myfunc() ( x )
> x
# myfunc() x
[1] 99
# x 99
x
x
assign(, , env=.GlobalEnv)
*10
# z
# z
"z"
# z 999
# myfunc()
# z 999
R R RjpWiki
79
> x <- 10
> x <- 10
x <- c(1, 2)
x <- 1:3
x[2] <<- 0
assign(x[2], 2, env=.GlobalEnv)
print(x)
print(x)
+ }
+ }
> myfunc()
> myfunc()
[1] 1 2 3
> x
[1] 10
# ...
7.5.8
y <- 2
a*b
mydouble(x, y)
+ }
> myfunc(3)
[1] 6
7.5.9
formals() alist()
> f
function(x) 2*x
# body(f)
> formals(f)
# f
$x
> formals(f) <- alist(x=, y=3)
# x, y y 3
> f
# f
function (x, y = 3)
2*x
> formals(f) <- alist(x = , y = 3, ... = )
> f
function (x, y = 3, ...)
2*x
# ...
80
7.5.10
on.exit()
on.exit()
par(col="red")
on.exit(par(oldpar))
plot(x)
par(col="red")
par(oldpar)
plot(x)
+ }
+ }
> myfunc(1:10)
> myfunc(1:10)
on.exit() on.exit()
on.exit() add add=Tadd=F
on.exit(par(oldpar), add=T)
# par(oldpar)
on.exit(par(oldpar), add=F)
# par(oldpar)
on.exit()
7.6
7.6.1
if *11
myprod() n 0 1 ifelse()
if (n==0) 1
else
+ }
prod(1:n)
ifelse(n==0, 1, prod(1:n))
+ }
> myprod(0)
> myprod(0)
[1] 1
[1] 1
> myprod(5)
> myprod(5)
[1] 120
[1] 120
7.6.2
*11
76
81
y y 1
return(x*y)
return(x*y)
+ }
+ }
> mymultiply(2,5)
> mymultiply(2,5)
[1] 10
[1] 10
> mymultiply(2)
> mymultiply(2)
[1] 2
[1] 8
return(x*y)
+ }
> myfunc(2)
[1] 8
> myfunc()
myfunc() :
missing()
missing()
missing() TRUE *12 missing()
y missing()
warning()
if (missing(y)) return(x)
else return(x*y)
return(x*y)
+ }
+ }
> mymultiply(2,5)
> mymultiply(3,2)
[1] 10
[1] 6
> mymultiply(2)
> mymultiply(2)
[1] 2
[1] 2
y isnt inputted. in: mymultiply(2)
7.6.3
c() 2 10
... ...
... =
*12
82
return(args)
+ }
substitute(x)
+ }
+ }
> h(5)
[1] 2
[[1]]
> h(5,y=2)
[1]
2*y
[1] 4
[[2]]
> h(y=2)
[1] "title"
[1] 4
7.6.4
apply() R
*13
[,1] [,2]
[1,]
[1] 1.732051
[2,]
apply()
> mean(x)
[1] 15.16667
[1]
8 18 32 50 72
mean(x, a)
7.6.5
xl
xlab xlim
> plot(sin, xlab = "x-title")
# x
= "x-title")
# "xla" "xlab"
> plot(sin, xl
= "x-title")
# "xl" "xlab"
RjpWiki
83
do <- match.arg(do)
switch(do, sum
= a+b,
minus = a-b,
multi = a*b,
div
# do (match.arg )
#
= a/b)
+ }
> myfunc(3, 2, "minus")
[1] 1
# "mi" "minus"
7.7
Recall()
Recall()
if (n <= 1) return(1)
if (n <= 1) return(1)
else
else
return( n * myfactorial(n-1) )
return( n * Recall(n-1) )
+ }
+ }
> myfactorial(5)
> myfactorial(10)
[1] 120
[1] 3628800
7.8
cat() print()
myfunc() x y s
x <- rnorm(1);
return(s*z)
+ }
y <- rnorm(1)
# x y
# s
84
> myfunc(5)
x = 1.305908
y = -0.1477835
s = 0.1929916
[1] 0.9649579
R
browser()
nls()
wherereturn()
Q
debug()
browser()
undebug()
trace()
untrace()
traceback()
debugger()
try()
stopifnot()
system.time()
Sys.sleep()
browser()
x <- rnorm(1);
y <- rnorm(1)
browser()
return(s*z)
+ }
> myfunc(5)
Called from: myfunc(5)
Browse[1]> ls()
# x
[1] -0.3039924
Browse[1]> c
[1] 0.6327254
7.9
7.9.1
C x[1]x[2] R
assign() paste()
85
# Ai i
7.9.2 readline()
readline()
*14
Sys.sleep(time)
Sys.sleep(3)
cat("Hello.\n")
else
+ }
cat("Wrong...\n")
+ }
> mymessage()
> mytest()
Hello.
1+2? 3
Correct!
readline()
strsplit() unlist()
as.numeric() , myfunc()
as.numeric(unlist(strsplit(Str, ",")))
+ }
> x <- myfunc()
> y
[1]
Enter : 1,2,3,4,5
> x
[1] 1 2 3 4 5
7.9.3 menu()
menu() choices , title
0, 1, 2, ..0
switch
[1] 3
*14
RjpWiki
86
8.0
R 10 2
round()
> 0.4-0.2-0.2
[1] 0
> 0.4-0.3-0.1
[1] 2.775558e-17
> round( 0.4-0.3-0.1 )
# round()
[1] 0
8.1
R
round(x, digits = 0)
IEEE digits = 0
ceiling(x)
floor(x)
signif(x, digits = 6)
digits ( 6 )
trunc(x)
x 0
round() IEEE
*1
(1) 1
(2) 2
(3) 1
+
(2)
*1
RjpWiki ()
87
round(2.4)
round(2.5)
round(3.5)
round(2.51)
round(3.51)
round(3.46)
(1)
(2)
(2)
(1)
(1)
(1) (3)
8.2
beta(a, b)
lbeta(a, b)
log(beta(a,b))
gamma(x)
x n
n! gamma(n+1)
lgamma(x)
log(gamma(x))
digamma(x)
lgamma
trigamma(x)
lgamma
tetragamma(x)
lgamma
pentagamma(x)
lgamma
choose(n, k)
(n k )gamma(n+1) / ( gamma(k+1)gamma(n-
k+1) ) n, k
lchoose(n, k)
choose(n,k) log(choose(n,k))
(s) =
xs1 ex dx
1 (1) = 1
+
1
2 ( ) =
+
3 (s) = (s 1)(s 1)
+
4 (n) = (n 1)!
+
(s > 1)
(n : )
n! gamma(n+1) *2 factorial()
3!
> factorial(3)
[1] 6
*2
prod() 0! = 1
lgamma(n+1) lfactorial(n)
88
8.3
x nu
besselJ(x, nu)
besselY(x, nu)
gammaCody(x)
gamma() gamma()
8.4
x f (x) f (x) = 0
f (x)
O
x
x
R uniroot() 0 x 2 f (x) = x3 2 0 x
# (1) f(x)
# (2) c(0, 2)
$root
# $root
[1] 1.259934
()
x = 0.6931457
8.5
polyroot() p(x) = 5 + 4x + x2 c(5,
4, 1)
> polyroot(c(5, 4, 1))
# x^2 + 4x + 5 = 0
# x^2 + 2x + 1 = 0
round()
# x^2 + 2x + 1 = 0
89
8.6
R
# (1) f expression()
# (2) D(f, )
a * (4 * x^3)
DD
+ }
# D(, , )
deriv()
# (1) f = exp(-x^2) g
> g(2)
# (2) g(2)
[1] 0.01831564
f(2)
f(2)
attr(,"gradient")
x
[1,] -0.07326256
deriv() *3
x2 x f (x)
x2 y x y
g(x, y)
x2 yz x
y
h(x, y, z = 4)
*3
90
8.7
integrate()
# (1)
integrate(f, 0, 1)
# (2) integrate(, , )
# 0.3333333
R
> integrate(sin, 0, pi)
2 with absolute error < 2.2e-14
Inf Inf()
1.96
# dnorm:
> library(adapt)
> mynorm <- function(z) {
+
+ }
> adapt(2, lo = c(-4,-4), up = c(4,4), functn = mynorm)
value
relerr
minpts
lenwrk
ifail
0.993709 0.0004751543
263
73
8.8
optim() control fnscale
( fnscale=1)
10 3 p
# (0, 1)
$par
[1] 0.30 0.95
$value
[1] 0.2668279
# 0.3
......................
91
8.9
nlm() *4
10 3 p
# 0.5
$minimum
[1] -0.26683
$estimate
[1] 0.3
# 0.3
......................
8.10
lpSolve lp() f.conx1 + 2x2 + 3x3
8.11
fft() convolve(x,y) 2 *5
*4
*5
( )
mvfff()
IV
93
9.1
data.frame
1 2
*1
2
3
4
5
F
M
158
162
177
M
M
173
166
51
55
72
57
64
9.1.1
data.frame()
1 *2
*1
*
,
*2 x data.frame(x)
94
158
51
162
55
177
72
173
57
5
+
166
64
x
x x
$
# x
> x$HEIGHT
[1] 158 162 177 173 166
# x
# x 2
> x[,2]
[1] 158 162 177 173 166
9.1.2
summary()
1 3
> summary(x)
SEX
HEIGHT
:158.0
WEIGHT
F:2
Min.
M:3
1st Qu.:162.0
1st Qu.:55.0
Median :166.0
Median :57.0
Mean
Mean
:167.2
Min.
:51.0
:59.8
3rd Qu.:173.0
3rd Qu.:64.0
Max.
Max.
:177.0
:72.0
9.2
5 sexheightweight
sex
height
weight
158
51
162
55
177
72
173
57
166
64
9.2.0
22
95
9.2.1
x < read.table(C:/data.txt)
data01.txt
F
158
51
162
55
V2 V3
177
72
F 158 51
173
57
F 162 55
166
64
...........
(2) data02.txt 1
read.table() header T
+
)
data02.txt
158
51
162
55
158
51
177
72
162
55
173
57
166
64
...................
(3) data03.txt 1 2
header T skip 1
+
)
data03.txt
### data03.txt
F
158
51
158
51
162
55
162
55
177
72
177
72
173
57
166
64
(4) data04.txt
data04.txt
)
sep ,
sex,height,weight
Yt
F,158,51
> ( x <- read.table("data04.txt", header=T, sep=",") )
sex height weight
,
*
,
*
M,177,72
158
51
M,173,57
162
55
M,166,64
+
*3
F,162,55
...................
...................
rownames() colnames()
96
sep =
skip = 0
nrows = 1
header = F
F
comment.char
row.names=NULL
row.names=
row.names=
row.names=
read.table()
header
sep
quote
dec
read.csv()
read.csv2()
read.delim()
Yt
read.delim2()
Yt
read.fwf()
Yt
widths
data04.txt read.csv()
Windows
data
2
YY Y
9.2.2 EXCEL
csv
read.csv() data04.txt EXCEL
[] [] [] [
] [CSV ] Windows R
97
9.1
9.2 csv
Mac OS X R
9.3
9.4 csv
EXCEL read.csv()
read.csv() header F col.names
data05.csv
F,158,51
F,162,55
M,177,72
M,173,57
col.names=c("SEX","HEIGHT","WEIGHT")) )
SEX HEIGHT WEIGHT
158
51
162
55
...................
EXCEL
M,166,64
9.5 Windows
9.6 Mac OS X
98
(1) Windows *4
> x <- read.delim("clipboard")
"Enter $ 2
xls
gregmisc Perl*5 xls read.xls()
sheet
> library(gregmisc)
> x <- read.xls("data01.xls", sheet=1)
9.3
x *6
x$x[]x[[]]
SEX WEIGHT
x[2], x[[2]]
3 2
x[c(1, 2)]
1 2 x[ , c(1, 2)]
x[c(3, 4), ]
3 4
x[c(1,3,5,2,4), ]
c(1,3,5,2,4)
*4
R Editor
write.table(x, clipboard, sep=Yt) x EXCEL
*5 Windows ActivePerl
*6 [ ] names
99
x[ ,c(T,F,T)]
c(T,F,T) TRUE
x[x$SEX==F, ]
F 50kg
# 2 -> a[2]
> x[[2]]
[1] 158 162 177 173 166
# 1, 3, 4
> x[c(1,3,4),]
SEX HEIGHT WEIGHT
1
158
51
177
72
173
57
(2) NULL
> x[[2]] <- NULL
> x
SEX WEIGHT
1
51
55
72
57
64
9.4
x y
ncol(x)
nrow(x)
names(x)
rbind(x, y)
x y x y
cbind(x, y)
x y x y
data.frame(x, y)
x y x y
merge(x, y)
x y all=T
all=T
100
D1 D2
D1
ID
*
)
ID
A 158
A 51
>
C 177
B 55
E 166
D 57
D2
4 E 55
,
+
ID all
ID T
all.x all.y
ID
A 158 51
A 158 51
E 166 55
C 177 NA
E 166 55
NA 55
NA 57
(2) by 2 ID
2 by.x by.y *7
by by.x by.y merge(D1,
A 158 51
C 177 NA
3
4
by.y="Num", all=T) )
Num
A 51
A 158 51
B 55
E 166 55
C 177 NA
D 57
E 166 55
E 55
........
D3
NA 55
ID
........
(3) order()
(1) D W
*7
A 158 51
E 166 55
NA 55
NA 57
C 177 NA
by.x sort F
101
()
> rownames(D4) <- c(1:nrow(D4))
> D4
ID
A 158 51
E 166 55
...........
order(1 , pmax(1 , 2 ))
> D[sortlist,]
W
# W
A 158 51
# H
ID
A 158 51
E 166 55
NA 55
E 166 55
NA 57
C 177 NA
C 177 NA
# W
# W
# ID
NA 55
NA 57
9.5
head(x, n=a)
a tail(x, n=b) b
na.omit(x)
NA na.exclude()
transform(x, y=)
x y
x[sapply(x, )]
TRUE x[sapply(x,
is.numeric)]
subset(x, )
subset(x, , )
subset(x,
split(x, x$w==70)
x mean
aggregate()
reshape(x, ...)
DF
> id
<- c(
1,
2,
3,
4,
5)
# ID
DF
ID SEX
# (F:M:)
F 158 51
> h
<- c(158,162,177,173,166)
F 162 55
> w
M 177 72
> DF
M 173 57
5 5
+
M 166 64
102
(1) DF W
sum() gW 1000
> sum(DF$W)
# kg g
> DF
ID SEX
F 158 51000
F 162 55000
..................
DF G transform()
> transform(DF, G=DF$W * 1000 )
ID SEX
F 158 51 51000
F 162 55 55000
.....................
(2) ifelse(, TRUE , FALSE
)
# (NA )
> DF
ID SEX
F 158 NA
F 162 NA
M 177 72
...............
(3) [ ]
> cond <- (DF$H >= 170)
# H 170cm
> DF[cond,]
ID SEX
M 177 72
M 173 57
subset() sapply()
M 173 57
M 166 64
# ID > 3
103
(4) as.factor() *8
> DF$ID <- as.factor(DF$ID)
1 158 NA
2 162 NA
........
(5) ID SEX reshape()
> reshape(DF, timevar="SEX", idvar="ID", direction="wide")
ID H.F W.F H.M W.M
1
1 158
51
NA
NA
2 162
55
NA
NA
NA
NA 177
72
NA
NA 173
57
NA
NA 166
64
# H.FW.FH.MW.M
9.6
(1) edit() *9
> DF <- edit(DF)
9.7
Windows
9.8
Mac OS X
(2) 62
data.matrix()
str() is.data.frame()
*8
as.ordered(A$id) as.single(A$w)
data.entry(DF$ ID, DF$ SEX)
data.entry(..., Modes = NULL, Names = NULL) Modes Names
104
9.7
9.7.1
R data()
> data()
# base
# USArrests, VADeaths
> help(USArrests)
# USArrests
> library(nls)
# nls
> data(Puromycin)
# Puromycin
v <- rnorm(5);
w <- u*v
> w
[1]
0.166959
1.676896
1.435630 -3.024536
1.859002
attach() *10
> data(sleep)
# R (W.S.Gosset (Student) )
> attach(sleep)
# attach() extra
> extra
[1]
[13]
3.4
3.7
0.8
1.1
1.6
4.6
3.4
0.1 -0.1
4.4
5.5
0.0
2.0
1.9
0.8
detach() myframe$u
u attach
*10
attach(myframe)
105
detach() (on.exit(detach(sleep) )
)
> search()
> detach(sleep)
# sleep
> data(sleep)
# sleep F
> with(sleep, {
+
x <- extra[group==1];
y <- extra[group==2];
var.test(x, y)
+ })
F test to compare two variances
9.7.3 R
R ( SAS ) foreign
SPSS
> library(foreign)
> read.spss("datafile", use.value.labels=FALSE)
R foreign
lookup.xport()
SAS XPORT
read.dta()
read.epiinfo()
read.mtp()
read.spss()
read.ssd()
read.xport() SAS
read.xport()
write.dta()
106
9.9
9.10
"Next> $
Windows ActivePerl OS
2000/XP MSI 98/Me/NT 9.12
Windows Installer MSI
9.11
9.12
ActivePerl C:YperlYbinYperl.exe
read.xls() ActivePerl perl
9.7.4
NA NA
> sex <- c("F",NA,"M"); height <- c(158,162,NA); weight <- c(51,55,72)
> ( x <- data.frame(SEX=sex, HEIGHT=height, WEIGHT=weight) )
SEX HEIGHT WEIGHT
1
158
51
2 <NA>
162
55
NA
72
data06.txt
NA
107
data06.txt
sex,height,weight
158
51
F,158,51
162
55
F,162,55
NA
72
M,
173
57
M,173,57
166
64
M,166,64
+
9.7.5 scan()
,72
,
scan()
scan() *11
data07.txt 6 1
x ()
# data.txt
> x
62.6
25.0
[13] 178.0
[19]
23.0
0.0 163.0
75.4
28.0
33.0
1.0 158.0
49.8
158 49.8 25 0
42.0
1.0
163 68.7 42 1
1.0 163.0
55.7
178 75.4 33 1
68.7
0.0
163 55.7 28 0
+
scan()
*11
171 62.6 23 1
[1] 171.0
[7]
data07.txt
R read.table() scan()
scan() scan()
read.table() write.table() write()
108
= 0 ( = 1 ) sex
edit()
> x$h <- x$h/100
scan() read.table()
read.table()
sep =
scan()
sep =
skip = 0
nrows = 1
header = F
skip = 0
nlines
what
nmax = 1
comment.char
nmax what
quote
row.names,
col.names
quote = n = 1na.strings
= NA
109
scan()
scan() 2 2
data08.txt
1,2,3,4,5
> x
6,7,8,9,10
[1]
9 10
data09.txt
data09.txt
1,2,3,4,5,
> x
6,7,8,9,10
[1]
5 NA
9 10
9.7.6
*
)
data10.txt
1,2,3,4,5,
6,7,8,9,10,
,
+
fill = TRUE
strip.write = TRUE
10
110
10
10.1
read.table() write.table() write() *1 5 3
data07.txt C:/ data.txt read.table()
write.table(, ) output.txt
write.table(x,C:/output.txt) quote=F
> x <- read.table("C:/data11.txt")
> write.table(x, "C:/output.txt", quote=F,
col.names=F, append=T)
data11.txt
# append=F
write.table() write(
, )
# 4 * 2
# data12.txt
> scan("data12.txt")
# scan ()
Read 8 items
[1] 1 2 3 4 5 6 7 8
> y <- matrix(scan("data12.txt"), ncol=4)
Read 8 items
> y
[,1] [,2] [,3] [,4]
[1,]
[2,]
*1
10
111
save()
load()
# data13.dat
> load("data13.dat")
10.2
{
} write() 1
data14.txt
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9
+ }
> out <- cbind(out, x[length(x)])
> write(out, file="C:/data14.txt",
+
ncolumns = 2*length(x))
writeLines(paste( ), sep=,)
Yn
data15.txt
1, 2, 3, 4, 5
if (i < 5)
else
,
*
6, 7, 8, 9, 10
+ }
> close(out)
10.3 EXCEL
R EXCEL
(1) .csv.txt
(2) [] [] [
%
'
(3) & (
(.prn, .txt, .csv)]
10
112
> library(xtable)
> mytable <- xtable( matrix(1:24,4,6) )
> print(mytable)
% latex table generated in R 2.1.0 by xtable 1.2-4 package
% Sat May 28 14:47:23 2005
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrrrr}
\hline
& 1 & 2 & 3 & 4 & 5 & 6 \\
\hline
1 & 1.00 & 5.00 & 9.00 & 13.00 & 17.00 & 21.00 \\
2 & 2.00 & 6.00 & 10.00 & 14.00 & 18.00 & 22.00 \\
3 & 3.00 & 7.00 & 11.00 & 15.00 & 19.00 & 23.00 \\
4 & 4.00 & 8.00 & 12.00 & 16.00 & 20.00 & 24.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
html *2
*2
Hmisc latex()
11
114
11
plot()
11.1
R
R 2
()*1
11.1
11.2
X
3
*1
11
115
()
11.3
R
R R UNIX , Linux X11()
Windows windows()
R
R
R help(Devices)
bitmap()
bmp()
dev2bitmap()
jpeg()
JPEG
pdf()
ADOBE PDF
pictex()
LATEX
png()
PNG
postscript()
POSTSCRIPT
quartz()
(Mac OS )
windows()
(Windows )
win.graph()
(Windows )
win.metafile()
Windows
win.print()
(Windows )
x11()
(UNIX )
X11()
(UNIX )
xfig()
Xfig
dev.off() postscript()
dev.off()
# PS file
> data(cars)
pointsize=15)
# TeX ,
null device
1
() ( myplot.eps )
22
11
116
11.4
()
bmp(), jpeg(),
pdf()
()
dev.list()
dev.next()
dev.prev()
dev.set(which=k)
dev.off(k)
dev.copy(device, which=k)
k device postscript
dev.copy2eps(file=)
eps dev.copy()
dev.print(device,which=k)
k
( postscript)
graphics.off()
> plot(1:10)
> dev.copy(pdf, file="finename.pdf")
> dev.off()
>
# ...
> pdf()
# pdf ()
> plot(1:10)
# -> Rplots.pdf
> dev.off()
eps
# X11 ( OS )
> X11()
> plot(1:10)
> X11()
# X11 ( OS )
> plot(1:10)
# X11
> dev.print(file="finename.eps",
# EPS
11
117
Windows R
metafile bmp metafile (.emf)
postscript (.ps , .eps)
11.2 Windows R
11.5 plot()
R plot() plot()
x y
# plot(x , y , )
> plot(x, y)
# (xlim=c(1,10) )
10
8
6
y
4
2
10
xlim, ylim
10
10
# plot(, , )
0.3
0.1
0.2
gauss.density (x)
0.0
0.5
0.0
1.0
sin (x)
0.5
1.0
0.4
> plot(gauss.density,-3,3)
2
x
0
x
11
118
3 persp()
# x
> y <- x
# y
# 2
# 2
1/(2*pi*sqrt(1-rho^2))*exp(-(x^2-2*rho*x*y+y^2) / (2*(1-rho^2)))
+ }
> z <- outer(x,y,gauss3d)
# z
# 1
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
11.6 plot()
plot() type type 9
type=p
()
type=l
()
type=b
type=c
type=o
type=h
type=s
type=S
type=n
()
11
119
11.7
log=x
x ( x ) y ( y ) xy ()
()
xlim=c(0, 1)ylim=c(1, 1)
2 x y xlog,
axes=FALSE
11.8
main=Title
sub=SubTitle
xlab=X-Labelylab=Y-
x y x
Label
ann = F
( xlab = ylab =
)
tmag=1.2
11
120
11.9
par() add=T
() plot() add=T
> plot(sin, -pi, pi, xlab="", ylab="", lty=2)
# sin(x)
> par(new=T)
# cos(x)
11.10
y
0.0
1.0
0.2
0.5
0.4
0.0
0.6
0.5
0.8
1.0
1.0
0.0
11.3
0.2
0.4
0.6
0.8
1.0
11.4
11.11
frame() plot.new() new
TRUE
> frame()
> plot.new()
12
121
12
12.1
12.1.1 plot()
plot() plot()
plot(x) x x x
x
x 2 2
x 2 names()
x y
> plot(rnorm(10))
> x <- list(1:5, 3:7);
> plot(x)
plot(x, y) x y x y
> x <- rnorm(10);
y <- rnorm(10)
> plot(x,y)
plot(y x)
> x <- rnorm(10);
y <- rnorm(10)
> plot(y ~ x)
plot(f) f f
> f <- factor(c(rep("A",3), rep("B",5)))
> plot(f)
plot(f, y) f y f y
> x <- factor(c(rep("A",3), rep("B",5)));
> plot(f, y)
y <- rnorm(8)
12
122
plot(df) df
plot( expr) expr + ( a+b+c )
12.1.2 dotchart()
)
*
)
dotchart() *1
dotchart(, groups=,
12.1.3 stripchart()
)
stripchart()
stripchart( ~ ,
vertical = F, group.names=,...)
12.1.4 sunflowerplot()
+
+
,
labels=paste("sample", 1:10))
*
,
*
)
> stripchart(y ~ x)
+
,
sunflowerplot() 1
)
*
sunflowerplot(x , y , digits=6,
> sunflowerplot(y)
,
+
12.1.5 curve()
curve() n
12.1.6 matplot()
*
)
+
,
> curve(sin(x^2)*exp(-x^2),-pi,pi)
matplot()
matpoints() matlines()
*1
rug()
plot(density(faithful$eruptions))
rug(faithful$eruptions, side=1)
12
123
*
)
matplot( 2 ,
> sines
12.1.7
sample 9
1
sample 7
round.x..d...1..1
sample 1
sample 10
sample 3
sample 5
sample 8
2
sample 6
sample 4
1
sample 2
10
1.0
0.5
0.0
0.5
1.0
1.5
2.0
round.x..d...1.
12.1 dotchart()
12.2 stripchart()
12.3 sunflowerplot()
1.0
0.4
0.30
0.5
0.25
Density
0.2
0.0
sines
0.3
0.20
0.15
0.0
1.0
0.00
0.05
0.1
0.5
0.10
sin(x^2) * exp(x^2)
0.5
density(x = faithful$eruptions)
10
15
20
12.4 curve()
12.5 matplot()
12.6 rug()
12.2
12.2.1 hist()
hist()
() *2
# -3 -3 1
# ()
*2
12
124
12.2.2 barplot()
barplot()
*
)
barplot(, ...)
+
barplot(, ...)
> barplot(1:10)
barplot() barplot() x
lines
angle=45
beside=F
T F
col=rainbow(10)
density=30
horiz=F
legend = rownames(x)
names=
space=1
width=
12.2.3 pie()
)
*
)
pie()
+
,
pie()
col=rainbow(10))
*
,
angle=45
border=NULL
NA
col=rainbow(10)
density=30
main=Title
radius=0.8
11
12.2.4 boxplot()
)
*
)
boxplot()
boxplot(x ~ )
+
> boxplot(x)
> boxplot(len ~ dose, data = ToothGrowth)
,
+
,
12
125
boxplot()
boxwex = 0.8
horizontal=F
notch=F
notch
outwex = 0.5
outlier
range=1.5
range
range 1.5
Q3 + 1.5(Q3Q1) range
0
staplewex = 0.5
width=
12.2.5
40
10
20
5
0
3
12.7 hist()
12.9 barplot()2
35
12.8 barplot()1
30
25
3
7
-1
15
20
-2
10
10
-3
Frequency
60
15
80
10
Histogram of x
0.5
12.10 pie()
12.11 boxplot()1
12.12 boxplot()2
12
126
12.3
12.3.1 fourfoldplot()
)
)
*
fourfoldplot() 1 2 2 2
fourfoldplot(2*2 )
12.3.2 mosaicplot()
)
*
)
mosaicplot(, data=)
+
mosaicplot()
12.3.3 assocplot()
)
)
*
> assocplot(x)
,
+
assocplot()
12.3.4
Row: A
Titanic
2nd
Child Adult
Child
3rd
Adult
Child
Crew
Adult
Eye
Hazel
Green
Class
Row: B
12.13 fourfoldplot()
Blue
Sex
No
Female
Yes
Col: B
Col: A
Male
Yes
Brown
No
1st
Child Adult
Black
Brown
Red
Hair
12.14 mosaicplot()
12.15 assocplot()
Blond
12
127
12.4
12.4.1 stars()
stars()
12.4.2 symbols()
symbols()
y <- sort(10*runif(10));
z <- runif(10)
circles=
squares=
rectangles=
stars=3 0 1
thermometers=c() thermometers=c(
1 2)
boxplots=c( 1 2)
fg=
bg=
inches=F x inches=T
1 inches=
12.4.3 pairs()
pairs() pairs()
> pairs(iris[1:4])
>
> pairs(~ Fertility + Education + Catholic, data = swiss,
+
12
128
12.4.4 coplot()
coplot() x y z ( z )
z y x
coplot(y x | z) z z y x z
z y x
coplot(y x | z1 z2 ) 2 z1 z2 x y
> x <- 1:10;
y <- rnorm(10);
z <- x*y
> coplot(y ~ x | z)
12.4.5
Motor Trend Cars
Datsun 710
Hornet 4 Drive
Valiant
Hornet Sportabout
Mazda RX4
10
disp
Merc 240D
Merc 280
Merc 230
Merc 450SE
Merc 280C
Merc 450SL
Merc 450SLC
Lincoln Continental
Fiat 128
Cadillac Fleetwood
Chrysler Imperial
Honda Civic
Toyota Corolla
Dodge Challenger
Camaro Z28
Toyota Corona
AMC Javelin
mpg
hp
Duster 360
Fiat X19
Porsche 9142
Lotus Europa
Ford Pantera L
cyl
disp
drat
wt
Maserati Bora
Volvo 142E
2.0
3.0
4.0
mpg
1.5
10
Given : z
4
15
6.5
90
7.5
80
5.5
60
4.0
70
4.5
Fertility
2
10
10
0.0
0.4
10
Education
2.0
15
0.4
Sepal.Width
3.0
10
12.18 symbols()
Education < 20
2.5
wt
Sepal.Length
6
x
qsec
12.17 stars()2
0.5
hp
drat
12.16 stars()1
Ferrari Dino
Pontiac Firebird
y
0.4
100
80
0.8
0.4
60
0
0.5
20
40
Catholic
Petal.Width
0.0
3
2
1
2.5
1.5
0.8
Petal.Length
2
4.5
5.5
6.5
7.5
60
70
80
90
20
40
60
80
10
100
12.19 pairs()1
12.20 pairs()2
12.21 coplot()
12
129
12.5 3
12.5.1 image()
image() 3
y <- 10*(1:ncol(volcano))
12.5.2 persp()
persp() 3
y <- x
12.5.3 contour()
contour() 3 filled.contour()
> x
<- -6:16
12.5.4 scatterplot3d()
scatterplot3d 3 scatterplot3d()
scatterplot3d
> library(scatterplot3d)
> z <- seq(-10, 10, 0.01);
x <- cos(z);
y <- sin(z)
>
> temp <- seq(-pi, 0, length = 50)
> x <- c(rep(1, 50) %*% t(cos(temp)))
> y <- c(cos(temp) %*% t(sin(temp)))
> z <- c(sin(temp) %*% t(sin(temp)))
> scatterplot3d(x, y, z, highlight.3d=TRUE, col.axis="blue",
+
12.5.5 lattice
lattice S Trellis R
cloud() wireframe()
12
130
> library(lattice)
> data(volcano)
> wireframe(volcano, shade = TRUE, aspect = c(61/87, 0.4),
+
light.source = c(10,0,10))
lattice
barchart()
bwplot()
densityplot()
dotplot()
histogram()
qqmath()
stripplot()
rfs()
qq()
2 q-q
xyplot()
trellis plot()
levelplot()
contourplot()
cloud()
()
wireframe()
()
splom()
parallel()
12.5.6
0.4
0.6
0.8
1.0
0.0
0.2
x
cos(r2)er
0.0
0.2
0.4
0.6
0.8
1.0
12.23 persp()
Plot1
Plot2
12.24 contour()
0.8
10
1.0
12.22 image()
1.0
0.5
0.2
0.5
0.0
0.0
1.0
0.5
0.0
0.5
1.0
12.25 scatterplot3d()1
column
row
0.5
0.0
10
0.5
1.0
z
0.4
0.6
volcano
1.0
1.0
1.0
0.5
0.0
0.5
1.0
12.26 scatterplot3d()2
12.27 latticewireframe()
12.5.7 grid
grid R
graphics graphics
lattice
*3
*3
13
131
13
13.1
x y (
points() type p )
pch
points(approx(x, y))
x y
( lines()
type l )
a b y = a + bx
abline(h = y)
abline(v = x)
abline(result)
result lm()
grid(a, b)
text(x, y, labels)
(xy)
text srt = ()
x 45
text side
at = NA)
(1234)
line at
13
132
box()
title(main, sub)
main sub
tmag
axis(side=4, labels=F)
axis(side=1, pos=0)
pos (side=3)
legend(x, y, legend)
(xy) legend
ncol 2
legend(locator(1), legend=)
locator()
()
pch=v)
polygon(x,y)
(xy) x y
NA
density /
angle=c(45, 45))
angle
()
13.2
( 1) axis() box() legend()
polygon()
# X
# Y
> box()
> legend(2, 3, paste("sin(",6:9,"x)"), col=6:9,
+
# (5,9)
# (5,-1) (4,2)
13
133
( 2) polygon() *1
4 x 4 2 x 4
# x 10 ()
> polygon(c(xvals,rev(xvals)),
+ c(rep(0,10),rev(dvals)),col="gray") #
> title("Title")
(1) (2)
sin( 6 x)
sin( 7 x)
0.4
Title
sin( 8 x)
sin( 9 x)
0.3
subtitle
0.2
0
3
dnorm (x)
0.1
0.0
13.1
(1)
13.2
(2)
13.3
text() mtext() axis() title()
expression()
> ?plotmath
> demo(plotmath)
> example(plotmath)
*1
RjpWiki
13
134
13
135
13
136
13
137
13
138
13.4 eps
R eps ADBE
13.4.1 Tgif
Tgif eps pstoedit http : //www.pstoedit.net/pstoedit eps obj
Windows Tgif
13
139
(1) Ghost Script (7.04 ?) GSView http : //f orum.nif ty.com/f dtp/install/win/gs800.htm
14
140
14
14.1
par()
2
2
par() *1
col
> par(col="red")
14.2
par()
par()
par()
par(, )
par(c(, ))
par(col=2)
par(col=2, lty=3)
par(list(col=2, lty=3))
*1
par(cin)
par(cra)par(csi)par(cxy)par(din)
par(cin)/par(pin)
14
141
14.2.1 par()
par() *2
> par(new=T)
# new TRUE
> par(ask=TRUE)
> help(par)
14.2.2
par()
R 1
> par(oldpar)
par()
on.exit(par(oldpar))
par(col=2);
plot(1:10)
+ }
14.2.3
par() plot()
14.1
*2
14.2
14
142
*3
14.2.4
xpd = F FALSE TRUE
NA
> library(rpart)
> result <- rpart(Species ~ ., data=iris)
# par(xpd=F) par(xpd=NA)
> plot(result)
> text(result)
Petal.Length<
2.45
|
Petal.Length< 2.45
|
tosa
Petal.Width< 1.75
setosa
versicolor
Petal.Width< 1.75
virginic
versicolor
14.3 par(xpd=F)
virginica
14.4 par(xpd=NA)
pin = c(6,6)
x y c(0, 1, 0, 1)
ps = 12
pty = m
s m m
*3
ps, eps
14
143
par(usr)
> par("usr")
fig = c(0, 1, 0, 1)
x y c(0, 1, 0, 1)
c(0, 0.5, 0.5, 1)
par(new=T)
S
fin = c(5,5)
par(fin) [6.968749
6.958332] c(5, 5)
par(new=T) S
14.2.7
mar
pltpin
mex = 1
0.5
mar = c(5, 4, 4, 2)
mex
3
4 ( 4 mex ) mai
pltpin
(outer margins
in inches)
oma = c(2, 0, 3, 0)
outer marginmex
omd = c(0, 1, 0, 1)
0 1
(x1 , x2 , y1 , y2 )
y mar
> par(mar=c(5,4,5,4))
> plot(1:10)
> axis(side=4)
14
144
14.3
par() plot()
pch 2 par()
()
# "+"
> par(pch="+")
# "+"
> plot(1:10)
# "+"
> plot(1:20)
# "+"
14.3.1
col col.axis, col.lab, col.main, col.sub
(color) col = ( 1 )
1
col = rgb(1, 0, 0) col = rgb() 16
col = #FFFFFF
col = rainbow(10)
cm.colors(n) col
col = hsv(.5, .5, .5),
hsv
col = gray(0.8)
gamma = 1.0
14.3.2
pch = pch = 0 25
+
pch
pch
pch
pch
pch
pch
pch
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
14.3.3
14
145
adj = 0
( 00.51) adj =
c(x,y) x y
cex = 1
1 csi
cex.axis, cex.lab,
cex.main, cex.sub
ps = 20
text(x, y, labels=
(x, y) labels ()
, srt=90)
( x ) crt ()
col = 1
font = 1
( 1234
5 (Adobe symbol encoding) ) font.axis, font.lab,
font.main, font.sub
family=
serifsansmonosymbol
14.3.4
*4
lwd = 1
(line width)
lty=0 lty=blank
(line type)
lty=1 lty=solid
(line type)
lty=2 lty=dashed
(line type)
lty=3 lty=dotted
(line type)
lty=4 lty=dotdash
(line type)
lty=5 lty=longdash
(line type)
lty=6 lty=twodash
(line type)
type = p
()
type = l
()
type = b
type = c
type = o
type = h
type = s
type = S
type = n
*4
lend0round1butt2squarelheightljoin0round1mitre
2bevellmitre
14
146
14.3.5
bg = blue
fg = blue
bty = o
bty = l
bty = 7
bty = c
bty = u
bty =
bty = n
()
14.3.6
ann = F
FALSE
las = 0
las = 1
las = 2
las = 3
lab = c(5,5,7)
3 x y
( R 2.1.1 )
mgp = c(3,1,0)
3
(mex )
tck = NA
tck = 1
tcl = 0.5
xaxt = s yaxt = s
NA tcl = 0.5
(mex)
tcl = NA tck
0.01
t
xaxt = n yaxt = n
xaxt = n yaxt = n
axes = F
xaxs = r yaxs = r
x y 4 %
xaxs = i yaxs = i
x y
par(xaxp) par(yaxp)
xy
xlog = F ylog = F
x y TRUE
FALSE
15
147
15
R
R
15.1 locator()
locator() locator()
Windows R
> locator()
> locator(10)
# 10 ( 512 )
locator() x y
# z x y
15.2 identify()
identify()
locator()
y <- rnorm(20)
# y 20
> plot(x, y)
# (x, y)
> identify(x, y, x)
# x
> identify(x, y, y)
# y
> identify(x, y)
[1]
8 11 16 20
identify() 3
148
15
16
11
20
identify() x y 8 11 16
20
> pt <- identify(x,y,y) # identify(x,y) identify(x,y,x)
# pt
> pt
[1]
8 11 16 20
( 1) plot=F ()
( 2) 0.25
15.3
15.3.1
15
149
par()
m n mfcol mfrow
1 mfrow=c(1,1)
mfcol=c(1,1)
mfg = c(i,j,m,n)
m n
i j
fig = c(4,9,1,4)/10
22
> par(mfrow=c(2,2))
1
> plot(sin)
> plot(cos)
> plot(asin)
> plot(acos)
outer = T mtext
0.4
0.6
0.8
1.0
0.0
0.2
0.4
0.6
0.8
1.0
0.6
0.8
1.0
0.9
0.2
0.4
0.6
0.8
1.0
0.0
0.4
0.6
0.8
1.0
0.6
0.8
1.0
0.2
0.4
0.6
0.8
1.0
0.0
0.2
0.4
15.1
1.0
acos (x)
0.5
0.0
0.5
0.0
0.0
0.0
1.0
asin (x)
1.5
1.0
acos (x)
0.5
1.5
1.0
0.5
0.0
asin (x)
0.2
1.5
1.5
0.2
0.8
cos (x)
0.6
0.0
0.0
0.7
0.6
0.4
sin (x)
0.2
0.0
cos (x)
0.4
0.0
0.2
sin (x)
0.6
0.8
0.8
1.0
Title
0.0
0.2
0.4
0.6
0.8
1.0
0.0
0.2
0.4
15.2
( 1) frame() 1
( 2) stars pairs
15
150
15.3.2
mfrow mfcol
layout()
layout() mat
# ()
[1,]
# ( 1 )
[2,]
# ( --------- 2 --------- )
> layout(mat)
()
> plot(sin)
0.4
0.0
0.2
sin (x)
0.6
0.8
> plot(cos)
0.0
0.2
0.4
0.6
0.8
1.0
0.8
0.7
0.6
cos (x)
0.9
1.0
0.0
0.2
0.4
0.6
0.8
1.0
layout.show(n) lcm(x)
> layout(matrix(c(1,3,2,2), 2, 2, byrow = TRUE), respect=T, widths=lcm(5), heights=lcm(5))
> plot(sin)
> plot(cos)
> plot(acos)
split.screen()
split.screen() 2 c( m, n ) *1
2
> split.screen(c(2,1))
[1] 1 2
1 2 screen
2 3
*1
15
151
# 1
# 3
# 4
# 5
()
frame() erase.screen()
1
close.screen(all=T)
15.3.3
add=T 2 x y
( 1) par(new=T) 2 plot()
1 axes=F , xlab=ylab=
ann=T
( 2) par(new=T)
xlim = c(x0, x1)ylim = c(y0,
y1)
15
152
x <- rnorm(100)
# 100
par(new=T)
axis() 2 plot(density(x)...) y
> axis(side=4)
0.5
0.4
0.3
0.4
0.3
4
0
x
15.3 axis()
0.2
0.1
0.0
0.0
0.1
0.2
Density
0.0
0.1
0.2
Density
0.3
0.4
0.5
Histogram of x
0.5
Histogram of x
15.4 axis()
() axis() y mar
VI
16
154
16
16.1
10
2 xy *1
ave(x)
fivenum(x)
IQR(x)
max(x)
mean(x)
median(x)
min(x)
()
quantile(x)
range(x)
sd(x)
sum(x)
var(x,y)
weighted.mean(x)
summary()
# x
> quantile(x)
0%
25%
50%
75%
100%
-1.600 -0.175
0.350
1.700
3.700
# y
> summary(y)
Min. 1st Qu.
-0.100
Median
0.875
1.750
4.150
Max.
5.500
16.1.1
pie(result) barplot(result)
# result
(-2,0]
(0,2]
(2,4]
*1
*2
170
5 fivenum() 4 IQR() 3 1
quantile() 1 3 range()
sum((x-mean(x))2)mean((x-mean(x))3)/(sd(x)3)mean((x-mean(x))4)/(sd(x)4)
0 3 e1071
skewness()kurtosis()
16
155
16.1.2
var()
x n E() var(x)
var(x) =
%
n $
E(x2 ) {E(x)}2
n1
var() (n 1)/n
> var(x)
[1] 3.200556
> variance <- function(x) var(x)*(length(x)-1)/length(x)
> variance(x)
[1] 2.8805
#
> sd(x)
[1] 1.789010
> sqrt(variance(x))
[1] 1.697204
16.1.3
2 var() 1
2 var()
var() (n 1)/n
cor() 1 2
> var(x, y)
[1] 2.848333
> cor(x, y)
[1] 0.7951702
if (det(tmpcor) != 0)
else { library(MASS);
d <- diag(sol)
return(sol)
+ }
> sampledata <- matrix(c(1:9), ncol=3, byrow=T)
> my.cor(sampledata)
16
156
-1
-1
-1
Var 2
-1
-1
-1
Var 3
-1
-1
-1
16.1.4
x 0 1
scale(x) x
0 ()
1 () 2 *3
> scale(x)
[,1]
[1,] -0.02794842
[2,] -1.31357592
................
[10,]
0.69871059
attr(,"scaled:center")
[1] 0.75
attr(,"scaled:scale")
[1] 1.789010
16.2
R f (x) P (X x) min{x : P (X x) > q}
16.2.1
xxx qxxx t pt
qt
(xxx )
(pdf)
dxxx(q)
q norm
dnorm(q)
(cdf)
pxxx(q)
q norm
pnorm(q)
(quantile)
qxxx(p)
p norm qnorm(p)
rxxx(n)
n norm
rnorm(n)
*3
16
157
16.3 R
R qtukey () ptukey (
) dmultinom rmultinom
pbirthday qbirthday*4
beta
binom
birthday
cauchy
chisq
exp
gamma
shape, scale
geom
prob
hyper
n, m, k
lnorm
meanlog, sdlog
logis
location, scale
multinom
n, size, prob
nbinom
size, prob
norm
mean, sd
pois
lambda
signrank
m, n
t
unif
df, ncp
min, max
tukey
nmeans, df
weibull
shape, scale
wilcox
m, n
# x : , prob :
# q : quantile
# p :
# n :
size :
*4
158
0.0
0.1
0.2
0.3
16
-4
-2.78
2.78
( 3) norm f(x)
# ()
> dnorm(0)
[1] 0.3989423
> rnorm(5)
0.0
0.15
0.00
0.05
0.10
0.2
0.1
dnorm (x)
0.3
0.20
0.4
0.25
16.2
10
0:10
16.3
> qf(0.95, 2, 3)
[1] 9.552094
# qf(0.05, 2, 3, lower=F)
16
159
16.3.1 set.seed()
R ( R
) set.seed()
# 5
> runif(5)
> runif(5)
# (seed)
16.3.2 2
r 2 r2norm()
<- mu+sigma*tmp
return(data.frame(x=x,y=y))
+ }
> mydata <- r2norm(100, 0, 1, 0.5)
> cor(mydata$x,mydata$y)
[1] 0.5756098
16.3.3 3
3
3 r3norm()
U <- svd(A)$u;
V <- svd(A)$v;
D <- diag(sqrt(svd(A)$d))
w <- c()
return(w)
# A
+ }
> mu <- cbind(c(1,1,1))
> A <- array(c(2,1,1,1,2,1,1,1,2), dim=c(3,3))
> w <- r3norm(mu, A, 2000)
# ()
16
160
3 t
p N(0,Ip )
V = C%*%t(C)
X = sqrt(m/R)*Z p t met(p,m,0,Ip )
Y = +C%*%X p t met(p,m,,V)
3 t met3()
# m
<- svd(V)$u;
w <- c()
for (i in 1:n) {
V1 <- svd(V)$v;
<- diag(sqrt(svd(V)$d))
R <- 0
return(w)
+ }
m/(m 2) V m > 2) t
> n
<- 1000
> w
> sm <- 0
> for (i in 1:n) { sm <- sm + w[[i]] }
> sm <- sm/n
> sv <- 0
> for (i in 1:n) { sv <- sv + (w[[i]]-sm) %*% t(w[[i]]-sm) }
> sv <- sv/n
16.3.4
U
X = F 1 (U )
P (X x) = P (F 1 (U ) x) = P (U F (x)) = F (x)
X F
16
161
1 ( x > 0)
u <- log(runif(n))
return(u*v)
+ }
> x <- rlaplace(1000)
16.3.5
()
f (x)
g(x)
c( 1) f (x) c g(x) ()
g(x) = 1/
(0 < x < )
(0 < x < )
c = /2
y <- c();
while (i <= x) {
u <- runif(1);
if (u < w){
v <- pi*runif(1);
+
+
i <- 1
}
}
i <- i+1
w <- sin(v)
16
162
return(y)
+ }
> mydata <- myrand(1000)
16.3.6 2 x 2
2 x 2 r2dtable()
n
r
c (cr )
r,c 22 n
[2,]
[[2]]
[,1] [,2]
[1,]
[2,]
16.4
n m sample()
replace=FALSE
m <- 5
# n
[1]
5 18 20 18
5
# m = n
> sample(x)
[1]
6 11
1 14
7 15 20 16 12
5 18 17 13 10
8 19
# 100
[1] 1 1 1 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 0 1 0 0 1 1 1 1 0 1 0 0 1 1 0 1 0 0 0
[38] 0 0 1 0 0 0 0 1 1 0 0 1 1 1 0 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 0 1 1 0 0 1
[75] 1 1 0 0 1 0 0 1 1 1 0 0 1 1 1 1 0 1 1 0 0 0 0 0 1 1
( 1/n )
p <- p/sum(p)
4 16 12 20
17
163
17
17.1
factor() table()
*1
> data(warpbreaks)
# 1
> attach(warpbreaks)
> levels(fc)
> table(fc)
fc
L
18 18 18
cut()
L M H
(10,20] 3 6 8
(20,30] 7 7 7
.............
(60,70] 2 0 0
tapply() tapply()
> # tapply()
> ( mymeans <- tapply(breaks, fc, mean) )
L
# breaks
16.446487
9.121009
8.352527
*1
xtabs()
17
164
> stem(breaks)
The decimal point is 1 digit(s) to the right of the |
1 | 0234555667788899
2 | 001111445666678889999
3 | 00156699
4 | 1234
5 | 124
6 | 7
7 | 0
> summary(breaks)
Min. 1st Qu.
10.00
Median
18.25
26.00
> fivenum(breaks)
34.00
Max.
70.00
[1] 10 18 26 35 70
1 2
17.2
1 1 2 1
f (x) = 0.6(x) + 0.4(x)
generator()
0.6/sqrt(2*pi)*exp(-(x+1)^2/2) + 0.4/sqrt(2*pi)*exp(-(x-2)^2/2)
+ }
> curve(truedensity, xlim=c(-6,6), ylim=c(0,0.3), col=2)
f (x)
# (x)
# (x)
# 0.6 data1
+ }
hist() R probability=T
17
165
100
Frequency
0.15
0.00
0.05
50
0.10
truedensity (x)
0.20
150
0.25
200
0.30
Histogram of x
17.1
17.2
17.3
hist()
log2 n + 1 ( n )
Sturges (1926!!) (
)
MASS truehist() ( Scott (1992) )
KernSmooth dpih() ( Wand (1995) )
> library(MASS)
> x <- generator(1000)
# hist(x, breaks="Scott")
> truehist(x)
> library(KernSmooth)
KernSmooth 2.22 installed
Copyright M. P. Wand 1997
> x <- generator(1000)
> h <- dpih(x)
# bin x
# bin
60
0
0.00
20
0.05
40
0.10
Frequency
0.15
80
0.20
100
Histogram of x
2
x
2
x
17
166
( 50 )
> library(KernSmooth)
> x <- generator(100)
> h <- dpih(x)
> bins <- seq(min(x)-2*h, max(x)+h, by=h)
#
20
15
Frequency
10
15
10
0
Frequency
20
25
Histogram of x
25
Histogram of x
17.4
density()
# plot
> par(new=T)
> curve(truedensity, xlim=c(-6,6), ylim=c(0,0.3), col=2)
x
> plot(density(x))
> rug(x)
> rug(jitter(x, amount = .1), side = 3, col = "light blue")
density(x = x)
Density
0.10
0.15
0.00
0.00
0.05
0.05
0.10
truedensity
Density (x)
0.20
0.15
0.25
0.20
0.30
density(x = data)
N = 1000 Bandwidth
x
= 0.3965
density() bw
17
167
bw
nrd0
()
1/5 1.34 0.9 (=
Silverman (1986) ) 0
bw > 0 (Silverman, B. W. (1986) Density Estimation.
ucv
bcv
SJ-ste
bw
adjust
bw adjust*bw
kernel window
gaussian rect-
width
S bw width
bw bw width
give.Rkern
width
n
from to
from to n1
cut
na.rm
TRUE x FALSE
*2
>
>
>
>
library(KernSmooth)
data <- generator(1000); h <- dpik(data)
est <- bkde(data, bandwidth=h)
plot(est, type="l", xlim=c(-6,6), ylim=c(0,0.3))
17
168
0.15
0.00
0.05
0.10
truedensity (x)
0.20
0.25
0.30
17.5
hist2d() (gregmisc)
bkde2D() (KernSmooth) kde2d() (MASS)
>
library(gregmisc)
# 2
>
# (persp)
# hist2d()
>
>
>
>
library(MASS)
# width.SJ() MASS
>
library(KernSmooth)
# bkde2D() KernSmooth
>
>
>
>
17
169
60
unts
h2d$co
40
20
0
10
d$
x
5
5
$y
h2
10
h2d
5
10
f1$fha
10
17.6 lowess()
lowess() lowess() x y
lines()
x y
iter
iter lowess
delta
delta x lowess
> data(cars)
> plot(cars, main = "lowess(cars)")
> lines(lowess(cars), col = 2)
> lines(lowess(cars, f = 0.2), col = 3)
> legend(5, 120, c(paste("f = ", c("2/3", ".2"))), lty = 1, col = 2:3)
120
lowess(cars)
dist
20
40
60
80
100
f = 2/3
f = .2
10
15
20
25
speed
*3
Nadaraya-Watson ksmooth() 3
smooth.spline()Friedman SuperSmoother supsmu() runmed()
smooth() smoothEnds()
18
170
18
18.1
R
> ()
(x)
1 1 x
(x, y)
2 2 x, y
(y x)
x y
(A)
2 2 A
10 2 2
sleep
group1
0.7
group2
1.9
1.6
0.8
0.2
1.1
1.2
0.1
0.1
0.1
3.4
3.7
0.8
0.0
2.0
4.4
5.5
1.6
4.6
3.4
# 1
# 2
t t.test() sleep
18.1.1 (1) t
t.test() 1 t
95%
18
171
group1
2.0297804
sample estimates:
mean of x
0.75
mu 0
data
t = 1.3257
df = 9
p-value = 0.2176
p 0.05
alternative hypothesis
sample estimates
0.75
group1
t = -113.5692, df = 9, p-value = 1
alternative hypothesis: true mean is greater than 65
95 percent confidence interval:
-0.2870553
Inf
sample estimates:
mean of x
0.75
18.1.2 (2) t
1 2
t.test() t
18
172
alternative=two.sided
alternative
alternative=greater
()
alternative=less
()
mu
paired
TRUE t
var.equal
TRUE 2
conf.level
t.test() *1
0.2038740
sample estimates:
mean of x mean of y
0.75
2.33
statistic
parameters
p.value
p 5% 0.05
95 percent confidence
95%
interval
estimate
null.value
alternative
method
data.name
*1
var.equal=T var.equal=F
18
173
18.2
18.2.1
stepfum ecdf() (cdf)
stepfun
> data <- rnorm(30)
# ecdf()
> Fn
> summary(Fn)
# Fn
> summary.stepfun(Fn)
# Fn
> plot(Fn)
0.8
0.0
0.2
0.4
Fn(x)
0.6
0.8
0.6
Fn(x)
0.4
0.2
0.0
0.0
0.2
0.4
Fn(x)
0.6
0.8
1.0
ecdf(data)
1.0
ecdf(data)
1.0
ecdf(data)
Q-Q
qqnorm(x)
x
qqnorm()
qqline(x)
qqplot(x, y)
x y ( Quantile-Quantile PlotQ-Q )
ppoints() QQ qlnorm() *2
> plot(qlnorm(ppoints(x)), sort(x))
*2
qlnorm()
174
2
0
2
sort(x)
18
qt(ppoints(50), df = 5)
10
qlnorm(ppoints(x))
18.2.2
shapiro.test() Shapiro-Wilk
> data(faithful)
> long <- faithful$eruptions[faithful$eruptions > 3]
#
> long
> shapiro.test(long)
Shapiro-Wilk normality test
data:
long
> data(faithful)
> ( long <- faithful$eruptions[faithful$eruptions > 3] )
long
18.2.3
170
18.2.4
9 x y
0.50,
1.62,
visit
> y <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29) # second visit
18
175
t
t
Paired t-test
data:
x and y
Inf
sample estimates:
mean of the differences
0.4318889
x and y
18.3
170 sleep 1 2
t
t.test()
0.2054832
sample estimates:
mean of x mean of y
0.75
2.33
18
176
t
t *3 170 (y x)
2 3
*4
80
73
80
82
73
68
81
85
85
93
88
74
t 2
18
177
A B C 3
5%
bartlett.test()
> length <- c(80, 73, 80, 82, 74, 73, 68, 81, 85, 85, 93, 88)
> group
18.4
18.4.1 2 ()
chisq.test()
(16 ) 1
ABOAB 40%30%20%10%
m (m2)
18
178
18.4.2
2
14
22
17
21
18
25
43
p-value = 0.005089
alternative hypothesis: true odds ratio is not equal to 1
95 percent confidence interval:
1.567890 39.549785
sample estimates:
odds ratio
7.051895
( 2 2 )
2 2 2 2 ()
() 2 () 2
mcnemar.test() 2 2 correct=F
18
179
14
22
17
21
18
25
43
() 2 () 2
correct=F *5
> prop.test(c(245, 157), c(300,250), correct=F)
2-sample test for equality of proportions without continuity
data:
correction
prop 2
0.8166667 0.6280000
10
1 5 5 1
*5
vcd summary(oddsratio(table(A,B),log=F))
log=F
prop.trend.test() prop.trend.test
( 1, 2)
18
180
(5 1)
1 *6
> x <- c(4, 1, 1, 2, 1, 2, 4, 3, 3, 2)
> y <- c(2, 1, 3, 1, 5, 1, 5, 3, 3, 1)
> binom.test(c(length(x[x>y]), length(x[x < y])))
Exact binomial test
data:
A B A B
()
A
80
15
10
40
20
10
25
() vcd Kappa() *7
> measure2 <- matrix(c(80, 15,
+
5,
*6
prop.test(
, , p = 0.5) binom.test()
*7
# binom.test(, , )
> data(anxiety)
> icc(anxiety, model="twoway", type="agreement")
18
181
> Kappa(measure2)
value
ASE
lwr
upr
Weighted
18.4.3
2 x y cor.test()
> x <- c(70, 72, 62, 64, 71, 76, 60, 65, 74, 72)
> y <- c(70, 74, 65, 68, 72, 74, 61, 66, 76, 75)
method
pearson
kendall
spearman
x and y
0.9496011
18.4.4
A B C 3
80
73
80
82
73
68
81
85
85
93
88
74
> group
<- c( 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3)
18
182
gplots plotmeans()
*8
> library(gplots)
65
70
75
80
85
90
95
100
n=5
n=4
n=3
18.1
gplots plotCI()
> tmp
> ciw
# length length()
oneway.test()
1 2 interaction.plot()
> attach(ToothGrowth)
> interaction.plot(dose, supp, len, fixed=TRUE)
18
183
kruskal.test() *9
length by group
pairwise.t.test() 5%
holm bonferroni *10
A B C 3
10
20
30
40
180
193
200
201
150
189
186
190
144
178
166
173
5%
150,189,186,190,
*9
>
>
>
>
*10
pairwise.wilcox.test()
TukeyHSD() multcomp simtest(x b, type=Williams)
18
184
> friedman.test(x)
Friedman rank sum test
data:
fligner.test()Fligner-Killeen
mantelhaen.test()Cochran-Mantelansari.test()Ansari-Bradley
18.5
R
power.t.test()
power.anova.test()
power.prop.test()
t power
sig.level type=c(two.sample, one.sample, paired) ,
alternative=c(two.sided, one.sided)
19
185
19
19.1
lm() predict() trees
Girth
Height (
Volume 1
> plot(Volume ~ Girth, data = trees)
> abline(result)
> summary(result)
Call:
lm(formula = Volume ~ Girth, data = trees)
Residuals:
Min
1Q
Median
3Q
Max
-8.0654 -3.1067
0.1520
3.4948
9.5868
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -36.9435
3.3651
Girth
0.2474
5.0659
--Signif. codes:
36.9435 5.0659
186
60
40
cbind(result.pre, result.con)
50
y
40
30
10
20
20
60
80
70
19
65
70
75
80
85
10
15
19.1
20
new$Girth
19.2
19.2 lsfit()
lsfit() y = X b + e b y X
() cbind
lsfit()
wt
intercept
TRUE
FALSE
trees
y <- trees$Volume
# z
> print(z)
coef
intercept=T () 1
residuals
intercept
intercept
qr
x QR qt
t(Q)% *% y ( t(Q) Q ) qt qr
z *1
*1
ls.diag(z) H
19
187
> y - z$residuals
[1] 20.91087 13.19412 10.10742 23.99757 37.88772 40.97442 14.73747 28.62762
[9] 36.34437 28.62762 34.80102 30.17097 30.17097 19.36752 28.62762 27.08427
[17] 44.06112 45.60447 22.45422 11.65077 33.25767 36.34437 27.08427 23.99757
[25] 31.71432 37.88772 39.43107 36.34437 36.34437 36.34437 47.14782
R = () / ()
> plot(x, y)
10
20
30
40
50
60
70
65
70
75
80
85
19.3 lsfit()
19.3
lm()
*2
19.3.1 lm()
formula data
( )
19
188
formula
formula
yx
y = a + bx + ' ' y x
y x1 + x2
y = a + b1 x1 + b2 x2 + ' ' y
x1 , x2
y x1 x2
y x1 + x2 + x1 x2
y (x1 + x2)2
yx1
() + 0
y 1 + x + I(x2)
y = b0 + b1 x + b2 x2 + 'I(x2) poly(x,2)
yx|z
z y x
y . , data =
y x1 ,
y = a + b1 x1 + + ' ' y
y .
19.3.2
lm() obj
AIC(obj)
AIC
anova(obj1 , obj2)
coefficients(obj)
() coef(obj)
deviance(obj)
formula(obj)
logLik(obj)
plot(obj)
predict(obj, newdata=data.frame)
data.frame
print(obj)
residuals(obj)
() resid(obj)
df.residuals(obj)
step(obj)
AIC ()
summary(obj)
19
189
19.3.3
( 1) *3
> result <- lm(Volume ~ Girth, data = trees)
> summary(result)
( 2)
> result1 <- lm(log(Volume) ~ log(Girth), data = trees)
> summary(result1)
( 3) anova()
> anova(result1)
Analysis of Variance Table
Response: log(Volume)
Df Sum Sq Mean Sq F value
log(Girth)
1 7.9254
7.9254
Residuals
29 0.3832
0.0132
Pr(>F)
--Signif. codes:
> aov(result1)
Call:
aov(formula = result1)
Terms:
log(Girth) Residuals
Sum of Squares
7.925446
0.383244
29
Deg. of Freedom
update()
*3
> result <- summary( result <- lm(Volume ~ Girth, data = trees) )
> f.stat <- result$fstatistic
> p.value <- 1-pf(f.stat["value"],f.stat["numdf"],f.stat["dendf"])
> p.value
value
0
19
190
Call:
lm(formula = log(Volume) ~ log(Girth) + log(Height), data = trees)
Residuals:
Min
1Q
Median
3Q
Max
-0.168561 -0.048488
0.002431
0.063637
0.129223
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.63162
0.79979
log(Girth)
1.98265
0.07501
26.432
log(Height)
1.11712
0.20444
( 5) step() result2 1
AIC
AIC= -152.69
RSS
AIC
0.185 -152.685
- log(Height)
0.198
0.383 -132.185
- log(Girth)
4.628
4.813
-53.743
> summary(result3)
Call:
lm(formula = log(Volume) ~ log(Girth) + log(Height), data = trees)
Residuals:
Min
1Q
Median
3Q
Max
-0.168561 -0.048488
0.002431
0.063637
0.129223
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -6.63162
0.79979
log(Girth)
1.98265
0.07501
26.432
log(Height)
1.11712
0.20444
( 6) . 1
3Q
Max
Min
1Q
0.062948
0.174312
19
191
Coefficients:
Estimate Std. Error t value Pr(>|t|)
Girth
0.144695
0.006381
22.68
Height 0.017830
0.001138
( 7) wle mle.cp()mle.aic()mle.cv() Cp
AIC
# cp
# CV
aic
[1,]
-64.560
[2,]
-36.700
[3,]
2.196
[4,]
169.000
[5,]
632.100
[6,]
976.300
[7,]
0 1158.000
best models
wle wle.cp()wle.aic()wle.cv()
Cp AIC
19.4
glm() *4
binomialpoissoninverse.gaussian
gaussian
Gammaquasi family
family
binomial
gaussian
Gamma
inverse.gaussian
poisson
quasi
p(x) =
*4
1 + exp{(0 + 1 x)}
19
192
b1 <- 0.5;
<- 1/(1+exp(-b0+b1*x))
1Q
Median
3Q
Max
-0.28951
-0.16852
-0.09972
-0.05892
2.86202
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept)
-3.1513
1.4901
-2.115
0.0344 *
-0.4216
0.4925
-0.856
0.3919
19.5
nls() *5
par(new=T)
0.98
0.97
0.95
0.96
predict.c
0.99
1.00
6
x
*5
nlme nlme()
10
20
193
20
( library() )
20.1
v2 v1 v4 v3 v6 v5
v2
v3
v4
v5
v6
v2
v3
v4
v5
v6
Loadings:
Factor1 Factor2 Factor3
v1 0.944
0.182
0.267
v2 0.905
0.235
0.159
v3 0.236
0.210
0.946
v4 0.180
0.242
0.828
v5 0.242
0.881
0.286
v6 0.193
0.959
0.196
20
194
1.893
1.886
1.797
Proportion Var
0.316
0.314
0.300
Cumulative Var
0.316
0.630
0.929
The degrees of freedom for the model is 0 and the fit was 0.4755
v2 v1 v4 v3 v6 v5
20.2
cancor()
> library(mva)
> data(LifeCycleSavings)
> pop <- LifeCycleSavings[, 2:3]
> oec <- LifeCycleSavings[, -(2:3)]
> cancor(pop, oec)
$cor
# ()
$xcoef
[,1]
[,2]
0.048647514 -0.26031158
# y
$ycoef
[,1]
[,2]
[,3]
sr
0.0084710221
3.337936e-02 -5.157130e-03
dpi
0.0001307398 -7.588232e-05
4.543705e-06
5.188324e-02
# x
$xcenter
pop15
pop75
35.0896
2.2930
# y
$ycenter
sr
dpi
ddpi
9.6710 1106.7584
3.7576
20.3
MASS lda()
predict.lda()
> library(MASS)
> data(iris3)
> Iris <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]),
+
Sp = rep(c("s","c","v"), rep(50,3)))
20
> table(Iris$Sp[train])
c
195
25 25 25
# 22 23 30
> data(iris3)
> tr <- sample(1:50, 25)
> train <- rbind(iris3[tr,,1], iris3[tr,,2], iris3[tr,,3])
> test <- rbind(iris3[-tr,,1], iris3[-tr,,2], iris3[-tr,,3])
> cl <- factor(c(rep("s",25), rep("c",25), rep("v",25)))
> z <- qda(train, cl)
> predict(z,test)$class
[1] s s s s s s s s s s s s s s s s s s s s s s s s s c c c c c c c c c c c c c
[39] c c c c c v c c c c c c v v v v v v v v v v v v v v v v v v c v v v v v v
Levels: c s v
20.4
prcomp()
> data(USArrests)
> prcomp(USArrests)
Standard deviations:
[1] 83.732400 14.212402
6.489426
2.482790
Rotation:
PC1
PC2
PC3
PC4
Murder
-0.04170432 -0.04482166
0.07989066
0.99492173
Assault
UrbanPop -0.04633575
0.97685748 -0.20054629
Rape
0.20071807
-0.07515550
0.05816914
0.97408059 -0.07232502
PC2
PC3
PC4
Murder
-0.5358995 -0.4181809
0.3412327 -0.64922780
Assault
-0.5831836 -0.1879856
0.2681484
0.74340748
UrbanPop -0.2781909
0.8728062
Rape
-0.5434321
0.3780158 -0.13387773
20
196
> plot(prcomp(USArrests))
> summary(prcomp(USArrests, scale = TRUE))
Importance of components:
PC1
Standard deviation
PC2
PC3
PC4
20.5
hclust()
> data(USArrests)
> hc <- hclust(dist(USArrests), "ave")
> plot(hc)
> plot(hc, hang = -1)
> hc <- hclust(dist(USArrests)^2, "cen")
> memb <- cutree(hc, k = 10); cent <- NULL
> for(k in 1:10) cent <- rbind(cent, colMeans(USArrests[memb == k, , drop = FALSE]))
> hc1 <- hclust(dist(cent)^2, method = "cen" , members = table(memb))
> opar <- par(mfrow = c(1, 2))
> plot(hc, labels = FALSE, hang = -1, main = "Original Tree")
> plot(hc1, labels = FALSE, hang = -1, main = "Re-start from 10 clusters")
> par(opar)
Cluster Dendrogram
Original Tree
(1)
20000
Height
10000
0
5000
20.2
5000
Height
15000
20000
Florida
North Carolina
California
Maryland
Arizona
New Mexico
Delaware
Alabama
Louisiana
Illinois
New York
Michigan
Nevada
Alaska
Mississippi
South Carolina
Washington
Oregon
Wyoming
Oklahoma
Virginia
Rhode Island
Massachusetts
New Jersey
Missouri
Arkansas
Tennessee
Georgia
Colorado
Texas
Idaho
Nebraska
Kentucky
Montana
Ohio
Utah
Indiana
Kansas
Connecticut
Pennsylvania
Hawaii
West Virginia
Maine
South Dakota
North Dakota
Vermont
Minnesota
Wisconsin
Iowa
New Hampshire
1000
0
dist(USArrests)
hclust (*, "average")
20.1
10000
50
Height
15000
100
6000
5000
4000
3000
2000
Variances
150
7000
prcomp(USArrests)
dist(USArrests)^2
hclust (*, "centroid")
20.3
(2)
k-means
> library(mva)
> x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
+
# 2D example
dist(cent)^2
hclust (*, "centroid")
20
197
20.6
R R (
)
THE R BOOK ()
ts()
as.ts()is.ts()
ts.union()
ts.intersection()
window()
diff()
diffinv()
filter()
lag()
acf()
aggregate()
nfrequency
ar()
AR ar.burg()
ar.yw()ar.mle()ar.ols()
arima()
ARIMA
arima.sim()
ARMAacf()
ARMA ARMAtoMA()
Box.test()
ccf()
decompose()
stl()
pacf()
(partial autocorrelations)
PP.test()
spectrum()
spec.ar() spec.pgram()
spec.taper()
cpgram()
lag.plot()
monthplot()
ts.plot()
ts.plot() *1
> library(ts)
> data(UKLungDeaths)
> ts.plot(ldeaths, mdeaths, fdeaths,
+
*1
plot()
20
198
20.7
aml
time
status
x
Maintained Nonmaintained
*2 survfit()
> library(survival)
splines
# time2 +
11
31
18
Inf
x=Nonmaintained 12
11
23
Inf
> summary(result)
0.6
Maintained
Nonmaintained
0.4
2000
0.0
500
1000
0.2
1500
deaths
2500
3000
0.8
3500
1.0
4000
1974
1975
1976
1977
1978
1979
1980
50
100
150
year
20.4
20.5
survdiff() coxph() *3 R
( ) THE R BOOK ()
*2
Surv(, , ) 0123
*3 0 strata()
20
199
20.8
() ( )
> library(modreg)
> data(cars)
> with(cars, {
+
plot(speed, dist)
})
> data(cars)
> attach(cars)
> plot(speed, dist, main = "data(cars)
&
smoothing splines")
spar= 0.7801305
120
dist
60
80
100
20
40
60
40
20
dist
80
100
120
10
15
speed
20
25
10
15
speed
20
25
[1] The R Projecthttp : //www.r project.org/
R
[2] S ()
[3] S - (1) - ()
[4] isac S http : //s.isac.co.jp/
( 3 ) S 9 R ()
R
[5] HPhttp : //www.is.titech.ac.jp/ mase/R.html
R
R
R
[6] R ( HP)http : //aoki2.si.gunma u.ac.jp/R/
Mac R
[11] ()
R R
R ( S-Plus )
[15] R ()
PDF GUI R R commander
R commander
PDF (05/08/08)
PDF
RjpWiki PDF