This file is indexed.

/usr/share/R/doc/manual/R-intro.html is in r-doc-html 3.4.4-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This manual is for R, version 3.4.4 (2018-03-15).

Copyright (C) 1990 W. N. Venables

Copyright (C) 1992 W. N. Venables & D. M. Smith

Copyright (C) 1997 R. Gentleman & R. Ihaka

Copyright (C) 1997, 1998 M. Maechler

Copyright (C) 1999-2018 R Core Team

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation
approved by the R Core Team. -->
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>An Introduction to R</title>

<meta name="description" content="An Introduction to R">
<meta name="keywords" content="An Introduction to R">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2any">
<link href="#Top" rel="start" title="Top">
<link href="#Function-and-variable-index" rel="index" title="Function and variable index">
<link href="#SEC_Contents" rel="contents" title="Table of Contents">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
body {
    margin-left: 5%;
    margin-right: 5%;
}

h1 {             
    background: white;
    color: rgb(25%, 25%, 25%);
    font-family: monospace;
    font-size: xx-large;
    text-align: center;
}

h2 {
    background: white;
    color: rgb(40%, 40%, 40%);
    font-family: monospace;
    font-size: x-large;
    text-align: center;
}

h3 {
    background: white;
    color: rgb(40%, 40%, 40%);
    font-family: monospace;
    font-size: large;
}

h4 {
    background: white;
    color: rgb(40%, 40%, 40%);
    font-family: monospace;
}

span.samp {
    font-family: monospace;
}

span.command {
    font-family: monospace;
}

span.option {
    font-family: monospace;
}

span.file {
    font-family: monospace;
}

span.env {
    font-family: monospace;
}

ul {
    margin-top: 0.25ex;
    margin-bottom: 0.25ex;
}

li {
    margin-top: 0.25ex;
    margin-bottom: 0.25ex;
}

p {
    margin-top: 0.6ex;
    margin-bottom: 1.2ex;
}

-->
</style>


</head>

<body lang="en">
<h1 class="settitle" align="center">An Introduction to R</h1>

























<a name="SEC_Contents"></a>
<h2 class="contents-heading">Table of Contents</h2>

<div class="contents">

<ul class="no-bullet">
  <li><a name="toc-Preface-1" href="#Preface">Preface</a></li>
  <li><a name="toc-Introduction-and-preliminaries-1" href="#Introduction-and-preliminaries">1 Introduction and preliminaries</a>
  <ul class="no-bullet">
    <li><a name="toc-The-R-environment-1" href="#The-R-environment">1.1 The R environment</a></li>
    <li><a name="toc-Related-software-and-documentation-1" href="#Related-software-and-documentation">1.2 Related software and documentation</a></li>
    <li><a name="toc-R-and-statistics-1" href="#R-and-statistics">1.3 R and statistics</a></li>
    <li><a name="toc-R-and-the-window-system-1" href="#R-and-the-window-system">1.4 R and the window system</a></li>
    <li><a name="toc-Using-R-interactively-1" href="#Using-R-interactively">1.5 Using R interactively</a></li>
    <li><a name="toc-An-introductory-session" href="#An-introductory-session">1.6 An introductory session</a></li>
    <li><a name="toc-Getting-help-with-functions-and-features" href="#Getting-help">1.7 Getting help with functions and features</a></li>
    <li><a name="toc-R-commands_002c-case-sensitivity_002c-etc_002e" href="#R-commands_003b-case-sensitivity-etc">1.8 R commands, case sensitivity, etc.</a></li>
    <li><a name="toc-Recall-and-correction-of-previous-commands-1" href="#Recall-and-correction-of-previous-commands">1.9 Recall and correction of previous commands</a></li>
    <li><a name="toc-Executing-commands-from-or-diverting-output-to-a-file-1" href="#Executing-commands-from-or-diverting-output-to-a-file">1.10 Executing commands from or diverting output to a file</a></li>
    <li><a name="toc-Data-permanency-and-removing-objects-1" href="#Data-permanency-and-removing-objects">1.11 Data permanency and removing objects</a></li>
  </ul></li>
  <li><a name="toc-Simple-manipulations_003b-numbers-and-vectors" href="#Simple-manipulations-numbers-and-vectors">2 Simple manipulations; numbers and vectors</a>
  <ul class="no-bullet">
    <li><a name="toc-Vectors-and-assignment-1" href="#Vectors-and-assignment">2.1 Vectors and assignment</a></li>
    <li><a name="toc-Vector-arithmetic-1" href="#Vector-arithmetic">2.2 Vector arithmetic</a></li>
    <li><a name="toc-Generating-regular-sequences-1" href="#Generating-regular-sequences">2.3 Generating regular sequences</a></li>
    <li><a name="toc-Logical-vectors-1" href="#Logical-vectors">2.4 Logical vectors</a></li>
    <li><a name="toc-Missing-values-1" href="#Missing-values">2.5 Missing values</a></li>
    <li><a name="toc-Character-vectors-1" href="#Character-vectors">2.6 Character vectors</a></li>
    <li><a name="toc-Index-vectors_003b-selecting-and-modifying-subsets-of-a-data-set" href="#Index-vectors">2.7 Index vectors; selecting and modifying subsets of a data set</a></li>
    <li><a name="toc-Other-types-of-objects-1" href="#Other-types-of-objects">2.8 Other types of objects</a></li>
  </ul></li>
  <li><a name="toc-Objects_002c-their-modes-and-attributes" href="#Objects">3 Objects, their modes and attributes</a>
  <ul class="no-bullet">
    <li><a name="toc-Intrinsic-attributes_003a-mode-and-length" href="#The-intrinsic-attributes-mode-and-length">3.1 Intrinsic attributes: mode and length</a></li>
    <li><a name="toc-Changing-the-length-of-an-object-1" href="#Changing-the-length-of-an-object">3.2 Changing the length of an object</a></li>
    <li><a name="toc-Getting-and-setting-attributes-1" href="#Getting-and-setting-attributes">3.3 Getting and setting attributes</a></li>
    <li><a name="toc-The-class-of-an-object-1" href="#The-class-of-an-object">3.4 The class of an object</a></li>
  </ul></li>
  <li><a name="toc-Ordered-and-unordered-factors" href="#Factors">4 Ordered and unordered factors</a>
  <ul class="no-bullet">
    <li><a name="toc-A-specific-example" href="#A-specific-example">4.1 A specific example</a></li>
    <li><a name="toc-The-function-tapply_0028_0029-and-ragged-arrays-1" href="#The-function-tapply_0028_0029-and-ragged-arrays">4.2 The function <code>tapply()</code> and ragged arrays</a></li>
    <li><a name="toc-Ordered-factors-1" href="#Ordered-factors">4.3 Ordered factors</a></li>
  </ul></li>
  <li><a name="toc-Arrays-and-matrices-1" href="#Arrays-and-matrices">5 Arrays and matrices</a>
  <ul class="no-bullet">
    <li><a name="toc-Arrays-1" href="#Arrays">5.1 Arrays</a></li>
    <li><a name="toc-Array-indexing_002e-Subsections-of-an-array" href="#Array-indexing">5.2 Array indexing.  Subsections of an array</a></li>
    <li><a name="toc-Index-matrices-1" href="#Index-matrices">5.3 Index matrices</a></li>
    <li><a name="toc-The-array_0028_0029-function-1" href="#The-array_0028_0029-function">5.4 The <code>array()</code> function</a>
    <ul class="no-bullet">
      <li><a name="toc-Mixed-vector-and-array-arithmetic_002e-The-recycling-rule" href="#The-recycling-rule">5.4.1 Mixed vector and array arithmetic.  The recycling rule</a></li>
    </ul></li>
    <li><a name="toc-The-outer-product-of-two-arrays-1" href="#The-outer-product-of-two-arrays">5.5 The outer product of two arrays</a></li>
    <li><a name="toc-Generalized-transpose-of-an-array-1" href="#Generalized-transpose-of-an-array">5.6 Generalized transpose of an array</a></li>
    <li><a name="toc-Matrix-facilities-1" href="#Matrix-facilities">5.7 Matrix facilities</a>
    <ul class="no-bullet">
      <li><a name="toc-Matrix-multiplication" href="#Multiplication">5.7.1 Matrix multiplication</a></li>
      <li><a name="toc-Linear-equations-and-inversion-1" href="#Linear-equations-and-inversion">5.7.2 Linear equations and inversion</a></li>
      <li><a name="toc-Eigenvalues-and-eigenvectors-1" href="#Eigenvalues-and-eigenvectors">5.7.3 Eigenvalues and eigenvectors</a></li>
      <li><a name="toc-Singular-value-decomposition-and-determinants-1" href="#Singular-value-decomposition-and-determinants">5.7.4 Singular value decomposition and determinants</a></li>
      <li><a name="toc-Least-squares-fitting-and-the-QR-decomposition-1" href="#Least-squares-fitting-and-the-QR-decomposition">5.7.5 Least squares fitting and the QR decomposition</a></li>
    </ul></li>
    <li><a name="toc-Forming-partitioned-matrices_002c-cbind_0028_0029-and-rbind_0028_0029" href="#Forming-partitioned-matrices">5.8 Forming partitioned matrices, <code>cbind()</code> and <code>rbind()</code></a></li>
    <li><a name="toc-The-concatenation-function_002c-c_0028_0029_002c-with-arrays" href="#The-concatenation-function-c_0028_0029-with-arrays">5.9 The concatenation function, <code>c()</code>, with arrays</a></li>
    <li><a name="toc-Frequency-tables-from-factors-1" href="#Frequency-tables-from-factors">5.10 Frequency tables from factors</a></li>
  </ul></li>
  <li><a name="toc-Lists-and-data-frames-1" href="#Lists-and-data-frames">6 Lists and data frames</a>
  <ul class="no-bullet">
    <li><a name="toc-Lists-1" href="#Lists">6.1 Lists</a></li>
    <li><a name="toc-Constructing-and-modifying-lists-1" href="#Constructing-and-modifying-lists">6.2 Constructing and modifying lists</a>
    <ul class="no-bullet">
      <li><a name="toc-Concatenating-lists-1" href="#Concatenating-lists">6.2.1 Concatenating lists</a></li>
    </ul></li>
    <li><a name="toc-Data-frames-1" href="#Data-frames">6.3 Data frames</a>
    <ul class="no-bullet">
      <li><a name="toc-Making-data-frames-1" href="#Making-data-frames">6.3.1 Making data frames</a></li>
      <li><a name="toc-attach_0028_0029-and-detach_0028_0029-1" href="#attach_0028_0029-and-detach_0028_0029">6.3.2 <code>attach()</code> and <code>detach()</code></a></li>
      <li><a name="toc-Working-with-data-frames-1" href="#Working-with-data-frames">6.3.3 Working with data frames</a></li>
      <li><a name="toc-Attaching-arbitrary-lists-1" href="#Attaching-arbitrary-lists">6.3.4 Attaching arbitrary lists</a></li>
      <li><a name="toc-Managing-the-search-path-1" href="#Managing-the-search-path">6.3.5 Managing the search path</a></li>
    </ul></li>
  </ul></li>
  <li><a name="toc-Reading-data-from-files-1" href="#Reading-data-from-files">7 Reading data from files</a>
  <ul class="no-bullet">
    <li><a name="toc-The-read_002etable_0028_0029-function-1" href="#The-read_002etable_0028_0029-function">7.1 The <code>read.table()</code> function</a></li>
    <li><a name="toc-The-scan_0028_0029-function-1" href="#The-scan_0028_0029-function">7.2 The <code>scan()</code> function</a></li>
    <li><a name="toc-Accessing-builtin-datasets-1" href="#Accessing-builtin-datasets">7.3 Accessing builtin datasets</a>
    <ul class="no-bullet">
      <li><a name="toc-Loading-data-from-other-R-packages" href="#Loading-data-from-other-R-packages">7.3.1 Loading data from other R packages</a></li>
    </ul></li>
    <li><a name="toc-Editing-data-1" href="#Editing-data">7.4 Editing data</a></li>
  </ul></li>
  <li><a name="toc-Probability-distributions-1" href="#Probability-distributions">8 Probability distributions</a>
  <ul class="no-bullet">
    <li><a name="toc-R-as-a-set-of-statistical-tables-1" href="#R-as-a-set-of-statistical-tables">8.1 R as a set of statistical tables</a></li>
    <li><a name="toc-Examining-the-distribution-of-a-set-of-data-1" href="#Examining-the-distribution-of-a-set-of-data">8.2 Examining the distribution of a set of data</a></li>
    <li><a name="toc-One_002d-and-two_002dsample-tests-1" href="#One_002d-and-two_002dsample-tests">8.3 One- and two-sample tests</a></li>
  </ul></li>
  <li><a name="toc-Grouping_002c-loops-and-conditional-execution" href="#Loops-and-conditional-execution">9 Grouping, loops and conditional execution</a>
  <ul class="no-bullet">
    <li><a name="toc-Grouped-expressions-1" href="#Grouped-expressions">9.1 Grouped expressions</a></li>
    <li><a name="toc-Control-statements-1" href="#Control-statements">9.2 Control statements</a>
    <ul class="no-bullet">
      <li><a name="toc-Conditional-execution_003a-if-statements" href="#Conditional-execution">9.2.1 Conditional execution: <code>if</code> statements</a></li>
      <li><a name="toc-Repetitive-execution_003a-for-loops_002c-repeat-and-while" href="#Repetitive-execution">9.2.2 Repetitive execution: <code>for</code> loops, <code>repeat</code> and <code>while</code></a></li>
    </ul></li>
  </ul></li>
  <li><a name="toc-Writing-your-own-functions-1" href="#Writing-your-own-functions">10 Writing your own functions</a>
  <ul class="no-bullet">
    <li><a name="toc-Simple-examples-1" href="#Simple-examples">10.1 Simple examples</a></li>
    <li><a name="toc-Defining-new-binary-operators-1" href="#Defining-new-binary-operators">10.2 Defining new binary operators</a></li>
    <li><a name="toc-Named-arguments-and-defaults-1" href="#Named-arguments-and-defaults">10.3 Named arguments and defaults</a></li>
    <li><a name="toc-The-_2026-argument" href="#The-three-dots-argument">10.4 The &lsquo;<samp>&hellip;</samp>&rsquo; argument</a></li>
    <li><a name="toc-Assignments-within-functions" href="#Assignment-within-functions">10.5 Assignments within functions</a></li>
    <li><a name="toc-More-advanced-examples-1" href="#More-advanced-examples">10.6 More advanced examples</a>
    <ul class="no-bullet">
      <li><a name="toc-Efficiency-factors-in-block-designs-1" href="#Efficiency-factors-in-block-designs">10.6.1 Efficiency factors in block designs</a></li>
      <li><a name="toc-Dropping-all-names-in-a-printed-array-1" href="#Dropping-all-names-in-a-printed-array">10.6.2 Dropping all names in a printed array</a></li>
      <li><a name="toc-Recursive-numerical-integration-1" href="#Recursive-numerical-integration">10.6.3 Recursive numerical integration</a></li>
    </ul></li>
    <li><a name="toc-Scope-1" href="#Scope">10.7 Scope</a></li>
    <li><a name="toc-Customizing-the-environment-1" href="#Customizing-the-environment">10.8 Customizing the environment</a></li>
    <li><a name="toc-Classes_002c-generic-functions-and-object-orientation" href="#Object-orientation">10.9 Classes, generic functions and object orientation</a></li>
  </ul></li>
  <li><a name="toc-Statistical-models-in-R-1" href="#Statistical-models-in-R">11 Statistical models in R</a>
  <ul class="no-bullet">
    <li><a name="toc-Defining-statistical-models_003b-formulae" href="#Formulae-for-statistical-models">11.1 Defining statistical models; formulae</a>
    <ul class="no-bullet">
      <li><a name="toc-Contrasts-1" href="#Contrasts">11.1.1 Contrasts</a></li>
    </ul></li>
    <li><a name="toc-Linear-models-1" href="#Linear-models">11.2 Linear models</a></li>
    <li><a name="toc-Generic-functions-for-extracting-model-information-1" href="#Generic-functions-for-extracting-model-information">11.3 Generic functions for extracting model information</a></li>
    <li><a name="toc-Analysis-of-variance-and-model-comparison-1" href="#Analysis-of-variance-and-model-comparison">11.4 Analysis of variance and model comparison</a>
    <ul class="no-bullet">
      <li><a name="toc-ANOVA-tables-1" href="#ANOVA-tables">11.4.1 ANOVA tables</a></li>
    </ul></li>
    <li><a name="toc-Updating-fitted-models-1" href="#Updating-fitted-models">11.5 Updating fitted models</a></li>
    <li><a name="toc-Generalized-linear-models-1" href="#Generalized-linear-models">11.6 Generalized linear models</a>
    <ul class="no-bullet">
      <li><a name="toc-Families-1" href="#Families">11.6.1 Families</a></li>
      <li><a name="toc-The-glm_0028_0029-function-1" href="#The-glm_0028_0029-function">11.6.2 The <code>glm()</code> function</a></li>
    </ul></li>
    <li><a name="toc-Nonlinear-least-squares-and-maximum-likelihood-models-1" href="#Nonlinear-least-squares-and-maximum-likelihood-models">11.7 Nonlinear least squares and maximum likelihood models</a>
    <ul class="no-bullet">
      <li><a name="toc-Least-squares-1" href="#Least-squares">11.7.1 Least squares</a></li>
      <li><a name="toc-Maximum-likelihood-1" href="#Maximum-likelihood">11.7.2 Maximum likelihood</a></li>
    </ul></li>
    <li><a name="toc-Some-non_002dstandard-models-1" href="#Some-non_002dstandard-models">11.8 Some non-standard models</a></li>
  </ul></li>
  <li><a name="toc-Graphical-procedures" href="#Graphics">12 Graphical procedures</a>
  <ul class="no-bullet">
    <li><a name="toc-High_002dlevel-plotting-commands-1" href="#High_002dlevel-plotting-commands">12.1 High-level plotting commands</a>
    <ul class="no-bullet">
      <li><a name="toc-The-plot_0028_0029-function-1" href="#The-plot_0028_0029-function">12.1.1 The <code>plot()</code> function</a></li>
      <li><a name="toc-Displaying-multivariate-data-1" href="#Displaying-multivariate-data">12.1.2 Displaying multivariate data</a></li>
      <li><a name="toc-Display-graphics-1" href="#Display-graphics">12.1.3 Display graphics</a></li>
      <li><a name="toc-Arguments-to-high_002dlevel-plotting-functions-1" href="#Arguments-to-high_002dlevel-plotting-functions">12.1.4 Arguments to high-level plotting functions</a></li>
    </ul></li>
    <li><a name="toc-Low_002dlevel-plotting-commands-1" href="#Low_002dlevel-plotting-commands">12.2 Low-level plotting commands</a>
    <ul class="no-bullet">
      <li><a name="toc-Mathematical-annotation-1" href="#Mathematical-annotation">12.2.1 Mathematical annotation</a></li>
      <li><a name="toc-Hershey-vector-fonts-1" href="#Hershey-vector-fonts">12.2.2 Hershey vector fonts</a></li>
    </ul></li>
    <li><a name="toc-Interacting-with-graphics-1" href="#Interacting-with-graphics">12.3 Interacting with graphics</a></li>
    <li><a name="toc-Using-graphics-parameters-1" href="#Using-graphics-parameters">12.4 Using graphics parameters</a>
    <ul class="no-bullet">
      <li><a name="toc-Permanent-changes_003a-The-par_0028_0029-function" href="#The-par_0028_0029-function">12.4.1 Permanent changes: The <code>par()</code> function</a></li>
      <li><a name="toc-Temporary-changes_003a-Arguments-to-graphics-functions" href="#Arguments-to-graphics-functions">12.4.2 Temporary changes: Arguments to graphics functions</a></li>
    </ul></li>
    <li><a name="toc-Graphics-parameters-list" href="#Graphics-parameters">12.5 Graphics parameters list</a>
    <ul class="no-bullet">
      <li><a name="toc-Graphical-elements-1" href="#Graphical-elements">12.5.1 Graphical elements</a></li>
      <li><a name="toc-Axes-and-tick-marks-1" href="#Axes-and-tick-marks">12.5.2 Axes and tick marks</a></li>
      <li><a name="toc-Figure-margins-1" href="#Figure-margins">12.5.3 Figure margins</a></li>
      <li><a name="toc-Multiple-figure-environment-1" href="#Multiple-figure-environment">12.5.4 Multiple figure environment</a></li>
    </ul></li>
    <li><a name="toc-Device-drivers-1" href="#Device-drivers">12.6 Device drivers</a>
    <ul class="no-bullet">
      <li><a name="toc-PostScript-diagrams-for-typeset-documents-1" href="#PostScript-diagrams-for-typeset-documents">12.6.1 PostScript diagrams for typeset documents</a></li>
      <li><a name="toc-Multiple-graphics-devices-1" href="#Multiple-graphics-devices">12.6.2 Multiple graphics devices</a></li>
    </ul></li>
    <li><a name="toc-Dynamic-graphics-1" href="#Dynamic-graphics">12.7 Dynamic graphics</a></li>
  </ul></li>
  <li><a name="toc-Packages-1" href="#Packages">13 Packages</a>
  <ul class="no-bullet">
    <li><a name="toc-Standard-packages-1" href="#Standard-packages">13.1 Standard packages</a></li>
    <li><a name="toc-Contributed-packages-and-CRAN-1" href="#Contributed-packages-and-CRAN">13.2 Contributed packages and <acronym>CRAN</acronym></a></li>
    <li><a name="toc-Namespaces-1" href="#Namespaces">13.3 Namespaces</a></li>
  </ul></li>
  <li><a name="toc-OS-facilities-1" href="#OS-facilities">14 OS facilities</a>
  <ul class="no-bullet">
    <li><a name="toc-Files-and-directories-1" href="#Files-and-directories">14.1 Files and directories</a></li>
    <li><a name="toc-Filepaths-1" href="#Filepaths">14.2 Filepaths</a></li>
    <li><a name="toc-System-commands-1" href="#System-commands">14.3 System commands</a></li>
    <li><a name="toc-Compression-and-Archives-1" href="#Compression-and-Archives">14.4 Compression and Archives</a></li>
  </ul></li>
  <li><a name="toc-A-sample-session-1" href="#A-sample-session">Appendix A A sample session</a></li>
  <li><a name="toc-Invoking-R-1" href="#Invoking-R">Appendix B Invoking R</a>
  <ul class="no-bullet">
    <li><a name="toc-Invoking-R-from-the-command-line-1" href="#Invoking-R-from-the-command-line">B.1 Invoking R from the command line</a></li>
    <li><a name="toc-Invoking-R-under-Windows-1" href="#Invoking-R-under-Windows">B.2 Invoking R under Windows</a></li>
    <li><a name="toc-Invoking-R-under-macOS-1" href="#Invoking-R-under-macOS">B.3 Invoking R under macOS</a></li>
    <li><a name="toc-Scripting-with-R-1" href="#Scripting-with-R">B.4 Scripting with R</a></li>
  </ul></li>
  <li><a name="toc-The-command_002dline-editor-1" href="#The-command_002dline-editor">Appendix C The command-line editor</a>
  <ul class="no-bullet">
    <li><a name="toc-Preliminaries" href="#Preliminaries">C.1 Preliminaries</a></li>
    <li><a name="toc-Editing-actions" href="#Editing-actions">C.2 Editing actions</a></li>
    <li><a name="toc-Command_002dline-editor-summary" href="#Command_002dline-editor-summary">C.3 Command-line editor summary</a></li>
  </ul></li>
  <li><a name="toc-Function-and-variable-index-1" href="#Function-and-variable-index">Appendix D Function and variable index</a></li>
  <li><a name="toc-Concept-index-1" href="#Concept-index">Appendix E Concept index</a></li>
  <li><a name="toc-References-1" href="#References">Appendix F References</a></li>
</ul>
</div>


<a name="Top"></a>
<div class="header">
<p>
Next: <a href="#Preface" accesskey="n" rel="next">Preface</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="An-Introduction-to-R"></a>
<h1 class="top">An Introduction to R</h1>

<p>This is an introduction to R (&ldquo;GNU S&rdquo;), a language and environment for
statistical computing and graphics.  R is similar to the
award-winning<a name="DOCF1" href="#FOOT1"><sup>1</sup></a> S
system, which was developed at Bell Laboratories by John Chambers et al.
It provides a wide variety of statistical and graphical techniques
(linear and nonlinear modelling, statistical tests, time series
analysis, classification, clustering, ...).
</p>
<p>This manual provides information on data types, programming elements,
statistical modelling and graphics.
</p>
<p>This manual is for R, version 3.4.4 (2018-03-15).
</p>
<p>Copyright &copy; 1990 W. N. Venables<br>
Copyright &copy; 1992 W. N. Venables &amp; D. M. Smith<br>
Copyright &copy; 1997 R. Gentleman &amp; R. Ihaka<br>
Copyright &copy; 1997, 1998 M. Maechler<br>
Copyright &copy; 1999&ndash;2018 R Core Team
</p>
<blockquote>
<p>Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
</p>
<p>Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
</p>
<p>Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation
approved by the R Core Team.
</p></blockquote>



<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Preface" accesskey="1">Preface</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Introduction-and-preliminaries" accesskey="2">Introduction and preliminaries</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Simple-manipulations-numbers-and-vectors" accesskey="3">Simple manipulations numbers and vectors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Objects" accesskey="4">Objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Factors" accesskey="5">Factors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Arrays-and-matrices" accesskey="6">Arrays and matrices</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Lists-and-data-frames" accesskey="7">Lists and data frames</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Reading-data-from-files" accesskey="8">Reading data from files</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Probability-distributions" accesskey="9">Probability distributions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Loops-and-conditional-execution">Loops and conditional execution</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Writing-your-own-functions">Writing your own functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Statistical-models-in-R">Statistical models in R</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Graphics">Graphics</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Packages">Packages</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#OS-facilities">OS facilities</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#A-sample-session">A sample session</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Invoking-R">Invoking R</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-command_002dline-editor">The command-line editor</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Function-and-variable-index">Function and variable index</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Concept-index">Concept index</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#References">References</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Preface"></a>
<div class="header">
<p>
Next: <a href="#Introduction-and-preliminaries" accesskey="n" rel="next">Introduction and preliminaries</a>, Previous: <a href="#Top" accesskey="p" rel="prev">Top</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Preface-1"></a>
<h2 class="unnumbered">Preface</h2>

<p>This introduction to R is derived from an original set of notes
describing the S and <small>S-PLUS</small> environments written in 1990&ndash;2 by
Bill Venables and David M. Smith when at the University of Adelaide.  We
have made a number of small changes to reflect differences between the
R and S programs, and expanded some of the material.
</p>
<p>We would like to extend warm thanks to Bill Venables (and David Smith)
for granting permission to distribute this modified version of the notes
in this way, and for being a supporter of R from way back.
</p>
<p>Comments and corrections are always welcome.  Please address email
correspondence to <a href="mailto:R-core@R-project.org">R-core@R-project.org</a>.
</p>
<a name="Suggestions-to-the-reader"></a>
<h4 class="subheading">Suggestions to the reader</h4>

<p>Most R novices will start with the introductory session in Appendix
A.  This should give some familiarity with the style of R sessions
and more importantly some instant feedback on what actually happens.
</p>
<p>Many users will come to R mainly for its graphical facilities.
See <a href="#Graphics">Graphics</a>, which can be read at almost any time and need not wait
until all the preceding sections have been digested.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Introduction-and-preliminaries" accesskey="1">Introduction and preliminaries</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Introduction-and-preliminaries"></a>
<div class="header">
<p>
Next: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="n" rel="next">Simple manipulations numbers and vectors</a>, Previous: <a href="#Preface" accesskey="p" rel="prev">Preface</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Introduction-and-preliminaries-1"></a>
<h2 class="chapter">1 Introduction and preliminaries</h2>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-R-environment" accesskey="1">The R environment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Related-software-and-documentation" accesskey="2">Related software and documentation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#R-and-statistics" accesskey="3">R and statistics</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#R-and-the-window-system" accesskey="4">R and the window system</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Using-R-interactively" accesskey="5">Using R interactively</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Getting-help" accesskey="6">Getting help</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#R-commands_003b-case-sensitivity-etc" accesskey="7">R commands; case sensitivity etc</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Recall-and-correction-of-previous-commands" accesskey="8">Recall and correction of previous commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Executing-commands-from-or-diverting-output-to-a-file" accesskey="9">Executing commands from or diverting output to a file</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Data-permanency-and-removing-objects">Data permanency and removing objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-R-environment"></a>
<div class="header">
<p>
Next: <a href="#Related-software-and-documentation" accesskey="n" rel="next">Related software and documentation</a>, Previous: <a href="#Introduction-and-preliminaries" accesskey="p" rel="prev">Introduction and preliminaries</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-R-environment-1"></a>
<h3 class="section">1.1 The R environment</h3>

<p>R is an integrated suite of software facilities for data
manipulation, calculation and graphical display.  Among other things it
has
</p>
<ul>
<li> an effective data handling and storage facility,
</li><li> a suite of operators for calculations on arrays, in particular matrices,
</li><li> a large, coherent, integrated collection of intermediate tools for data
analysis,
</li><li> graphical facilities for data analysis and display either directly at
the computer or on hardcopy, and
</li><li> a well developed, simple and effective programming language (called &lsquo;S&rsquo;)
which includes conditionals, loops, user defined recursive functions and
input and output facilities.  (Indeed most of the system supplied
functions are themselves written in the S language.)
</li></ul>

<p>The term &ldquo;environment&rdquo; is intended to characterize it as a fully
planned and coherent system, rather than an incremental accretion of
very specific and inflexible tools, as is frequently the case with other
data analysis software.
</p>
<p>R is very much a vehicle for newly developing methods of interactive
data analysis.  It has developed rapidly, and has been extended by a
large collection of <em>packages</em>.  However, most programs written in
R are essentially ephemeral, written for a single piece of data
analysis.
</p>
<hr>
<a name="Related-software-and-documentation"></a>
<div class="header">
<p>
Next: <a href="#R-and-statistics" accesskey="n" rel="next">R and statistics</a>, Previous: <a href="#The-R-environment" accesskey="p" rel="prev">The R environment</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Related-software-and-documentation-1"></a>
<h3 class="section">1.2 Related software and documentation</h3>

<p>R can be regarded as an implementation of the S language which
was developed at Bell Laboratories by Rick Becker, John Chambers and
Allan Wilks, and also forms the basis of the <small>S-PLUS</small> systems.
</p>
<p>The evolution of the S language is characterized by four books by
John Chambers and coauthors.  For R, the basic reference is <em>The
New S Language: A Programming Environment for Data Analysis and
Graphics</em> by Richard A. Becker, John M. Chambers and Allan R.
Wilks.  The new features of the 1991 release of S
are covered in <em>Statistical Models in S</em> edited by John M.
Chambers and Trevor J. Hastie.  The formal methods and classes of the
<strong>methods</strong> package are based on those described in <em>Programming
with Data</em> by John M. Chambers.  See <a href="#References">References</a>, for precise
references.
</p>
<p>There are now a number of books which describe how to use R for data
analysis and statistics, and documentation for S/<small>S-PLUS</small> can
typically be used with R, keeping the differences between the S
implementations in mind.  See <a href="R-FAQ.html#What-documentation-exists-for-R_003f">What documentation exists for R?</a> in <cite>The R statistical system FAQ</cite>.
</p>
<hr>
<a name="R-and-statistics"></a>
<div class="header">
<p>
Next: <a href="#R-and-the-window-system" accesskey="n" rel="next">R and the window system</a>, Previous: <a href="#Related-software-and-documentation" accesskey="p" rel="prev">Related software and documentation</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="R-and-statistics-1"></a>
<h3 class="section">1.3 R and statistics</h3>
<a name="index-Packages"></a>

<p>Our introduction to the R environment did not mention
<em>statistics</em>, yet many people use R as a statistics system.  We
prefer to think of it of an environment within which many classical and
modern statistical techniques have been implemented.  A few of these are
built into the base R environment, but many are supplied as
<em>packages</em>.  There are about 25 packages supplied with R (called
&ldquo;standard&rdquo; and &ldquo;recommended&rdquo; packages) and many more are available
through the <acronym>CRAN</acronym> family of Internet sites (via
<a href="https://CRAN.R-project.org">https://CRAN.R-project.org</a>) and elsewhere.  More details on
packages are given later (see <a href="#Packages">Packages</a>).
</p>
<p>Most classical statistics and much of the latest methodology is
available for use with R, but users may need to be prepared to do a
little work to find it.
</p>
<p>There is an important difference in philosophy between S (and hence
R) and the other main statistical systems.  In S a statistical
analysis is normally done as a series of steps, with intermediate
results being stored in objects.  Thus whereas SAS and SPSS will give
copious output from a regression or discriminant analysis, R will
give minimal output and store the results in a fit object for subsequent
interrogation by further R functions.
</p>
<hr>
<a name="R-and-the-window-system"></a>
<div class="header">
<p>
Next: <a href="#Using-R-interactively" accesskey="n" rel="next">Using R interactively</a>, Previous: <a href="#R-and-statistics" accesskey="p" rel="prev">R and statistics</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="R-and-the-window-system-1"></a>
<h3 class="section">1.4 R and the window system</h3>

<p>The most convenient way to use R is at a graphics workstation running
a windowing system.  This guide is aimed at users who have this
facility.  In particular we will occasionally refer to the use of R
on an X window system although the vast bulk of what is said applies
generally to any implementation of the R environment.
</p>
<p>Most users will find it necessary to interact directly with the
operating system on their computer from time to time.  In this guide, we
mainly discuss interaction with the operating system on UNIX machines.
If you are running R under Windows or macOS you will need to make
some small adjustments.
</p>
<p>Setting up a workstation to take full advantage of the customizable
features of R is a straightforward if somewhat tedious procedure, and
will not be considered further here.  Users in difficulty should seek
local expert help.
</p>
<hr>
<a name="Using-R-interactively"></a>
<div class="header">
<p>
Next: <a href="#Getting-help" accesskey="n" rel="next">Getting help</a>, Previous: <a href="#R-and-the-window-system" accesskey="p" rel="prev">R and the window system</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Using-R-interactively-1"></a>
<h3 class="section">1.5 Using R interactively</h3>

<p>When you use the R program it issues a prompt when it expects input
commands.  The default prompt is &lsquo;<samp><code>&gt;</code></samp>&rsquo;, which on UNIX might be
the same as the shell prompt, and so it may appear that nothing is
happening.  However, as we shall see, it is easy to change to a
different R prompt if you wish.  We will assume that the UNIX shell
prompt is &lsquo;<samp><code>$</code></samp>&rsquo;.
</p>
<p>In using R under UNIX the suggested procedure for the first occasion
is as follows:
</p>
<ol>
<li> Create a separate sub-directory, say <samp>work</samp>, to hold data files on
which you will use R for this problem.  This will be the working
directory whenever you use R for this particular problem.

<div class="example">
<pre class="example">$ mkdir work
$ cd work
</pre></div>

</li><li> Start the R program with the command

<div class="example">
<pre class="example">$ R
</pre></div>

</li><li> At this point R commands may be issued (see later).

</li><li> To quit the R program the command is

<div class="example">
<pre class="example">&gt; q()
</pre></div>

<p>At this point you will be asked whether you want to save the data from
your R session.  On some systems this will bring up a dialog box, and
on others you will receive a text prompt to which you can respond
<kbd>yes</kbd>, <kbd>no</kbd> or <kbd>cancel</kbd> (a single letter abbreviation will
do) to save the data before quitting, quit without saving, or return to
the R session.  Data which is saved will be available in future R
sessions.
</p>
</li></ol>

<p>Further R sessions are simple.
</p>
<ol>
<li> Make <samp>work</samp> the working directory and start the program as before:

<div class="example">
<pre class="example">$ cd work
$ R
</pre></div>

</li><li> Use the R program, terminating with the <code>q()</code> command at the end
of the session.

</li></ol>

<p>To use R under Windows the procedure to
follow is basically the same.  Create a folder as the working directory,
and set that in the <samp>Start In</samp> field in your R shortcut.
Then launch R by double clicking on the icon.
</p>
<a name="An-introductory-session"></a>
<h3 class="section">1.6 An introductory session</h3>

<p>Readers wishing to get a feel for R at a computer before proceeding
are strongly advised to work through the introductory session
given in <a href="#A-sample-session">A sample session</a>.
</p>
<hr>
<a name="Getting-help"></a>
<div class="header">
<p>
Next: <a href="#R-commands_003b-case-sensitivity-etc" accesskey="n" rel="next">R commands; case sensitivity etc</a>, Previous: <a href="#Using-R-interactively" accesskey="p" rel="prev">Using R interactively</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Getting-help-with-functions-and-features"></a>
<h3 class="section">1.7 Getting help with functions and features</h3>
<a name="index-help"></a>

<p>R has an inbuilt help facility similar to the <code>man</code> facility of
UNIX.  To get more information on any specific named function, for
example <code>solve</code>, the command is
</p>
<div class="example">
<pre class="example">&gt; help(solve)
</pre></div>
<a name="index-help-1"></a>

<p>An alternative is
</p>
<div class="example">
<pre class="example">&gt; ?solve
</pre></div>
<a name="index-_003f"></a>

<p>For a feature specified by special characters, the argument must be
enclosed in double or single quotes, making it a &ldquo;character string&rdquo;:
This is also necessary for a few words with syntactic meaning including
<code>if</code>, <code>for</code> and <code>function</code>.
</p>
<div class="example">
<pre class="example">&gt; help(&quot;[[&quot;)
</pre></div>

<p>Either form of quote mark may be used to escape the other, as in the
string <code>&quot;It's important&quot;</code>.  Our convention is to use
double quote marks for preference.
</p>
<p>On most R installations help is available in <acronym>HTML</acronym> format by
running
</p>
<div class="example">
<pre class="example">&gt; help.start()
</pre></div>
<a name="index-help_002estart"></a>

<p>which will launch a Web browser that allows the help pages to be browsed
with hyperlinks.  On UNIX, subsequent help requests are sent to the
<acronym>HTML</acronym>-based help system.  The &lsquo;Search Engine and Keywords&rsquo; link in the
page loaded by <code>help.start()</code> is particularly useful as it is
contains a high-level concept list which searches though available
functions.  It can be a great way to get your bearings quickly and to
understand the breadth of what R has to offer.
</p>
<a name="index-help_002esearch"></a>
<p>The <code>help.search</code> command (alternatively <code>??</code>) 
allows searching for help in various
ways. For example,
</p>
<div class="example">
<pre class="example">&gt; ??solve
</pre></div>
<a name="index-_003f_003f"></a>

<p>Try <code>?help.search</code> for details and more examples.
</p>
<p>The examples on a help topic can normally be run by
</p>
<div class="example">
<pre class="example">&gt; example(<var>topic</var>)
</pre></div>
<a name="index-example"></a>

<p>Windows versions of R have other optional help systems: use
</p>
<div class="example">
<pre class="example">&gt; ?help
</pre></div>

<p>for further details.
</p>
<hr>
<a name="R-commands_003b-case-sensitivity-etc"></a>
<div class="header">
<p>
Next: <a href="#Recall-and-correction-of-previous-commands" accesskey="n" rel="next">Recall and correction of previous commands</a>, Previous: <a href="#Getting-help" accesskey="p" rel="prev">Getting help</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="R-commands_002c-case-sensitivity_002c-etc_002e"></a>
<h3 class="section">1.8 R commands, case sensitivity, etc.</h3>

<p>Technically R is an <em>expression language</em> with a very simple
syntax.  It is <em>case sensitive</em> as are most UNIX based packages, so
<code>A</code> and <code>a</code> are different symbols and would refer to different
variables.  The set of symbols which can be used in R names depends
on the operating system and country within which R is being run
(technically on the <em>locale</em> in use).  Normally all alphanumeric
symbols are allowed<a name="DOCF2" href="#FOOT2"><sup>2</sup></a> (and in
some countries this includes accented letters) plus &lsquo;<samp><code>.</code></samp>&rsquo; and
&lsquo;<samp><code>_</code></samp>&rsquo;, with the restriction that a name must start with
&lsquo;<samp><code>.</code></samp>&rsquo; or a letter, and if it starts with &lsquo;<samp><code>.</code></samp>&rsquo; the
second character must not be a digit.  Names are effectively
unlimited in length.
</p>
<p>Elementary commands consist of either <em>expressions</em> or
<em>assignments</em>.  If an expression is given as a command, it is
evaluated, printed (unless specifically made invisible), and the value
is lost.  An assignment also evaluates an expression and passes the
value to a variable but the result is not automatically printed.
</p>
<p>Commands are separated either by a semi-colon (&lsquo;<samp><code>;</code></samp>&rsquo;), or by a
newline.  Elementary commands can be grouped together into one compound
expression by braces (&lsquo;<samp><code>{</code></samp>&rsquo; and &lsquo;<samp><code>}</code></samp>&rsquo;).
<em>Comments</em> can be put almost<a name="DOCF3" href="#FOOT3"><sup>3</sup></a> anywhere,
starting with a hashmark (&lsquo;<samp><code>#</code></samp>&rsquo;), everything to the end of the
line is a comment.
</p>
<p>If a command is not complete at the end of a line, R will
give a different prompt, by default
</p>
<div class="example">
<pre class="example">+
</pre></div>

<p>on second and subsequent lines and continue to read input until the
command is syntactically complete.  This prompt may be changed by the
user.  We will generally omit the continuation prompt
and indicate continuation by simple indenting.
</p>
<p>Command lines entered at the console are limited<a name="DOCF4" href="#FOOT4"><sup>4</sup></a> to about 4095 bytes (not characters).
</p>
<hr>
<a name="Recall-and-correction-of-previous-commands"></a>
<div class="header">
<p>
Next: <a href="#Executing-commands-from-or-diverting-output-to-a-file" accesskey="n" rel="next">Executing commands from or diverting output to a file</a>, Previous: <a href="#R-commands_003b-case-sensitivity-etc" accesskey="p" rel="prev">R commands; case sensitivity etc</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Recall-and-correction-of-previous-commands-1"></a>
<h3 class="section">1.9 Recall and correction of previous commands</h3>

<p>Under many versions of UNIX and on Windows, R provides a mechanism
for recalling and re-executing previous commands.  The vertical arrow
keys on the keyboard can be used to scroll forward and backward through
a <em>command history</em>.  Once a command is located in this way, the
cursor can be moved within the command using the horizontal arrow keys,
and characters can be removed with the <tt class="key">DEL</tt> key or added with the
other keys.  More details are provided later: see <a href="#The-command_002dline-editor">The command-line editor</a>.
</p>
<p>The recall and editing capabilities under UNIX are highly customizable.
You can find out how to do this by reading the manual entry for the
<strong>readline</strong> library.
</p>
<p>Alternatively, the Emacs text editor provides more general support
mechanisms (via <acronym>ESS</acronym>, <em>Emacs Speaks Statistics</em>) for
working interactively with R.  See <a href="R-FAQ.html#R-and-Emacs">R and Emacs</a> in <cite>The R
statistical system FAQ</cite>.
</p>
<hr>
<a name="Executing-commands-from-or-diverting-output-to-a-file"></a>
<div class="header">
<p>
Next: <a href="#Data-permanency-and-removing-objects" accesskey="n" rel="next">Data permanency and removing objects</a>, Previous: <a href="#Recall-and-correction-of-previous-commands" accesskey="p" rel="prev">Recall and correction of previous commands</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Executing-commands-from-or-diverting-output-to-a-file-1"></a>
<h3 class="section">1.10 Executing commands from or diverting output to a file</h3>
<a name="index-Diverting-input-and-output"></a>

<p>If commands<a name="DOCF5" href="#FOOT5"><sup>5</sup></a> are stored in an external
file, say <samp>commands.R</samp> in the working directory <samp>work</samp>, they
may be executed at any time in an R session with the command
</p>
<div class="example">
<pre class="example">&gt; source(&quot;commands.R&quot;)
</pre></div>
<a name="index-source"></a>

<p>For Windows <strong>Source</strong> is also available on the
<strong>File</strong> menu.  The function <code>sink</code>,
</p>
<div class="example">
<pre class="example">&gt; sink(&quot;record.lis&quot;)
</pre></div>
<a name="index-sink"></a>

<p>will divert all subsequent output from the console to an external file,
<samp>record.lis</samp>.  The command
</p>
<div class="example">
<pre class="example">&gt; sink()
</pre></div>

<p>restores it to the console once again.
</p>
<hr>
<a name="Data-permanency-and-removing-objects"></a>
<div class="header">
<p>
Previous: <a href="#Executing-commands-from-or-diverting-output-to-a-file" accesskey="p" rel="prev">Executing commands from or diverting output to a file</a>, Up: <a href="#Introduction-and-preliminaries" accesskey="u" rel="up">Introduction and preliminaries</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Data-permanency-and-removing-objects-1"></a>
<h3 class="section">1.11 Data permanency and removing objects</h3>

<p>The entities that R creates and manipulates are known as
<em>objects</em>.  These may be variables, arrays of numbers, character
strings, functions, or more general structures built from such
components.
</p>
<p>During an R session, objects are created and stored by name (we
discuss this process in the next section).  The R command
</p>
<div class="example">
<pre class="example">&gt; objects()
</pre></div>

<p>(alternatively, <code>ls()</code>) can be used to display the names of (most
of) the objects which are currently stored within R.  The collection
of objects currently stored is called the <em>workspace</em>.
<a name="index-Workspace"></a>
</p>
<p>To remove objects the function <code>rm</code> is available:
</p>
<div class="example">
<pre class="example">&gt; rm(x, y, z, ink, junk, temp, foo, bar)
</pre></div>
<a name="index-rm"></a>
<a name="index-Removing-objects"></a>

<p>All objects created during an R session can be stored permanently in
a file for use in future R sessions.  At the end of each R session
you are given the opportunity to save all the currently available
objects.  If you indicate that you want to do this, the objects are
written to a file called <samp>.RData</samp><a name="DOCF6" href="#FOOT6"><sup>6</sup></a> in the
current directory, and the command lines used in the session are saved
to a file called <samp>.Rhistory</samp>.
</p>
<p>When R is started at later time from the same directory it reloads
the workspace from this file.  At the same time the associated commands
history is reloaded.
</p>
<p>It is recommended that you should use separate working directories for
analyses conducted with R.  It is quite common for objects with names
<code>x</code> and <code>y</code> to be created during an analysis.  Names like this
are often meaningful in the context of a single analysis, but it can be
quite hard to decide what they might be when the several analyses have
been conducted in the same directory.
</p>
<hr>
<a name="Simple-manipulations-numbers-and-vectors"></a>
<div class="header">
<p>
Next: <a href="#Objects" accesskey="n" rel="next">Objects</a>, Previous: <a href="#Introduction-and-preliminaries" accesskey="p" rel="prev">Introduction and preliminaries</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Simple-manipulations_003b-numbers-and-vectors"></a>
<h2 class="chapter">2 Simple manipulations; numbers and vectors</h2>
<a name="index-Vectors"></a>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Vectors-and-assignment" accesskey="1">Vectors and assignment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Vector-arithmetic" accesskey="2">Vector arithmetic</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Generating-regular-sequences" accesskey="3">Generating regular sequences</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Logical-vectors" accesskey="4">Logical vectors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Missing-values" accesskey="5">Missing values</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Character-vectors" accesskey="6">Character vectors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Index-vectors" accesskey="7">Index vectors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Other-types-of-objects" accesskey="8">Other types of objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Vectors-and-assignment"></a>
<div class="header">
<p>
Next: <a href="#Vector-arithmetic" accesskey="n" rel="next">Vector arithmetic</a>, Previous: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="p" rel="prev">Simple manipulations numbers and vectors</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Vectors-and-assignment-1"></a>
<h3 class="section">2.1 Vectors and assignment</h3>

<p>R operates on named <em>data structures</em>.  The simplest such
structure is the numeric <em>vector</em>, which is a single entity
consisting of an ordered collection of numbers.  To set up a vector
named <code>x</code>, say, consisting of five numbers, namely 10.4, 5.6, 3.1,
6.4 and 21.7, use the R command
</p>
<div class="example">
<pre class="example">&gt; x &lt;- c(10.4, 5.6, 3.1, 6.4, 21.7)
</pre></div>
<a name="index-c"></a>
<a name="index-vector"></a>

<p>This is an <em>assignment</em> statement using the <em>function</em>
<code>c()</code> which in this context can take an arbitrary number of vector
<em>arguments</em> and whose value is a vector got by concatenating its
arguments end to end.<a name="DOCF7" href="#FOOT7"><sup>7</sup></a>
</p>
<p>A number occurring by itself in an expression is taken as a vector of
length one.
</p>
<p>Notice that the assignment operator (&lsquo;<samp><code>&lt;-</code></samp>&rsquo;), which consists
of the two characters &lsquo;<samp><code>&lt;</code></samp>&rsquo; (&ldquo;less than&rdquo;) and
&lsquo;<samp><code>-</code></samp>&rsquo; (&ldquo;minus&rdquo;) occurring strictly side-by-side and it
&lsquo;points&rsquo; to the object receiving the value of the expression.
In most contexts the &lsquo;<samp><code>=</code></samp>&rsquo; operator can be used as an alternative.
<a name="index-Assignment"></a>
</p>
<p>Assignment can also be made using the function <code>assign()</code>.  An
equivalent way of making the same assignment as above is with:
</p>
<div class="example">
<pre class="example">&gt; assign(&quot;x&quot;, c(10.4, 5.6, 3.1, 6.4, 21.7))
</pre></div>

<p>The usual operator, <code>&lt;-</code>, can be thought of as a syntactic
short-cut to this.
</p>
<p>Assignments can also be made in the other direction, using the obvious
change in the assignment operator.  So the same assignment could be made
using
</p>
<div class="example">
<pre class="example">&gt; c(10.4, 5.6, 3.1, 6.4, 21.7) -&gt; x
</pre></div>

<p>If an expression is used as a complete command, the value is printed
<em>and lost</em><a name="DOCF8" href="#FOOT8"><sup>8</sup></a>.  So now if we
were to use the command
</p>
<div class="example">
<pre class="example">&gt; 1/x
</pre></div>

<p>the reciprocals of the five values would be printed at the terminal (and
the value of <code>x</code>, of course, unchanged).
</p>
<p>The further assignment
</p>
<div class="example">
<pre class="example">&gt; y &lt;- c(x, 0, x)
</pre></div>

<p>would create a vector <code>y</code> with 11 entries consisting of two copies
of <code>x</code> with a zero in the middle place.
</p>
<hr>
<a name="Vector-arithmetic"></a>
<div class="header">
<p>
Next: <a href="#Generating-regular-sequences" accesskey="n" rel="next">Generating regular sequences</a>, Previous: <a href="#Vectors-and-assignment" accesskey="p" rel="prev">Vectors and assignment</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Vector-arithmetic-1"></a>
<h3 class="section">2.2 Vector arithmetic</h3>

<p>Vectors can be used in arithmetic expressions, in which case the
operations are performed element by element.  Vectors occurring in the
same expression need not all be of the same length.  If they are not,
the value of the expression is a vector with the same length as the
longest vector which occurs in the expression.  Shorter vectors in the
expression are <em>recycled</em> as often as need be (perhaps
fractionally) until they match the length of the longest vector.  In
particular a constant is simply repeated.  So with the above assignments
the command
<a name="index-Recycling-rule"></a>
</p>
<div class="example">
<pre class="example">&gt; v &lt;- 2*x + y + 1
</pre></div>

<p>generates a new vector <code>v</code> of length 11 constructed by adding
together, element by element, <code>2*x</code> repeated 2.2 times, <code>y</code>
repeated just once, and <code>1</code> repeated 11 times.
</p>
<a name="index-Arithmetic-functions-and-operators"></a>
<p>The elementary arithmetic operators are the usual <code>+</code>, <code>-</code>,
<code>*</code>, <code>/</code> and <code>^</code> for raising to a power.
<a name="index-_002b"></a>
<a name="index-_002d"></a>
<a name="index-_002a"></a>
<a name="index-_002f"></a>
<a name="index-_005e"></a>
In addition all of the common arithmetic functions are available.
<code>log</code>, <code>exp</code>, <code>sin</code>, <code>cos</code>, <code>tan</code>, <code>sqrt</code>,
and so on, all have their usual meaning.
<a name="index-log"></a>
<a name="index-exp"></a>
<a name="index-sin"></a>
<a name="index-cos"></a>
<a name="index-tan"></a>
<a name="index-sqrt"></a>
<code>max</code> and <code>min</code> select the largest and smallest elements of a
vector respectively.
<a name="index-max"></a>
<a name="index-min"></a>
<code>range</code> is a function whose value is a vector of length two, namely
<code>c(min(x), max(x))</code>.
<a name="index-range"></a>
<code>length(x)</code> is the number of elements in <code>x</code>,
<a name="index-length"></a>
<code>sum(x)</code> gives the total of the elements in <code>x</code>,
<a name="index-sum"></a>
and <code>prod(x)</code> their product.
<a name="index-prod"></a>
</p>
<p>Two statistical functions are <code>mean(x)</code> which calculates the sample
mean, which is the same as <code>sum(x)/length(x)</code>,
<a name="index-mean"></a>
and <code>var(x)</code> which gives
</p>
<div class="example">
<pre class="example">sum((x-mean(x))^2)/(length(x)-1)
</pre></div>
<a name="index-var"></a>

<p>or sample variance.  If the argument to <code>var()</code> is an
<em>n</em>-by-<em>p</em> matrix the value is a <em>p</em>-by-<em>p</em> sample
covariance matrix got by regarding the rows as independent
<em>p</em>-variate sample vectors.
</p>
<p><code>sort(x)</code> returns a vector of the same size as <code>x</code> with the
elements arranged in increasing order; however there are other more
flexible sorting facilities available (see <code>order()</code> or
<code>sort.list()</code> which produce a permutation to do the sorting).
<a name="index-sort"></a>
<a name="index-order"></a>
</p>
<p>Note that <code>max</code> and <code>min</code> select the largest and smallest
values in their arguments, even if they are given several vectors.  The
<em>parallel</em> maximum and minimum functions <code>pmax</code> and
<code>pmin</code> return a vector (of length equal to their longest argument)
that contains in each element the largest (smallest) element in that
position in any of the input vectors.
<a name="index-pmax"></a>
<a name="index-pmin"></a>
</p>
<p>For most purposes the user will not be concerned if the &ldquo;numbers&rdquo; in a
numeric vector are integers, reals or even complex.  Internally
calculations are done as double precision real numbers, or double
precision complex numbers if the input data are complex.
</p>
<p>To work with complex numbers, supply an explicit complex part.  Thus
</p>
<div class="example">
<pre class="example">sqrt(-17)
</pre></div>

<p>will give <code>NaN</code> and a warning, but
</p>
<div class="example">
<pre class="example">sqrt(-17+0i)
</pre></div>

<p>will do the computations as complex numbers.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Generating-regular-sequences" accesskey="1">Generating regular sequences</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Generating-regular-sequences"></a>
<div class="header">
<p>
Next: <a href="#Logical-vectors" accesskey="n" rel="next">Logical vectors</a>, Previous: <a href="#Vector-arithmetic" accesskey="p" rel="prev">Vector arithmetic</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Generating-regular-sequences-1"></a>
<h3 class="section">2.3 Generating regular sequences</h3>
<a name="index-Regular-sequences"></a>

<p>R has a number of facilities for generating commonly used sequences
of numbers.  For example <code>1:30</code> is the vector <code>c(1, 2,
&hellip;, 29, 30)</code>.
<a name="index-_003a"></a>
The colon operator has high priority within an expression, so, for
example <code>2*1:15</code> is the vector <code>c(2, 4, &hellip;, 28, 30)</code>.
Put <code>n &lt;- 10</code> and compare the sequences <code>1:n-1</code> and
<code>1:(n-1)</code>.
</p>
<p>The construction <code>30:1</code> may be used to generate a sequence
backwards.
</p>
<a name="index-seq"></a>
<p>The function <code>seq()</code> is a more general facility for generating
sequences.  It has five arguments, only some of which may be specified
in any one call.  The first two arguments, if given, specify the
beginning and end of the sequence, and if these are the only two
arguments given the result is the same as the colon operator.  That is
<code>seq(2,10)</code> is the same vector as <code>2:10</code>.
</p>
<p>Arguments to <code>seq()</code>, and to many other R functions, can also
be given in named form, in which case the order in which they appear is
irrelevant.  The first two arguments may be named
<code>from=<var>value</var></code> and <code>to=<var>value</var></code>; thus
<code>seq(1,30)</code>, <code>seq(from=1, to=30)</code> and <code>seq(to=30,
from=1)</code> are all the same as <code>1:30</code>.  The next two arguments to
<code>seq()</code> may be named <code>by=<var>value</var></code> and
<code>length=<var>value</var></code>, which specify a step size and a length for
the sequence respectively.  If neither of these is given, the default
<code>by=1</code> is assumed.
</p>
<p>For example
</p>
<div class="example">
<pre class="example">&gt; seq(-5, 5, by=.2) -&gt; s3
</pre></div>

<p>generates in <code>s3</code> the vector <code>c(-5.0, -4.8, -4.6, &hellip;,
4.6, 4.8, 5.0)</code>.  Similarly
</p>
<div class="example">
<pre class="example">&gt; s4 &lt;- seq(length=51, from=-5, by=.2)
</pre></div>

<p>generates the same vector in <code>s4</code>.
</p>
<p>The fifth argument may be named <code>along=<var>vector</var></code>, which is
normally used as the only argument to create the sequence <code>1, 2,
&hellip;, length(<var>vector</var>)</code>, or the empty sequence if the vector is
empty (as it can be).
</p>
<p>A related function is <code>rep()</code>
<a name="index-rep"></a>
which can be used for replicating an object in various complicated ways.
The simplest form is
</p>
<div class="example">
<pre class="example">&gt; s5 &lt;- rep(x, times=5)
</pre></div>

<p>which will put five copies of <code>x</code> end-to-end in <code>s5</code>.  Another
useful version is
</p>
<div class="example">
<pre class="example">&gt; s6 &lt;- rep(x, each=5)
</pre></div>

<p>which repeats each element of <code>x</code> five times before moving on to
the next.
</p>
<hr>
<a name="Logical-vectors"></a>
<div class="header">
<p>
Next: <a href="#Missing-values" accesskey="n" rel="next">Missing values</a>, Previous: <a href="#Generating-regular-sequences" accesskey="p" rel="prev">Generating regular sequences</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Logical-vectors-1"></a>
<h3 class="section">2.4 Logical vectors</h3>

<p>As well as numerical vectors, R allows manipulation of logical
quantities.  The elements of a logical vector can have the values
<code>TRUE</code>, <code>FALSE</code>, and <code>NA</code> (for &ldquo;not available&rdquo;, see
below).  The first two are often abbreviated as <code>T</code> and <code>F</code>,
respectively.  Note however that <code>T</code> and <code>F</code> are just
variables which are set to <code>TRUE</code> and <code>FALSE</code> by default, but
are not reserved words and hence can be overwritten by the user.  Hence,
you should always use <code>TRUE</code> and <code>FALSE</code>.
<a name="index-FALSE"></a>
<a name="index-TRUE"></a>
<a name="index-F"></a>
<a name="index-T"></a>
</p>
<p>Logical vectors are generated by <em>conditions</em>.  For example
</p>
<div class="example">
<pre class="example">&gt; temp &lt;- x &gt; 13
</pre></div>

<p>sets <code>temp</code> as a vector of the same length as <code>x</code> with values
<code>FALSE</code> corresponding to elements of <code>x</code> where the condition
is <em>not</em> met and <code>TRUE</code> where it is.
</p>
<p>The logical operators are <code>&lt;</code>, <code>&lt;=</code>, <code>&gt;</code>, <code>&gt;=</code>,
<code>==</code> for exact equality and <code>!=</code> for inequality.
<a name="index-_003c"></a>
<a name="index-_003c_003d"></a>
<a name="index-_003e"></a>
<a name="index-_003e_003d"></a>
<a name="index-_003d_003d"></a>
<a name="index-_0021_003d"></a>
In addition if <code>c1</code> and <code>c2</code> are logical expressions, then
<code>c1&nbsp;&amp;&nbsp;c2</code><!-- /@w --> is their intersection (<em>&ldquo;and&rdquo;</em>), <code>c1&nbsp;|&nbsp;c2</code><!-- /@w -->
is their union (<em>&ldquo;or&rdquo;</em>), and <code>!c1</code> is the negation of
<code>c1</code>.
<a name="index-_0021"></a>
<a name="index-_007c"></a>
<a name="index-_0026"></a>
</p>
<p>Logical vectors may be used in ordinary arithmetic, in which case they
are <em>coerced</em> into numeric vectors, <code>FALSE</code> becoming <code>0</code>
and <code>TRUE</code> becoming <code>1</code>.  However there are situations where
logical vectors and their coerced numeric counterparts are not
equivalent, for example see the next subsection.
</p>
<hr>
<a name="Missing-values"></a>
<div class="header">
<p>
Next: <a href="#Character-vectors" accesskey="n" rel="next">Character vectors</a>, Previous: <a href="#Logical-vectors" accesskey="p" rel="prev">Logical vectors</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Missing-values-1"></a>
<h3 class="section">2.5 Missing values</h3>
<a name="index-Missing-values"></a>

<p>In some cases the components of a vector may not be completely
known.  When an element or value is &ldquo;not available&rdquo; or a &ldquo;missing
value&rdquo; in the statistical sense, a place within a vector may be
reserved for it by assigning it the special value <code>NA</code>.
<a name="index-NA"></a>
In general any operation on an <code>NA</code> becomes an <code>NA</code>.  The
motivation for this rule is simply that if the specification of an
operation is incomplete, the result cannot be known and hence is not
available.
</p>
<a name="index-is_002ena"></a>
<p>The function <code>is.na(x)</code> gives a logical vector of the same size as
<code>x</code> with value <code>TRUE</code> if and only if the corresponding element
in <code>x</code> is <code>NA</code>.
</p>
<div class="example">
<pre class="example">&gt; z &lt;- c(1:3,NA);  ind &lt;- is.na(z)
</pre></div>

<p>Notice that the logical expression <code>x == NA</code> is quite different
from <code>is.na(x)</code> since <code>NA</code> is not really a value but a marker
for a quantity that is not available.  Thus <code>x == NA</code> is a vector
of the same length as <code>x</code> <em>all</em> of whose values are <code>NA</code>
as the logical expression itself is incomplete and hence undecidable.
</p>
<p>Note that there is a second kind of &ldquo;missing&rdquo; values which are
produced by numerical computation, the so-called <em>Not a Number</em>,
<code>NaN</code>,
<a name="index-NaN"></a>
values.  Examples are
</p>
<div class="example">
<pre class="example">&gt; 0/0
</pre></div>

<p>or
</p>
<div class="example">
<pre class="example">&gt; Inf - Inf
</pre></div>

<p>which both give <code>NaN</code> since the result cannot be defined sensibly.
</p>
<p>In summary, <code>is.na(xx)</code> is <code>TRUE</code> <em>both</em> for <code>NA</code>
and <code>NaN</code> values.  To differentiate these, <code>is.nan(xx)</code> is only
<code>TRUE</code> for <code>NaN</code>s.
<a name="index-is_002enan"></a>
</p>
<p>Missing values are sometimes printed as <code>&lt;NA&gt;</code> when character
vectors are printed without quotes.
</p>
<hr>
<a name="Character-vectors"></a>
<div class="header">
<p>
Next: <a href="#Index-vectors" accesskey="n" rel="next">Index vectors</a>, Previous: <a href="#Missing-values" accesskey="p" rel="prev">Missing values</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Character-vectors-1"></a>
<h3 class="section">2.6 Character vectors</h3>
<a name="index-Character-vectors"></a>

<p>Character quantities and character vectors are used frequently in R,
for example as plot labels.  Where needed they are denoted by a sequence
of characters delimited by the double quote character, e.g.,
<code>&quot;x-values&quot;</code>, <code>&quot;New iteration results&quot;</code>.
</p>
<p>Character strings are entered using either matching double (<code>&quot;</code>) or
single (<code>'</code>) quotes, but are printed using double quotes (or
sometimes without quotes).  They use C-style escape sequences, using
<code>\</code> as the escape character, so <code>\\</code> is entered and printed as
<code>\\</code>, and inside double quotes <code>&quot;</code> is entered as <code>\&quot;</code>.
Other useful escape sequences are <code>\n</code>, newline, <code>\t</code>, tab and
<code>\b</code>, backspace&mdash;see <code>?Quotes</code> for a full list.
</p>
<p>Character vectors may be concatenated into a vector by the <code>c()</code>
function; examples of their use will emerge frequently.
<a name="index-c-1"></a>
</p>
<a name="index-paste"></a>
<p>The <code>paste()</code> function takes an arbitrary number of arguments and
concatenates them one by one into character strings.  Any numbers given
among the arguments are coerced into character strings in the evident
way, that is, in the same way they would be if they were printed.  The
arguments are by default separated in the result by a single blank
character, but this can be changed by the named argument,
<code>sep=<var>string</var></code>, which changes it to <code><var>string</var></code>,
possibly empty.
</p>
<p>For example
</p>
<div class="example">
<pre class="example">&gt; labs &lt;- paste(c(&quot;X&quot;,&quot;Y&quot;), 1:10, sep=&quot;&quot;)
</pre></div>

<p>makes <code>labs</code> into the character vector
</p>
<div class="example">
<pre class="example">c(&quot;X1&quot;, &quot;Y2&quot;, &quot;X3&quot;, &quot;Y4&quot;, &quot;X5&quot;, &quot;Y6&quot;, &quot;X7&quot;, &quot;Y8&quot;, &quot;X9&quot;, &quot;Y10&quot;)
</pre></div>

<p>Note particularly that recycling of short lists takes place here too;
thus <code>c(&quot;X&quot;, &quot;Y&quot;)</code> is repeated 5 times to match the sequence
<code>1:10</code>.
<a name="DOCF9" href="#FOOT9"><sup>9</sup></a>
</p>
<hr>
<a name="Index-vectors"></a>
<div class="header">
<p>
Next: <a href="#Other-types-of-objects" accesskey="n" rel="next">Other types of objects</a>, Previous: <a href="#Character-vectors" accesskey="p" rel="prev">Character vectors</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Index-vectors_003b-selecting-and-modifying-subsets-of-a-data-set"></a>
<h3 class="section">2.7 Index vectors; selecting and modifying subsets of a data set</h3>
<a name="index-Indexing-vectors"></a>

<p>Subsets of the elements of a vector may be selected by appending to the
name of the vector an <em>index vector</em> in square brackets.  More
generally any expression that evaluates to a vector may have subsets of
its elements similarly selected by appending an index vector in square
brackets immediately after the expression.
</p>

<p>Such index vectors can be any of four distinct types.
</p>
<ol>
<li> <strong>A logical vector</strong>.  In this case the index vector is recycled to the
same length as the vector from which elements are to be selected.
Values corresponding to <code>TRUE</code> in the index vector are selected and
those corresponding to <code>FALSE</code> are omitted.  For example

<div class="example">
<pre class="example">&gt; y &lt;- x[!is.na(x)]
</pre></div>

<p>creates (or re-creates) an object <code>y</code> which will contain the
non-missing values of <code>x</code>, in the same order.  Note that if
<code>x</code> has missing values, <code>y</code> will be shorter than <code>x</code>.
Also
</p>
<div class="example">
<pre class="example">&gt; (x+1)[(!is.na(x)) &amp; x&gt;0] -&gt; z
</pre></div>

<p>creates an object <code>z</code> and places in it the values of the vector
<code>x+1</code> for which the corresponding value in <code>x</code> was both
non-missing and positive.
</p>
</li><li> <strong>A vector of positive integral quantities</strong>.  In this case the
values in the index vector must lie in the set {1, 2, &hellip;,
<code>length(x)</code>}.  The corresponding elements of the vector are
selected and concatenated, <em>in that order</em>, in the result.  The
index vector can be of any length and the result is of the same length
as the index vector.  For example <code>x[6]</code> is the sixth component of
<code>x</code> and

<div class="example">
<pre class="example">&gt; x[1:10]
</pre></div>

<p>selects the first 10 elements of <code>x</code> (assuming <code>length(x)</code> is
not less than 10).  Also
</p>
<div class="example">
<pre class="example">&gt; c(&quot;x&quot;,&quot;y&quot;)[rep(c(1,2,2,1), times=4)]
</pre></div>

<p>(an admittedly unlikely thing to do) produces a character vector of
length 16 consisting of <code>&quot;x&quot;, &quot;y&quot;, &quot;y&quot;, &quot;x&quot;</code> repeated four times.
</p>
</li><li> <strong>A vector of negative integral quantities</strong>.  Such an index vector
specifies the values to be <em>excluded</em> rather than included.  Thus

<div class="example">
<pre class="example">&gt; y &lt;- x[-(1:5)]
</pre></div>

<p>gives <code>y</code> all but the first five elements of <code>x</code>.
</p>
</li><li> <strong>A vector of character strings</strong>.  This possibility only applies
where an object has a <code>names</code> attribute to identify its components.
In this case a sub-vector of the names vector may be used in the same way
as the positive integral labels in item 2 further above.

<div class="example">
<pre class="example">&gt; fruit &lt;- c(5, 10, 1, 20)
&gt; names(fruit) &lt;- c(&quot;orange&quot;, &quot;banana&quot;, &quot;apple&quot;, &quot;peach&quot;)
&gt; lunch &lt;- fruit[c(&quot;apple&quot;,&quot;orange&quot;)]
</pre></div>

<p>The advantage is that alphanumeric <em>names</em> are often easier to
remember than <em>numeric indices</em>.  This option is particularly
useful in connection with data frames, as we shall see later.
</p>
</li></ol>

<p>An indexed expression can also appear on the receiving end of an
assignment, in which case the assignment operation is performed
<em>only on those elements of the vector</em>.  The expression must be of
the form <code>vector[<var>index_vector</var>]</code> as having an arbitrary
expression in place of the vector name does not make much sense here.
</p>
<p>For example
</p>
<div class="example">
<pre class="example">&gt; x[is.na(x)] &lt;- 0
</pre></div>

<p>replaces any missing values in <code>x</code> by zeros and
</p>
<div class="example">
<pre class="example">&gt; y[y &lt; 0] &lt;- -y[y &lt; 0]
</pre></div>

<p>has the same effect as
</p>
<div class="example">
<pre class="example">&gt; y &lt;- abs(y)
</pre></div>

<hr>
<a name="Other-types-of-objects"></a>
<div class="header">
<p>
Previous: <a href="#Index-vectors" accesskey="p" rel="prev">Index vectors</a>, Up: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="u" rel="up">Simple manipulations numbers and vectors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Other-types-of-objects-1"></a>
<h3 class="section">2.8 Other types of objects</h3>

<p>Vectors are the most important type of object in R, but there are
several others which we will meet more formally in later sections.
</p>
<ul>
<li> <em>matrices</em> or more generally <em>arrays</em> are multi-dimensional
generalizations of vectors.  In fact, they <em>are</em> vectors that can
be indexed by two or more indices and will be printed in special ways.
See <a href="#Arrays-and-matrices">Arrays and matrices</a>.

</li><li> <em>factors</em> provide compact ways to handle categorical data.
See <a href="#Factors">Factors</a>.

</li><li> <em>lists</em> are a general form of vector in which the various elements
need not be of the same type, and are often themselves vectors or lists.
Lists provide a convenient way to return the results of a statistical
computation.  See <a href="#Lists">Lists</a>.

</li><li> <em>data frames</em> are matrix-like structures, in which the columns can
be of different types.  Think of data frames as &lsquo;data matrices&rsquo; with one
row per observational unit but with (possibly) both numerical and
categorical variables.  Many experiments are best described by data
frames: the treatments are categorical but the response is numeric.
See <a href="#Data-frames">Data frames</a>.

</li><li> <em>functions</em> are themselves objects in R which can be stored in
the project&rsquo;s workspace.  This provides a simple and convenient way to
extend R.  See <a href="#Writing-your-own-functions">Writing your own functions</a>.

</li></ul>

<hr>
<a name="Objects"></a>
<div class="header">
<p>
Next: <a href="#Factors" accesskey="n" rel="next">Factors</a>, Previous: <a href="#Simple-manipulations-numbers-and-vectors" accesskey="p" rel="prev">Simple manipulations numbers and vectors</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Objects_002c-their-modes-and-attributes"></a>
<h2 class="chapter">3 Objects, their modes and attributes</h2>
<a name="index-Objects"></a>
<a name="index-Attributes"></a>


<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-intrinsic-attributes-mode-and-length" accesskey="1">The intrinsic attributes mode and length</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Changing-the-length-of-an-object" accesskey="2">Changing the length of an object</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Getting-and-setting-attributes" accesskey="3">Getting and setting attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-class-of-an-object" accesskey="4">The class of an object</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-intrinsic-attributes-mode-and-length"></a>
<div class="header">
<p>
Next: <a href="#Changing-the-length-of-an-object" accesskey="n" rel="next">Changing the length of an object</a>, Previous: <a href="#Objects" accesskey="p" rel="prev">Objects</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Intrinsic-attributes_003a-mode-and-length"></a>
<h3 class="section">3.1 Intrinsic attributes: mode and length</h3>

<p>The entities R operates on are technically known as <em>objects</em>.
Examples are vectors of numeric (real) or complex values, vectors of
logical values and vectors of character strings.  These are known as
&ldquo;atomic&rdquo; structures since their components are all of the same type,
or <em>mode</em>, namely <em>numeric</em><a name="DOCF10" href="#FOOT10"><sup>10</sup></a>, <em>complex</em>,
<em>logical</em>, <em>character</em> and <em>raw</em>.
</p>
<p>Vectors must have their values <em>all of the same mode</em>.  Thus any
given vector must be unambiguously either <em>logical</em>,
<em>numeric</em>, <em>complex</em>, <em>character</em> or <em>raw</em>.  (The
only apparent exception to this rule is the special &ldquo;value&rdquo; listed as
<code>NA</code> for quantities not available, but in fact there are several
types of <code>NA</code>).  Note that a vector can be empty and still have a
mode.  For example the empty character string vector is listed as
<code>character(0)</code> and the empty numeric vector as <code>numeric(0)</code>.
</p>
<p>R also operates on objects called <em>lists</em>, which are of mode
<em>list</em>.  These are ordered sequences of objects which individually
can be of any mode.  <em>lists</em> are known as &ldquo;recursive&rdquo; rather than
atomic structures since their components can themselves be lists in
their own right.
</p>
<p>The other recursive structures are those of mode <em>function</em> and
<em>expression</em>.  Functions are the objects that form part of the R
system along with similar user written functions, which we discuss in
some detail later.  Expressions as objects form an
advanced part of R which will not be discussed in this guide, except
indirectly when we discuss <em>formulae</em> used with modeling in R.
</p>
<p>By the <em>mode</em> of an object we mean the basic type of its
fundamental constituents.  This is a special case of a &ldquo;property&rdquo;
of an object.  Another property of every object is its <em>length</em>.  The
functions <code>mode(<var>object</var>)</code> and <code>length(<var>object</var>)</code> can be
used to find out the mode and length of any defined structure
<a name="DOCF11" href="#FOOT11"><sup>11</sup></a>.
</p>
<p>Further properties of an object are usually provided by
<code>attributes(<var>object</var>)</code>, see <a href="#Getting-and-setting-attributes">Getting and setting attributes</a>.
Because of this, <em>mode</em> and <em>length</em> are also called &ldquo;intrinsic
attributes&rdquo; of an object.
<a name="index-mode"></a>
<a name="index-length-1"></a>
</p>
<p>For example, if <code>z</code> is a complex vector of length 100, then in an
expression <code>mode(z)</code> is the character string <code>&quot;complex&quot;</code> and
<code>length(z)</code> is <code>100</code>.
</p>
<p>R caters for changes of mode almost anywhere it could be considered
sensible to do so, (and a few where it might not be).  For example with
</p>
<div class="example">
<pre class="example">&gt; z &lt;- 0:9
</pre></div>

<p>we could put
</p>
<div class="example">
<pre class="example">&gt; digits &lt;- as.character(z)
</pre></div>

<p>after which <code>digits</code> is the character vector <code>c(&quot;0&quot;, &quot;1&quot;, &quot;2&quot;,
&hellip;, &quot;9&quot;)</code>.  A further <em>coercion</em>, or change of mode,
reconstructs the numerical vector again:
</p>
<div class="example">
<pre class="example">&gt; d &lt;- as.integer(digits)
</pre></div>

<p>Now <code>d</code> and <code>z</code> are the same.<a name="DOCF12" href="#FOOT12"><sup>12</sup></a>  There is a
large collection of functions of the form <code>as.<var>something</var>()</code>
for either coercion from one mode to another, or for investing an object
with some other attribute it may not already possess.  The reader should
consult the different help files to become familiar with them.
</p>

<hr>
<a name="Changing-the-length-of-an-object"></a>
<div class="header">
<p>
Next: <a href="#Getting-and-setting-attributes" accesskey="n" rel="next">Getting and setting attributes</a>, Previous: <a href="#The-intrinsic-attributes-mode-and-length" accesskey="p" rel="prev">The intrinsic attributes mode and length</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Changing-the-length-of-an-object-1"></a>
<h3 class="section">3.2 Changing the length of an object</h3>

<p>An &ldquo;empty&rdquo; object may still have a mode.  For example
</p>
<div class="example">
<pre class="example">&gt; e &lt;- numeric()
</pre></div>

<p>makes <code>e</code> an empty vector structure of mode numeric.  Similarly
<code>character()</code> is a empty character vector, and so on.  Once an
object of any size has been created, new components may be added to it
simply by giving it an index value outside its previous range.  Thus
</p>
<div class="example">
<pre class="example">&gt; e[3] &lt;- 17
</pre></div>

<p>now makes <code>e</code> a vector of length 3, (the first two components of
which are at this point both <code>NA</code>).  This applies to any structure
at all, provided the mode of the additional component(s) agrees with the
mode of the object in the first place.
</p>
<p>This automatic adjustment of lengths of an object is used often, for
example in the <code>scan()</code> function for input.  (see <a href="#The-scan_0028_0029-function">The scan() function</a>.)
</p>
<p>Conversely to truncate the size of an object requires only an assignment
to do so.  Hence if <code>alpha</code> is an object of length 10, then
</p>
<div class="example">
<pre class="example">&gt; alpha &lt;- alpha[2 * 1:5]
</pre></div>

<p>makes it an object of length 5 consisting of just the former components
with even index.  (The old indices are not retained, of course.)  We can
then retain just the first three values by
</p>
<div class="example">
<pre class="example">&gt; length(alpha) &lt;- 3
</pre></div>

<p>and vectors can be extended (by missing values) in the same way.
</p>
<hr>
<a name="Getting-and-setting-attributes"></a>
<div class="header">
<p>
Next: <a href="#The-class-of-an-object" accesskey="n" rel="next">The class of an object</a>, Previous: <a href="#Changing-the-length-of-an-object" accesskey="p" rel="prev">Changing the length of an object</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Getting-and-setting-attributes-1"></a>
<h3 class="section">3.3 Getting and setting attributes</h3>
<a name="index-attr"></a>
<a name="index-attributes"></a>

<p>The function <code>attributes(<var>object</var>)</code>
<a name="index-attributes-1"></a>
returns a list of all the non-intrinsic attributes currently defined for
that object.  The function <code>attr(<var>object</var>, <var>name</var>)</code>
<a name="index-attr-1"></a>
can be used to select a specific attribute.  These functions are rarely
used, except in rather special circumstances when some new attribute is
being created for some particular purpose, for example to associate a
creation date or an operator with an R object.  The concept, however,
is very important.
</p>
<p>Some care should be exercised when assigning or deleting attributes
since they are an integral part of the object system used in R.
</p>
<p>When it is used on the left hand side of an assignment it can be used
either to associate a new attribute with <code><var>object</var></code> or to
change an existing one.  For example
</p>
<div class="example">
<pre class="example">&gt; attr(z, &quot;dim&quot;) &lt;- c(10,10)
</pre></div>

<p>allows R to treat <code>z</code> as if it were a 10-by-10 matrix.
</p>
<hr>
<a name="The-class-of-an-object"></a>
<div class="header">
<p>
Previous: <a href="#Getting-and-setting-attributes" accesskey="p" rel="prev">Getting and setting attributes</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-class-of-an-object-1"></a>
<h3 class="section">3.4 The class of an object</h3>
<a name="index-Classes"></a>

<p>All objects in R have a <em>class</em>, reported by the function
<code>class</code>.  For simple vectors this is just the mode, for example
<code>&quot;numeric&quot;</code>, <code>&quot;logical&quot;</code>, <code>&quot;character&quot;</code> or <code>&quot;list&quot;</code>,
but <code>&quot;matrix&quot;</code>, <code>&quot;array&quot;</code>, <code>&quot;factor&quot;</code> and
<code>&quot;data.frame&quot;</code> are other possible values.
</p>
<p>A special attribute known as the <em>class</em> of the object is used to
allow for an object-oriented style<a name="DOCF13" href="#FOOT13"><sup>13</sup></a> of
programming in R.  For example if an object has class
<code>&quot;data.frame&quot;</code>, it will be printed in a certain way, the
<code>plot()</code> function will display it graphically in a certain way, and
other so-called generic functions such as <code>summary()</code> will react to
it as an argument in a way sensitive to its class.
</p>
<p>To remove temporarily the effects of class, use the function
<code>unclass()</code>.
<a name="index-unclass"></a>
For example if <code>winter</code> has the class <code>&quot;data.frame&quot;</code> then
</p>
<div class="example">
<pre class="example">&gt; winter
</pre></div>

<p>will print it in data frame form, which is rather like a matrix, whereas
</p>
<div class="example">
<pre class="example">&gt; unclass(winter)
</pre></div>

<p>will print it as an ordinary list.  Only in rather special situations do
you need to use this facility, but one is when you are learning to come
to terms with the idea of class and generic functions.
</p>
<p>Generic functions and classes will be discussed further in <a href="#Object-orientation">Object orientation</a>, but only briefly.
</p>
<hr>
<a name="Factors"></a>
<div class="header">
<p>
Next: <a href="#Arrays-and-matrices" accesskey="n" rel="next">Arrays and matrices</a>, Previous: <a href="#Objects" accesskey="p" rel="prev">Objects</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Ordered-and-unordered-factors"></a>
<h2 class="chapter">4 Ordered and unordered factors</h2>
<a name="index-Factors"></a>
<a name="index-Ordered-factors"></a>

<p>A <em>factor</em> is a vector object used to specify a discrete
classification (grouping) of the components of other vectors of the same length.
R provides both <em>ordered</em> and <em>unordered</em> factors.
While the &ldquo;real&rdquo; application of factors is with model formulae
(see <a href="#Contrasts">Contrasts</a>), we here look at a specific example.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-function-tapply_0028_0029-and-ragged-arrays" accesskey="1">The function tapply() and ragged arrays</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Ordered-factors" accesskey="2">Ordered factors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<a name="A-specific-example"></a>
<h3 class="section">4.1 A specific example</h3>

<p>Suppose, for example, we have a sample of 30 tax accountants from all
the states and territories of Australia<a name="DOCF14" href="#FOOT14"><sup>14</sup></a>
and their individual state of origin is specified by a character vector
of state mnemonics as
</p>
<div class="example">
<pre class="example">&gt; state &lt;- c(&quot;tas&quot;, &quot;sa&quot;,  &quot;qld&quot;, &quot;nsw&quot;, &quot;nsw&quot;, &quot;nt&quot;,  &quot;wa&quot;,  &quot;wa&quot;,
             &quot;qld&quot;, &quot;vic&quot;, &quot;nsw&quot;, &quot;vic&quot;, &quot;qld&quot;, &quot;qld&quot;, &quot;sa&quot;,  &quot;tas&quot;,
             &quot;sa&quot;,  &quot;nt&quot;,  &quot;wa&quot;,  &quot;vic&quot;, &quot;qld&quot;, &quot;nsw&quot;, &quot;nsw&quot;, &quot;wa&quot;,
             &quot;sa&quot;,  &quot;act&quot;, &quot;nsw&quot;, &quot;vic&quot;, &quot;vic&quot;, &quot;act&quot;)
</pre></div>

<p>Notice that in the case of a character vector, &ldquo;sorted&rdquo; means sorted
in alphabetical order.
</p>
<p>A <em>factor</em> is similarly created using the <code>factor()</code> function:
<a name="index-factor"></a>
</p>
<div class="example">
<pre class="example">&gt; statef &lt;- factor(state)
</pre></div>

<p>The <code>print()</code> function handles factors slightly differently from
other objects:
</p>
<div class="example">
<pre class="example">&gt; statef
 [1] tas sa  qld nsw nsw nt  wa  wa  qld vic nsw vic qld qld sa
[16] tas sa  nt  wa  vic qld nsw nsw wa  sa  act nsw vic vic act
Levels:  act nsw nt qld sa tas vic wa
</pre></div>

<p>To find out the levels of a factor the function <code>levels()</code> can be
used.
<a name="index-levels"></a>
</p>
<div class="example">
<pre class="example">&gt; levels(statef)
[1] &quot;act&quot; &quot;nsw&quot; &quot;nt&quot;  &quot;qld&quot; &quot;sa&quot;  &quot;tas&quot; &quot;vic&quot; &quot;wa&quot;
</pre></div>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-function-tapply_0028_0029-and-ragged-arrays" accesskey="1">The function tapply() and ragged arrays</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Ordered-factors" accesskey="2">Ordered factors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-function-tapply_0028_0029-and-ragged-arrays"></a>
<div class="header">
<p>
Next: <a href="#Ordered-factors" accesskey="n" rel="next">Ordered factors</a>, Previous: <a href="#Factors" accesskey="p" rel="prev">Factors</a>, Up: <a href="#Factors" accesskey="u" rel="up">Factors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-function-tapply_0028_0029-and-ragged-arrays-1"></a>
<h3 class="section">4.2 The function <code>tapply()</code> and ragged arrays</h3>
<a name="index-tapply"></a>

<p>To continue the previous example, suppose we have the incomes of the
same tax accountants in another vector (in suitably large units of
money)
</p>
<div class="example">
<pre class="example">&gt; incomes &lt;- c(60, 49, 40, 61, 64, 60, 59, 54, 62, 69, 70, 42, 56,
               61, 61, 61, 58, 51, 48, 65, 49, 49, 41, 48, 52, 46,
               59, 46, 58, 43)
</pre></div>

<p>To calculate the sample mean income for each state we can now use the
special function <code>tapply()</code>:
</p>
<div class="example">
<pre class="example">&gt; incmeans &lt;- tapply(incomes, statef, mean)
</pre></div>

<p>giving a means vector with the components labelled by the levels
</p>
<div class="example">
<pre class="example">   act    nsw     nt    qld     sa    tas    vic     wa
44.500 57.333 55.500 53.600 55.000 60.500 56.000 52.250
</pre></div>

<p>The function <code>tapply()</code> is used to apply a function, here
<code>mean()</code>, to each group of components of the first argument, here
<code>incomes</code>, defined by the levels of the second component, here
<code>statef</code><a name="DOCF15" href="#FOOT15"><sup>15</sup></a>, as if they were separate vector
structures.  The result is a structure of the same length as the levels
attribute of the factor containing the results.  The reader should
consult the help document for more details.
</p>
<p>Suppose further we needed to calculate the standard errors of the state
income means.  To do this we need to write an R function to calculate
the standard error for any given vector.  Since there is an builtin
function <code>var()</code> to calculate the sample variance, such a function
is a very simple one liner, specified by the assignment:
</p>
<div class="example">
<pre class="example">&gt; stdError &lt;- function(x) sqrt(var(x)/length(x))
</pre></div>

<p>(Writing functions will be considered later in <a href="#Writing-your-own-functions">Writing your own functions</a>.  Note that R&rsquo;s a builtin function <code>sd()</code> is something different.)
<a name="index-sd"></a>
<a name="index-var-1"></a>
After this assignment, the standard errors are calculated by
</p>
<div class="example">
<pre class="example">&gt; incster &lt;- tapply(incomes, statef, stdError)
</pre></div>

<p>and the values calculated are then
</p>
<div class="example">
<pre class="example">&gt; incster
act    nsw  nt    qld     sa tas   vic     wa
1.5 4.3102 4.5 4.1061 2.7386 0.5 5.244 2.6575
</pre></div>

<p>As an exercise you may care to find the usual 95% confidence limits for
the state mean incomes.  To do this you could use <code>tapply()</code> once
more with the <code>length()</code> function to find the sample sizes, and the
<code>qt()</code> function to find the percentage points of the appropriate
<em>t</em>-distributions.  (You could also investigate R&rsquo;s facilities
for <em>t</em>-tests.)
</p>
<p>The function <code>tapply()</code> can also be used to handle more complicated
indexing of a vector by multiple categories.  For example, we might wish
to split the tax accountants by both state and sex.  However in this
simple instance (just one factor) what happens can be thought of as
follows.  The values in the vector are collected into groups
corresponding to the distinct entries in the factor.  The function is
then applied to each of these groups individually.  The value is a
vector of function results, labelled by the <code>levels</code> attribute of
the factor.
</p>
<p>The combination of a vector and a labelling factor is an example of what
is sometimes called a <em>ragged array</em>, since the subclass sizes are
possibly irregular.  When the subclass sizes are all the same the
indexing may be done implicitly and much more efficiently, as we see in
the next section.
</p>

<hr>
<a name="Ordered-factors"></a>
<div class="header">
<p>
Previous: <a href="#The-function-tapply_0028_0029-and-ragged-arrays" accesskey="p" rel="prev">The function tapply() and ragged arrays</a>, Up: <a href="#Factors" accesskey="u" rel="up">Factors</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Ordered-factors-1"></a>
<h3 class="section">4.3 Ordered factors</h3>
<a name="index-ordered"></a>

<p>The levels of factors are stored in alphabetical order, or in the order
they were specified to <code>factor</code> if they were specified explicitly.
</p>
<p>Sometimes the levels will have a natural ordering that we want to record
and want our statistical analysis to make use of.  The <code>ordered()</code>
<a name="index-ordered-1"></a>
function creates such ordered factors but is otherwise identical to
<code>factor</code>.  For most purposes the only difference between ordered
and unordered factors is that the former are printed showing the
ordering of the levels, but the contrasts generated for them in fitting
linear models are different.
</p>

<hr>
<a name="Arrays-and-matrices"></a>
<div class="header">
<p>
Next: <a href="#Lists-and-data-frames" accesskey="n" rel="next">Lists and data frames</a>, Previous: <a href="#Factors" accesskey="p" rel="prev">Factors</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Arrays-and-matrices-1"></a>
<h2 class="chapter">5 Arrays and matrices</h2>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Arrays" accesskey="1">Arrays</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Array-indexing" accesskey="2">Array indexing</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Index-matrices" accesskey="3">Index matrices</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-array_0028_0029-function" accesskey="4">The array() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-outer-product-of-two-arrays" accesskey="5">The outer product of two arrays</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Generalized-transpose-of-an-array" accesskey="6">Generalized transpose of an array</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Matrix-facilities" accesskey="7">Matrix facilities</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Forming-partitioned-matrices" accesskey="8">Forming partitioned matrices</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-concatenation-function-c_0028_0029-with-arrays" accesskey="9">The concatenation function c() with arrays</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Frequency-tables-from-factors">Frequency tables from factors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Arrays"></a>
<div class="header">
<p>
Next: <a href="#Array-indexing" accesskey="n" rel="next">Array indexing</a>, Previous: <a href="#Arrays-and-matrices" accesskey="p" rel="prev">Arrays and matrices</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Arrays-1"></a>
<h3 class="section">5.1 Arrays</h3>
<a name="index-Arrays"></a>
<a name="index-Matrices"></a>

<p>An array can be considered as a multiply subscripted collection of data
entries, for example numeric.  R allows simple facilities for
creating and handling arrays, and in particular the special case of
matrices.
</p>
<p>A dimension vector is a vector of non-negative integers.  If its length is
<em>k</em> then the array is <em>k</em>-dimensional, e.g.&nbsp;a matrix is a
<em>2</em>-dimensional array.  The dimensions are indexed from one up to
the values given in the dimension vector.
</p>
<p>A vector can be used by R as an array only if it has a dimension
vector as its <em>dim</em> attribute.  Suppose, for example, <code>z</code> is a
vector of 1500 elements.  The assignment
</p>
<div class="example">
<pre class="example">&gt; dim(z) &lt;- c(3,5,100)
</pre></div>
<a name="index-dim"></a>

<p>gives it the <em>dim</em> attribute that allows it to be treated as a
<em>3</em> by <em>5</em> by <em>100</em> array.
</p>
<p>Other functions such as <code>matrix()</code> and <code>array()</code> are available
for simpler and more natural looking assignments, as we shall see in
<a href="#The-array_0028_0029-function">The array() function</a>.
</p>
<p>The values in the data vector give the values in the array in the same
order as they would occur in FORTRAN, that is &ldquo;column major order,&rdquo;
with the first subscript moving fastest and the last subscript slowest.
</p>
<p>For example if the dimension vector for an array, say <code>a</code>, is
<code>c(3,4,2)</code> then there are 3 * 4 * 2
= 24 entries in <code>a</code> and the data vector holds them in the order
<code>a[1,1,1], a[2,1,1], &hellip;, a[2,4,2], a[3,4,2]</code>.
</p>
<p>Arrays can be one-dimensional: such arrays are usually treated in the
same way as vectors (including when printing), but the exceptions can
cause confusion.
</p>
<hr>
<a name="Array-indexing"></a>
<div class="header">
<p>
Next: <a href="#Index-matrices" accesskey="n" rel="next">Index matrices</a>, Previous: <a href="#Arrays" accesskey="p" rel="prev">Arrays</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Array-indexing_002e-Subsections-of-an-array"></a>
<h3 class="section">5.2 Array indexing.  Subsections of an array</h3>
<a name="index-Indexing-of-and-by-arrays"></a>

<p>Individual elements of an array may be referenced by giving the name of
the array followed by the subscripts in square brackets, separated by
commas.
</p>
<p>More generally, subsections of an array may be specified by giving a
sequence of <em>index vectors</em> in place of subscripts; however
<em>if any index position is given an empty index vector, then the
full range of that subscript is taken</em>.
</p>
<p>Continuing the previous example, <code>a[2,,]</code> is a 4 *
2 array with dimension vector <code>c(4,2)</code> and data vector containing
the values
</p>
<div class="example">
<pre class="example">c(a[2,1,1], a[2,2,1], a[2,3,1], a[2,4,1],
  a[2,1,2], a[2,2,2], a[2,3,2], a[2,4,2])
</pre></div>

<p>in that order.  <code>a[,,]</code> stands for the entire array, which is the
same as omitting the subscripts entirely and using <code>a</code> alone.
</p>
<p>For any array, say <code>Z</code>, the dimension vector may be referenced
explicitly as <code>dim(Z)</code> (on either side of an assignment).
</p>
<p>Also, if an array name is given with just <em>one subscript or index
vector</em>, then the corresponding values of the data vector only are used;
in this case the dimension vector is ignored.  This is not the case,
however, if the single index is not a vector but itself an array, as we
next discuss.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Index-matrices" accesskey="1">Index matrices</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-array_0028_0029-function" accesskey="2">The array() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Index-matrices"></a>
<div class="header">
<p>
Next: <a href="#The-array_0028_0029-function" accesskey="n" rel="next">The array() function</a>, Previous: <a href="#Array-indexing" accesskey="p" rel="prev">Array indexing</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Index-matrices-1"></a>
<h3 class="section">5.3 Index matrices</h3>

<p>As well as an index vector in any subscript position, a matrix may be
used with a single <em>index matrix</em> in order either to assign a vector
of quantities to an irregular collection of elements in the array, or to
extract an irregular collection as a vector.
</p>
<p>A matrix example makes the process clear.  In the case of a doubly
indexed array, an index matrix may be given consisting of two columns
and as many rows as desired.  The entries in the index matrix are the
row and column indices for the doubly indexed array.  Suppose for
example we have a <em>4</em> by <em>5</em> array <code>X</code> and we wish to do
the following:
</p>
<ul>
<li> Extract elements <code>X[1,3]</code>, <code>X[2,2]</code> and <code>X[3,1]</code> as a
vector structure, and
</li><li> Replace these entries in the array <code>X</code> by zeroes.
</li></ul>
<p>In this case we need a <em>3</em> by <em>2</em> subscript array, as in the
following example.
</p>
<div class="example">
<pre class="example">&gt; x &lt;- array(1:20, dim=c(4,5))   # <span class="roman">Generate a 4 by 5 array.</span>
&gt; x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20
&gt; i &lt;- array(c(1:3,3:1), dim=c(3,2))
&gt; i                             # <span class="roman"><code>i</code> is a 3 by 2 index array.</span>
     [,1] [,2]
[1,]    1    3
[2,]    2    2
[3,]    3    1
&gt; x[i]                          # <span class="roman">Extract those elements</span>
[1] 9 6 3
&gt; x[i] &lt;- 0                     # <span class="roman">Replace those elements by zeros.</span>
&gt; x
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    0   13   17
[2,]    2    0   10   14   18
[3,]    0    7   11   15   19
[4,]    4    8   12   16   20
&gt;
</pre></div>
<p>Negative indices are not allowed in index matrices.  <code>NA</code> and zero
values are allowed: rows in the index matrix containing a zero are
ignored, and rows containing an <code>NA</code> produce an <code>NA</code> in the
result.
</p>

<p>As a less trivial example, suppose we wish to generate an (unreduced)
design matrix for a block design defined by factors <code>blocks</code>
(<code>b</code> levels) and <code>varieties</code> (<code>v</code> levels).  Further
suppose there are <code>n</code> plots in the experiment.  We could proceed as
follows:
</p>
<div class="example">
<pre class="example">&gt; Xb &lt;- matrix(0, n, b)
&gt; Xv &lt;- matrix(0, n, v)
&gt; ib &lt;- cbind(1:n, blocks)
&gt; iv &lt;- cbind(1:n, varieties)
&gt; Xb[ib] &lt;- 1
&gt; Xv[iv] &lt;- 1
&gt; X &lt;- cbind(Xb, Xv)
</pre></div>

<p>To construct the incidence matrix, <code>N</code> say, we could use
</p>
<div class="example">
<pre class="example">&gt; N &lt;- crossprod(Xb, Xv)
</pre></div>
<a name="index-crossprod"></a>

<p>However a simpler direct way of producing this matrix is to use
<code>table()</code>:
<a name="index-table"></a>
</p>
<div class="example">
<pre class="example">&gt; N &lt;- table(blocks, varieties)
</pre></div>

<p>Index matrices must be numerical: any other form of matrix (e.g.&nbsp;a
logical or character matrix) supplied as a matrix is treated as an
indexing vector.
</p>
<hr>
<a name="The-array_0028_0029-function"></a>
<div class="header">
<p>
Next: <a href="#The-outer-product-of-two-arrays" accesskey="n" rel="next">The outer product of two arrays</a>, Previous: <a href="#Index-matrices" accesskey="p" rel="prev">Index matrices</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-array_0028_0029-function-1"></a>
<h3 class="section">5.4 The <code>array()</code> function</h3>
<a name="index-array"></a>

<p>As well as giving a vector structure a <code>dim</code> attribute, arrays can
be constructed from vectors by the <code>array</code> function, which has the
form
</p>
<div class="example">
<pre class="example">&gt; Z &lt;- array(<var>data_vector</var>, <var>dim_vector</var>)
</pre></div>

<p>For example, if the vector <code>h</code> contains 24 or fewer, numbers then
the command
</p>
<div class="example">
<pre class="example">&gt; Z &lt;- array(h, dim=c(3,4,2))
</pre></div>

<p>would use <code>h</code> to set up <em>3</em> by <em>4</em> by <em>2</em> array in
<code>Z</code>.  If the size of <code>h</code> is exactly 24 the result is the same as
</p>
<div class="example">
<pre class="example">&gt; Z &lt;- h ; dim(Z) &lt;- c(3,4,2)
</pre></div>

<p>However if <code>h</code> is shorter than 24, its values are recycled from the
beginning again to make it up to size 24 (see <a href="#The-recycling-rule">The recycling rule</a>)
but <code>dim(h) &lt;- c(3,4,2)</code> would signal an error about mismatching
length.
As an extreme but common example
</p>
<div class="example">
<pre class="example">&gt; Z &lt;- array(0, c(3,4,2))
</pre></div>

<p>makes <code>Z</code> an array of all zeros.
</p>
<p>At this point <code>dim(Z)</code> stands for the dimension vector
<code>c(3,4,2)</code>, and <code>Z[1:24]</code> stands for the data vector as it was
in <code>h</code>, and <code>Z[]</code> with an empty subscript or <code>Z</code> with no
subscript stands for the entire array as an array.
</p>
<p>Arrays may be used in arithmetic expressions and the result is an array
formed by element-by-element operations on the data vector.  The
<code>dim</code> attributes of operands generally need to be the same, and
this becomes the dimension vector of the result.  So if <code>A</code>,
<code>B</code> and <code>C</code> are all similar arrays, then
</p>
<div class="example">
<pre class="example">&gt; D &lt;- 2*A*B + C + 1
</pre></div>

<p>makes <code>D</code> a similar array with its data vector being the result of
the given element-by-element operations.  However the precise rule
concerning mixed array and vector calculations has to be considered a
little more carefully.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-recycling-rule" accesskey="1">The recycling rule</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-recycling-rule"></a>
<div class="header">
<p>
Previous: <a href="#The-array_0028_0029-function" accesskey="p" rel="prev">The array() function</a>, Up: <a href="#The-array_0028_0029-function" accesskey="u" rel="up">The array() function</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Mixed-vector-and-array-arithmetic_002e-The-recycling-rule"></a>
<h4 class="subsection">5.4.1 Mixed vector and array arithmetic.  The recycling rule</h4>
<a name="index-Recycling-rule-1"></a>

<p>The precise rule affecting element by element mixed calculations with
vectors and arrays is somewhat quirky and hard to find in the
references.  From experience we have found the following to be a reliable
guide.
</p>
<ul>
<li> The expression is scanned from left to right.
</li><li> Any short vector operands are extended by recycling their values until
they match the size of any other operands.
</li><li> As long as short vectors and arrays <em>only</em> are encountered, the
arrays must all have the same <code>dim</code> attribute or an error results.
</li><li> Any vector operand longer than a matrix or array operand generates an error.
</li><li> If array structures are present and no error or coercion to vector has
been precipitated, the result is an array structure with the common
<code>dim</code> attribute of its array operands.
</li></ul>

<hr>
<a name="The-outer-product-of-two-arrays"></a>
<div class="header">
<p>
Next: <a href="#Generalized-transpose-of-an-array" accesskey="n" rel="next">Generalized transpose of an array</a>, Previous: <a href="#The-array_0028_0029-function" accesskey="p" rel="prev">The array() function</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-outer-product-of-two-arrays-1"></a>
<h3 class="section">5.5 The outer product of two arrays</h3>
<a name="index-Outer-products-of-arrays"></a>

<p>An important operation on arrays is the <em>outer product</em>.  If
<code>a</code> and <code>b</code> are two numeric arrays, their outer product is an
array whose dimension vector is obtained by concatenating their two
dimension vectors (order is important), and whose data vector is got by
forming all possible products of elements of the data vector of <code>a</code>
with those of <code>b</code>.  The outer product is formed by the special
operator <code>%o%</code>:
<a name="index-_0025o_0025"></a>
</p>
<div class="example">
<pre class="example">&gt; ab &lt;- a %o% b
</pre></div>

<p>An alternative is
</p>
<div class="example">
<pre class="example">&gt; ab &lt;- outer(a, b, &quot;*&quot;)
</pre></div>
<a name="index-outer"></a>

<p>The multiplication function can be replaced by an arbitrary function of
two variables.  For example if we wished to evaluate the function
f(x; y) = cos(y)/(1 + x^2)
over a regular grid of values with <em>x</em>- and <em>y</em>-coordinates
defined by the R vectors <code>x</code> and <code>y</code> respectively, we could
proceed as follows:
</p>
<div class="example">
<pre class="example">&gt; f &lt;- function(x, y) cos(y)/(1 + x^2)
&gt; z &lt;- outer(x, y, f)
</pre></div>

<p>In particular the outer product of two ordinary vectors is a doubly
subscripted array (that is a matrix, of rank at most 1).  Notice that
the outer product operator is of course non-commutative.  Defining your
own R functions will be considered further in <a href="#Writing-your-own-functions">Writing your own functions</a>.
</p>
<a name="An-example_003a-Determinants-of-2-by-2-single_002ddigit-matrices"></a>
<h4 class="subsubheading">An example: Determinants of 2 by 2 single-digit matrices</h4>

<p>As an artificial but cute example, consider the determinants of <em>2</em>
by <em>2</em> matrices <em>[a, b; c, d]</em> where each entry is a
non-negative integer in the range <em>0, 1, &hellip;, 9</em>, that is a
digit.
</p>
<p>The problem is to find the determinants, <em>ad - bc</em>, of all possible
matrices of this form and represent the frequency with which each value
occurs as a <em>high density</em> plot.  This amounts to finding the
probability distribution of the determinant if each digit is chosen
independently and uniformly at random.
</p>
<p>A neat way of doing this uses the <code>outer()</code> function twice:
</p>
<div class="example">
<pre class="example">&gt; d &lt;- outer(0:9, 0:9)
&gt; fr &lt;- table(outer(d, d, &quot;-&quot;))
&gt; plot(fr, xlab=&quot;Determinant&quot;, ylab=&quot;Frequency&quot;)
</pre></div>

<p>Notice that <code>plot()</code> here uses a histogram like plot method, because
it &ldquo;sees&rdquo; that <code>fr</code> is of class <code>&quot;table&quot;</code>.
The &ldquo;obvious&rdquo; way of doing this problem with <code>for</code> loops, to be
discussed in <a href="#Loops-and-conditional-execution">Loops and conditional execution</a>, is so inefficient as
to be impractical.
</p>
<p>It is also perhaps surprising that about 1 in 20 such matrices is
singular.
</p>
<hr>
<a name="Generalized-transpose-of-an-array"></a>
<div class="header">
<p>
Next: <a href="#Matrix-facilities" accesskey="n" rel="next">Matrix facilities</a>, Previous: <a href="#The-outer-product-of-two-arrays" accesskey="p" rel="prev">The outer product of two arrays</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Generalized-transpose-of-an-array-1"></a>
<h3 class="section">5.6 Generalized transpose of an array</h3>
<a name="index-Generalized-transpose-of-an-array"></a>

<p>The function <code>aperm(a, perm)</code>
<a name="index-aperm"></a>
may be used to permute an array, <code>a</code>.  The argument <code>perm</code>
must be a permutation of the integers <em>{1, &hellip;, k}</em>, where
<em>k</em> is the number of subscripts in <code>a</code>.  The result of the
function is an array of the same size as <code>a</code> but with old dimension
given by <code>perm[j]</code> becoming the new <code>j</code>-th dimension.  The
easiest way to think of this operation is as a generalization of
transposition for matrices.  Indeed if <code>A</code> is a matrix, (that is, a
doubly subscripted array) then <code>B</code> given by
</p>
<div class="example">
<pre class="example">&gt; B &lt;- aperm(A, c(2,1))
</pre></div>

<p>is just the transpose of <code>A</code>.  For this special case a simpler
function <code>t()</code>
<a name="index-t"></a>
is available, so we could have used <code>B &lt;- t(A)</code>.
</p>
<hr>
<a name="Matrix-facilities"></a>
<div class="header">
<p>
Next: <a href="#Forming-partitioned-matrices" accesskey="n" rel="next">Forming partitioned matrices</a>, Previous: <a href="#Generalized-transpose-of-an-array" accesskey="p" rel="prev">Generalized transpose of an array</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Matrix-facilities-1"></a>
<h3 class="section">5.7 Matrix facilities</h3>



<p>As noted above, a matrix is just an array with two subscripts.  However
it is such an important special case it needs a separate discussion.
R contains many operators and functions that are available only for
matrices.  For example <code>t(X)</code> is the matrix transpose function, as
noted above.  The functions <code>nrow(A)</code> and <code>ncol(A)</code> give the
number of rows and columns in the matrix <code>A</code> respectively.
<a name="index-nrow"></a>
<a name="index-ncol"></a>
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Multiplication" accesskey="1">Multiplication</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Linear-equations-and-inversion" accesskey="2">Linear equations and inversion</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Eigenvalues-and-eigenvectors" accesskey="3">Eigenvalues and eigenvectors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Singular-value-decomposition-and-determinants" accesskey="4">Singular value decomposition and determinants</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Least-squares-fitting-and-the-QR-decomposition" accesskey="5">Least squares fitting and the QR decomposition</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Multiplication"></a>
<div class="header">
<p>
Next: <a href="#Linear-equations-and-inversion" accesskey="n" rel="next">Linear equations and inversion</a>, Previous: <a href="#Matrix-facilities" accesskey="p" rel="prev">Matrix facilities</a>, Up: <a href="#Matrix-facilities" accesskey="u" rel="up">Matrix facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Matrix-multiplication"></a>
<h4 class="subsection">5.7.1 Matrix multiplication</h4>

<a name="index-Matrix-multiplication"></a>
<p>The operator <code>%*%</code> is used for matrix multiplication.
<a name="index-_0025_002a_0025"></a>
An <em>n</em> by <em>1</em> or <em>1</em> by <em>n</em> matrix may of course be
used as an <em>n</em>-vector if in the context such is appropriate.
Conversely, vectors which occur in matrix multiplication expressions are
automatically promoted either to row or column vectors, whichever is
multiplicatively coherent, if possible, (although this is not always
unambiguously possible, as we see later).
</p>
<p>If, for example, <code>A</code> and <code>B</code> are square matrices of the same
size, then
</p>
<div class="example">
<pre class="example">&gt; A * B
</pre></div>

<p>is the matrix of element by element products and
</p>
<div class="example">
<pre class="example">&gt; A %*% B
</pre></div>

<p>is the matrix product.  If <code>x</code> is a vector, then
</p>
<div class="example">
<pre class="example">&gt; x %*% A %*% x
</pre></div>

<p>is a quadratic form.<a name="DOCF16" href="#FOOT16"><sup>16</sup></a>
</p>
<a name="index-crossprod-1"></a>
<p>The function <code>crossprod()</code> forms &ldquo;crossproducts&rdquo;, meaning that
<code>crossprod(X, y)</code> is the same as <code>t(X) %*% y</code> but the
operation is more efficient.  If the second argument to
<code>crossprod()</code> is omitted it is taken to be the same as the first.
</p>
<a name="index-diag"></a>
<p>The meaning of <code>diag()</code> depends on its argument.  <code>diag(v)</code>,
where <code>v</code> is a vector, gives a diagonal matrix with elements of the
vector as the diagonal entries.  On the other hand <code>diag(M)</code>, where
<code>M</code> is a matrix, gives the vector of main diagonal entries of
<code>M</code>.  This is the same convention as that used for <code>diag()</code> in
<small>MATLAB</small>.  Also, somewhat confusingly, if <code>k</code> is a single
numeric value then <code>diag(k)</code> is the <code>k</code> by <code>k</code> identity
matrix!
</p>
<hr>
<a name="Linear-equations-and-inversion"></a>
<div class="header">
<p>
Next: <a href="#Eigenvalues-and-eigenvectors" accesskey="n" rel="next">Eigenvalues and eigenvectors</a>, Previous: <a href="#Multiplication" accesskey="p" rel="prev">Multiplication</a>, Up: <a href="#Matrix-facilities" accesskey="u" rel="up">Matrix facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Linear-equations-and-inversion-1"></a>
<h4 class="subsection">5.7.2 Linear equations and inversion</h4>

<a name="index-Linear-equations"></a>
<a name="index-solve"></a>
<p>Solving linear equations is the inverse of matrix multiplication.
When after
</p>
<div class="example">
<pre class="example">&gt; b &lt;- A %*% x
</pre></div>

<p>only <code>A</code> and <code>b</code> are given, the vector <code>x</code> is the
solution of that linear equation system.  In R,
</p>
<div class="example">
<pre class="example">&gt; solve(A,b)
</pre></div>

<p>solves the system, returning <code>x</code> (up to some accuracy loss).
Note that in linear algebra, formally
<code>x = A^{-1} %*% b</code>
where
<code>A^{-1}</code> denotes the <em>inverse</em> of
<code>A</code>, which can be computed by
</p>
<div class="example">
<pre class="example">solve(A)
</pre></div>

<p>but rarely is needed.  Numerically, it is both inefficient and
potentially unstable to compute <code>x &lt;- solve(A) %*% b</code> instead of
<code>solve(A,b)</code>.
</p>
<p>The quadratic form &nbsp;<code>x %*% A^{-1} %*%
x</code> &nbsp; which is used in multivariate computations, should be computed by
something like<a name="DOCF17" href="#FOOT17"><sup>17</sup></a> <code>x %*% solve(A,x)</code>, rather
than computing the inverse of <code>A</code>.
</p>
<hr>
<a name="Eigenvalues-and-eigenvectors"></a>
<div class="header">
<p>
Next: <a href="#Singular-value-decomposition-and-determinants" accesskey="n" rel="next">Singular value decomposition and determinants</a>, Previous: <a href="#Linear-equations-and-inversion" accesskey="p" rel="prev">Linear equations and inversion</a>, Up: <a href="#Matrix-facilities" accesskey="u" rel="up">Matrix facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Eigenvalues-and-eigenvectors-1"></a>
<h4 class="subsection">5.7.3 Eigenvalues and eigenvectors</h4>
<a name="index-Eigenvalues-and-eigenvectors"></a>

<a name="index-eigen"></a>
<p>The function <code>eigen(Sm)</code> calculates the eigenvalues and
eigenvectors of a symmetric matrix <code>Sm</code>.  The result of this
function is a list of two components named <code>values</code> and
<code>vectors</code>.  The assignment
</p>
<div class="example">
<pre class="example">&gt; ev &lt;- eigen(Sm)
</pre></div>

<p>will assign this list to <code>ev</code>.  Then <code>ev$val</code> is the vector of
eigenvalues of <code>Sm</code> and <code>ev$vec</code> is the matrix of
corresponding eigenvectors.  Had we only needed the eigenvalues we could
have used the assignment:
</p>
<div class="example">
<pre class="example">&gt; evals &lt;- eigen(Sm)$values
</pre></div>

<p><code>evals</code> now holds the vector of eigenvalues and the second
component is discarded.  If the expression
</p>
<div class="example">
<pre class="example">&gt; eigen(Sm)
</pre></div>

<p>is used by itself as a command the two components are printed, with
their names.  For large matrices it is better to avoid computing the
eigenvectors if they are not needed by using the expression
</p>
<div class="example">
<pre class="example">&gt; evals &lt;- eigen(Sm, only.values = TRUE)$values
</pre></div>


<hr>
<a name="Singular-value-decomposition-and-determinants"></a>
<div class="header">
<p>
Next: <a href="#Least-squares-fitting-and-the-QR-decomposition" accesskey="n" rel="next">Least squares fitting and the QR decomposition</a>, Previous: <a href="#Eigenvalues-and-eigenvectors" accesskey="p" rel="prev">Eigenvalues and eigenvectors</a>, Up: <a href="#Matrix-facilities" accesskey="u" rel="up">Matrix facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Singular-value-decomposition-and-determinants-1"></a>
<h4 class="subsection">5.7.4 Singular value decomposition and determinants</h4>
<a name="index-Singular-value-decomposition"></a>

<a name="index-svd"></a>
<p>The function <code>svd(M)</code> takes an arbitrary matrix argument, <code>M</code>,
and calculates the singular value decomposition of <code>M</code>.  This
consists of a matrix of orthonormal columns <code>U</code> with the same
column space as <code>M</code>, a second matrix of orthonormal columns
<code>V</code> whose column space is the row space of <code>M</code> and a diagonal
matrix of positive entries <code>D</code> such that <code>M = U %*% D %*%
t(V)</code>.  <code>D</code> is actually returned as a vector of the diagonal
elements.  The result of <code>svd(M)</code> is actually a list of three
components named <code>d</code>, <code>u</code> and <code>v</code>, with evident meanings.
</p>
<p>If <code>M</code> is in fact square, then, it is not hard to see that
</p>
<div class="example">
<pre class="example">&gt; absdetM &lt;- prod(svd(M)$d)
</pre></div>

<p>calculates the absolute value of the determinant of <code>M</code>.  If this
calculation were needed often with a variety of matrices it could be
defined as an R function
</p>
<div class="example">
<pre class="example">&gt; absdet &lt;- function(M) prod(svd(M)$d)
</pre></div>

<a name="index-Determinants"></a>
<p>after which we could use <code>absdet()</code> as just another R function.
As a further trivial but potentially useful example, you might like to
consider writing a function, say <code>tr()</code>, to calculate the trace of
a square matrix.  [Hint: You will not need to use an explicit loop.
Look again at the <code>diag()</code> function.]
</p>
<a name="index-det"></a>
<a name="index-determinant"></a>
<p>R has a builtin function <code>det</code> to calculate a determinant,
including the sign, and another, <code>determinant</code>, to give the sign
and modulus (optionally on log scale),
</p>

<hr>
<a name="Least-squares-fitting-and-the-QR-decomposition"></a>
<div class="header">
<p>
Previous: <a href="#Singular-value-decomposition-and-determinants" accesskey="p" rel="prev">Singular value decomposition and determinants</a>, Up: <a href="#Matrix-facilities" accesskey="u" rel="up">Matrix facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Least-squares-fitting-and-the-QR-decomposition-1"></a>
<h4 class="subsection">5.7.5 Least squares fitting and the QR decomposition</h4>
<a name="index-Least-squares-fitting"></a>
<a name="index-QR-decomposition"></a>

<p>The function <code>lsfit()</code> returns a list giving results of a least
squares fitting procedure.  An assignment such as
</p>
<div class="example">
<pre class="example">&gt; ans &lt;- lsfit(X, y)
</pre></div>
<a name="index-lsfit"></a>

<p>gives the results of a least squares fit where <code>y</code> is the vector of
observations and <code>X</code> is the design matrix.  See the help facility
for more details, and also for the follow-up function <code>ls.diag()</code>
for, among other things, regression diagnostics.  Note that a grand mean
term is automatically included and need not be included explicitly as a
column of <code>X</code>.  Further note that you almost always will prefer
using <code>lm(.)</code> (see <a href="#Linear-models">Linear models</a>) to <code>lsfit()</code> for
regression modelling.
</p>
<a name="index-qr"></a>
<p>Another closely related function is <code>qr()</code> and its allies.
Consider the following assignments
</p>
<div class="example">
<pre class="example">&gt; Xplus &lt;- qr(X)
&gt; b &lt;- qr.coef(Xplus, y)
&gt; fit &lt;- qr.fitted(Xplus, y)
&gt; res &lt;- qr.resid(Xplus, y)
</pre></div>

<p>These compute the orthogonal projection of <code>y</code> onto the range of
<code>X</code> in <code>fit</code>, the projection onto the orthogonal complement in
<code>res</code> and the coefficient vector for the projection in <code>b</code>,
that is, <code>b</code> is essentially the result of the <small>MATLAB</small>
&lsquo;backslash&rsquo; operator.
</p>
<p>It is not assumed that <code>X</code> has full column rank.  Redundancies will
be discovered and removed as they are found.
</p>
<p>This alternative is the older, low-level way to perform least squares
calculations.  Although still useful in some contexts, it would now
generally be replaced by the statistical models features, as will be
discussed in <a href="#Statistical-models-in-R">Statistical models in R</a>.
</p>

<hr>
<a name="Forming-partitioned-matrices"></a>
<div class="header">
<p>
Next: <a href="#The-concatenation-function-c_0028_0029-with-arrays" accesskey="n" rel="next">The concatenation function c() with arrays</a>, Previous: <a href="#Matrix-facilities" accesskey="p" rel="prev">Matrix facilities</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Forming-partitioned-matrices_002c-cbind_0028_0029-and-rbind_0028_0029"></a>
<h3 class="section">5.8 Forming partitioned matrices, <code>cbind()</code> and <code>rbind()</code></h3>
<a name="index-cbind"></a>
<a name="index-rbind"></a>

<p>As we have already seen informally, matrices can be built up from other
vectors and matrices by the functions <code>cbind()</code> and <code>rbind()</code>.
Roughly <code>cbind()</code> forms matrices by binding together matrices
horizontally, or column-wise, and <code>rbind()</code> vertically, or
row-wise.
</p>
<p>In the assignment
</p>
<div class="example">
<pre class="example">&gt; X &lt;- cbind(<var>arg_1</var>, <var>arg_2</var>, <var>arg_3</var>, &hellip;)
</pre></div>

<p>the arguments to <code>cbind()</code> must be either vectors of any length, or
matrices with the same column size, that is the same number of rows.
The result is a matrix with the concatenated arguments <var>arg_1</var>,
<var>arg_2</var>, &hellip; forming the columns.
</p>
<p>If some of the arguments to <code>cbind()</code> are vectors they may be
shorter than the column size of any matrices present, in which case they
are cyclically extended to match the matrix column size (or the length
of the longest vector if no matrices are given).
</p>
<p>The function <code>rbind()</code> does the corresponding operation for rows.
In this case any vector argument, possibly cyclically extended, are of
course taken as row vectors.
</p>
<p>Suppose <code>X1</code> and <code>X2</code> have the same number of rows.  To
combine these by columns into a matrix <code>X</code>, together with an
initial column of <code>1</code>s we can use
</p>
<div class="example">
<pre class="example">&gt; X &lt;- cbind(1, X1, X2)
</pre></div>

<p>The result of <code>rbind()</code> or <code>cbind()</code> always has matrix status.
Hence <code>cbind(x)</code> and <code>rbind(x)</code> are possibly the simplest ways
explicitly to allow the vector <code>x</code> to be treated as a column or row
matrix respectively.
</p>
<hr>
<a name="The-concatenation-function-c_0028_0029-with-arrays"></a>
<div class="header">
<p>
Next: <a href="#Frequency-tables-from-factors" accesskey="n" rel="next">Frequency tables from factors</a>, Previous: <a href="#Forming-partitioned-matrices" accesskey="p" rel="prev">Forming partitioned matrices</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-concatenation-function_002c-c_0028_0029_002c-with-arrays"></a>
<h3 class="section">5.9 The concatenation function, <code>c()</code>, with arrays</h3>

<p>It should be noted that whereas <code>cbind()</code> and <code>rbind()</code> are
concatenation functions that respect <code>dim</code> attributes, the basic
<code>c()</code> function does not, but rather clears numeric objects of all
<code>dim</code> and <code>dimnames</code> attributes.  This is occasionally useful
in its own right.
</p>
<p>The official way to coerce an array back to a simple vector object is to
use <code>as.vector()</code>
</p>
<div class="example">
<pre class="example">&gt; vec &lt;- as.vector(X)
</pre></div>
<a name="index-as_002evector"></a>

<p>However a similar result can be achieved by using <code>c()</code> with just
one argument, simply for this side-effect:
</p>
<div class="example">
<pre class="example">&gt; vec &lt;- c(X)
</pre></div>
<a name="index-c-2"></a>

<p>There are slight differences between the two, but ultimately the choice
between them is largely a matter of style (with the former being
preferable).
</p>
<hr>
<a name="Frequency-tables-from-factors"></a>
<div class="header">
<p>
Previous: <a href="#The-concatenation-function-c_0028_0029-with-arrays" accesskey="p" rel="prev">The concatenation function c() with arrays</a>, Up: <a href="#Arrays-and-matrices" accesskey="u" rel="up">Arrays and matrices</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Frequency-tables-from-factors-1"></a>
<h3 class="section">5.10 Frequency tables from factors</h3>
<a name="index-Tabulation"></a>

<p>Recall that a factor defines a partition into groups.  Similarly a pair
of factors defines a two way cross classification, and so on.
<a name="index-table-1"></a>
The function <code>table()</code> allows frequency tables to be calculated
from equal length factors.  If there are <em>k</em> factor arguments,
the result is a <em>k</em>-way array of frequencies.
</p>
<p>Suppose, for example, that <code>statef</code> is a factor giving the state
code for each entry in a data vector.  The assignment
</p>
<div class="example">
<pre class="example">&gt; statefr &lt;- table(statef)
</pre></div>

<p>gives in <code>statefr</code> a table of frequencies of each state in the
sample.  The frequencies are ordered and labelled by the <code>levels</code>
attribute of the factor.  This simple case is equivalent to, but more
convenient than,
</p>
<div class="example">
<pre class="example">&gt; statefr &lt;- tapply(statef, statef, length)
</pre></div>

<p>Further suppose that <code>incomef</code> is a factor giving a suitably
defined &ldquo;income class&rdquo; for each entry in the data vector, for example
with the <code>cut()</code> function:
</p>
<div class="example">
<pre class="example">&gt; factor(cut(incomes, breaks = 35+10*(0:7))) -&gt; incomef
</pre></div>
<a name="index-cut"></a>

<p>Then to calculate a two-way table of frequencies:
</p>
<div class="example">
<pre class="example">&gt; table(incomef,statef)
         statef
incomef   act nsw nt qld sa tas vic wa
  (35,45]   1   1  0   1  0   0   1  0
  (45,55]   1   1  1   1  2   0   1  3
  (55,65]   0   3  1   3  2   2   2  1
  (65,75]   0   1  0   0  0   0   1  0
</pre></div>

<p>Extension to higher-way frequency tables is immediate.
</p>
<hr>
<a name="Lists-and-data-frames"></a>
<div class="header">
<p>
Next: <a href="#Reading-data-from-files" accesskey="n" rel="next">Reading data from files</a>, Previous: <a href="#Arrays-and-matrices" accesskey="p" rel="prev">Arrays and matrices</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Lists-and-data-frames-1"></a>
<h2 class="chapter">6 Lists and data frames</h2>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Lists" accesskey="1">Lists</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Constructing-and-modifying-lists" accesskey="2">Constructing and modifying lists</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Data-frames" accesskey="3">Data frames</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Lists"></a>
<div class="header">
<p>
Next: <a href="#Constructing-and-modifying-lists" accesskey="n" rel="next">Constructing and modifying lists</a>, Previous: <a href="#Lists-and-data-frames" accesskey="p" rel="prev">Lists and data frames</a>, Up: <a href="#Lists-and-data-frames" accesskey="u" rel="up">Lists and data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Lists-1"></a>
<h3 class="section">6.1 Lists</h3>
<a name="index-Lists"></a>

<p>An R <em>list</em> is an object consisting of an ordered collection of
objects known as its <em>components</em>.
</p>
<p>There is no particular need for the components to be of the same mode or
type, and, for example, a list could consist of a numeric vector, a
logical value, a matrix, a complex vector, a character array, a
function, and so on.  Here is a simple example of how to make a list:
</p>
<div class="example">
<pre class="example">&gt; Lst &lt;- list(name=&quot;Fred&quot;, wife=&quot;Mary&quot;, no.children=3,
              child.ages=c(4,7,9))
</pre></div>
<a name="index-list"></a>

<p>Components are always <em>numbered</em> and may always be referred to as
such.  Thus if <code>Lst</code> is the name of a list with four components,
these may be individually referred to as <code>Lst[[1]]</code>,
<code>Lst[[2]]</code>, <code>Lst[[3]]</code> and <code>Lst[[4]]</code>.  If, further,
<code>Lst[[4]]</code> is a vector subscripted array then <code>Lst[[4]][1]</code> is
its first entry.
</p>
<p>If <code>Lst</code> is a list, then the function <code>length(Lst)</code> gives the
number of (top level) components it has.
</p>
<p>Components of lists may also be <em>named</em>, and in this case the
component may be referred to either by giving the component name as a
character string in place of the number in double square brackets, or,
more conveniently, by giving an expression of the form
</p>
<div class="example">
<pre class="example">&gt; <var>name</var>$<var>component_name</var>
</pre></div>

<p>for the same thing.
</p>
<p>This is a very useful convention as it makes it easier to get the right
component if you forget the number.
</p>
<p>So in the simple example given above:
</p>
<p><code>Lst$name</code> is the same as <code>Lst[[1]]</code> and is the string
<code>&quot;Fred&quot;</code>,
</p>
<p><code>Lst$wife</code> is the same as <code>Lst[[2]]</code> and is the string
<code>&quot;Mary&quot;</code>,
</p>
<p><code>Lst$child.ages[1]</code> is the same as <code>Lst[[4]][1]</code> and is the
number <code>4</code>.
</p>
<p>Additionally, one can also use the names of the list components in
double square brackets, i.e., <code>Lst[[&quot;name&quot;]]</code> is the same as
<code>Lst$name</code>.  This is especially useful, when the name of the
component to be extracted is stored in another variable as in
</p>
<div class="example">
<pre class="example">&gt; x &lt;- &quot;name&quot;; Lst[[x]]
</pre></div>

<p>It is very important to distinguish <code>Lst[[1]]</code> from <code>Lst[1]</code>.
&lsquo;<samp><code>[[<var>&hellip;</var>]]</code></samp>&rsquo; is the operator used to select a single
element, whereas &lsquo;<samp><code>[<var>&hellip;</var>]</code></samp>&rsquo; is a general subscripting
operator.  Thus the former is the <em>first object in the list</em>
<code>Lst</code>, and if it is a named list the name is <em>not</em> included.
The latter is a <em>sublist of the list <code>Lst</code> consisting of the
first entry only.  If it is a named list, the names are transferred to
the sublist.</em>
</p>
<p>The names of components may be abbreviated down to the minimum number of
letters needed to identify them uniquely.  Thus <code>Lst$coefficients</code>
may be minimally specified as <code>Lst$coe</code> and <code>Lst$covariance</code>
as <code>Lst$cov</code>.
</p>
<p>The vector of names is in fact simply an attribute of the list like any
other and may be handled as such.  Other structures besides lists may,
of course, similarly be given a <em>names</em> attribute also.
</p>
<hr>
<a name="Constructing-and-modifying-lists"></a>
<div class="header">
<p>
Next: <a href="#Data-frames" accesskey="n" rel="next">Data frames</a>, Previous: <a href="#Lists" accesskey="p" rel="prev">Lists</a>, Up: <a href="#Lists-and-data-frames" accesskey="u" rel="up">Lists and data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Constructing-and-modifying-lists-1"></a>
<h3 class="section">6.2 Constructing and modifying lists</h3>

<p>New lists may be formed from existing objects by the function
<code>list()</code>.  An assignment of the form
</p>
<div class="example">
<pre class="example">&gt; Lst &lt;- list(<var>name_1</var>=<var>object_1</var>, <var>&hellip;</var>, <var>name_m</var>=<var>object_m</var>)
</pre></div>

<p>sets up a list <code>Lst</code> of <em>m</em> components using <var>object_1</var>,
&hellip;, <var>object_m</var> for the components and giving them names as
specified by the argument names, (which can be freely chosen).  If these
names are omitted, the components are numbered only.  The components
used to form the list are <em>copied</em> when forming the new list and
the originals are not affected.
</p>
<p>Lists, like any subscripted object, can be extended by specifying
additional components.  For example
</p>
<div class="example">
<pre class="example">&gt; Lst[5] &lt;- list(matrix=Mat)
</pre></div>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Concatenating-lists" accesskey="1">Concatenating lists</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Concatenating-lists"></a>
<div class="header">
<p>
Previous: <a href="#Constructing-and-modifying-lists" accesskey="p" rel="prev">Constructing and modifying lists</a>, Up: <a href="#Constructing-and-modifying-lists" accesskey="u" rel="up">Constructing and modifying lists</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Concatenating-lists-1"></a>
<h4 class="subsection">6.2.1 Concatenating lists</h4>
<a name="index-Concatenating-lists"></a>

<a name="index-c-3"></a>
<p>When the concatenation function <code>c()</code> is given list arguments, the
result is an object of mode list also, whose components are those of the
argument lists joined together in sequence.
</p>
<div class="example">
<pre class="example">&gt; list.ABC &lt;- c(list.A, list.B, list.C)
</pre></div>

<p>Recall that with vector objects as arguments the concatenation function
similarly joined together all arguments into a single vector structure.
In this case all other attributes, such as <code>dim</code> attributes, are
discarded.
</p>

<hr>
<a name="Data-frames"></a>
<div class="header">
<p>
Previous: <a href="#Constructing-and-modifying-lists" accesskey="p" rel="prev">Constructing and modifying lists</a>, Up: <a href="#Lists-and-data-frames" accesskey="u" rel="up">Lists and data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Data-frames-1"></a>
<h3 class="section">6.3 Data frames</h3>
<a name="index-Data-frames"></a>

<p>A <em>data frame</em> is a list with class <code>&quot;data.frame&quot;</code>.  There are
restrictions on lists that may be made into data frames, namely
</p>
<ul>
<li> The components must be vectors (numeric, character, or logical),
factors, numeric matrices, lists, or other data frames.
</li><li> Matrices, lists, and data frames provide as many variables to the new
data frame as they have columns, elements, or variables, respectively.
</li><li> Numeric vectors, logicals and factors are included as is, and by
default<a name="DOCF18" href="#FOOT18"><sup>18</sup></a> character vectors are coerced to be
factors, whose levels are the unique values appearing in the vector.
</li><li> Vector structures appearing as variables of the data frame must all have
the <em>same length</em>, and matrix structures must all have the same
<em>row size</em>.
</li></ul>

<p>A data frame may for many purposes be regarded as a matrix with columns
possibly of differing modes and attributes.  It may be displayed in
matrix form, and its rows and columns extracted using matrix indexing
conventions.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Making-data-frames" accesskey="1">Making data frames</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#attach_0028_0029-and-detach_0028_0029" accesskey="2">attach() and detach()</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Working-with-data-frames" accesskey="3">Working with data frames</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Attaching-arbitrary-lists" accesskey="4">Attaching arbitrary lists</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Managing-the-search-path" accesskey="5">Managing the search path</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Making-data-frames"></a>
<div class="header">
<p>
Next: <a href="#attach_0028_0029-and-detach_0028_0029" accesskey="n" rel="next">attach() and detach()</a>, Previous: <a href="#Data-frames" accesskey="p" rel="prev">Data frames</a>, Up: <a href="#Data-frames" accesskey="u" rel="up">Data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Making-data-frames-1"></a>
<h4 class="subsection">6.3.1 Making data frames</h4>

<p>Objects satisfying the restrictions placed on the columns (components)
of a data frame may be used to form one using the function
<code>data.frame</code>:
<a name="index-data_002eframe"></a>
</p>
<div class="example">
<pre class="example">&gt; accountants &lt;- data.frame(home=statef, loot=incomes, shot=incomef)
</pre></div>

<p>A list whose components conform to the restrictions of a data frame may
be <em>coerced</em> into a data frame using the function
<code>as.data.frame()</code>
<a name="index-as_002edata_002eframe"></a>
</p>
<p>The simplest way to construct a data frame from scratch is to use the
<code>read.table()</code> function to read an entire data frame from an
external file.  This is discussed further in <a href="#Reading-data-from-files">Reading data from files</a>.
</p>
<hr>
<a name="attach_0028_0029-and-detach_0028_0029"></a>
<div class="header">
<p>
Next: <a href="#Working-with-data-frames" accesskey="n" rel="next">Working with data frames</a>, Previous: <a href="#Making-data-frames" accesskey="p" rel="prev">Making data frames</a>, Up: <a href="#Data-frames" accesskey="u" rel="up">Data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="attach_0028_0029-and-detach_0028_0029-1"></a>
<h4 class="subsection">6.3.2 <code>attach()</code> and <code>detach()</code></h4>
<a name="index-attach"></a>
<a name="index-detach"></a>

<p>The <code>$</code> notation, such as <code>accountants$home</code>, for list
components is not always very convenient.  A useful facility would be
somehow to make the components of a list or data frame temporarily
visible as variables under their component name, without the need to
quote the list name explicitly each time.
</p>
<p>The <code>attach()</code> function takes a &lsquo;database&rsquo; such as a list or data
frame as its argument.  Thus suppose <code>lentils</code> is a
data frame with three variables <code>lentils$u</code>, <code>lentils$v</code>,
<code>lentils$w</code>.  The attach
</p>
<div class="example">
<pre class="example">&gt; attach(lentils)
</pre></div>

<p>places the data frame in the search path at position&nbsp;2<!-- /@w -->, and provided
there are no variables <code>u</code>, <code>v</code> or <code>w</code> in position&nbsp;1<!-- /@w -->,
<code>u</code>, <code>v</code> and <code>w</code> are available as variables from the data
frame in their own right.  At this point an assignment such as
</p>
<div class="example">
<pre class="example">&gt; u &lt;- v+w
</pre></div>

<p>does not replace the component <code>u</code> of the data frame, but rather
masks it with another variable <code>u</code> in the working directory at
position&nbsp;1<!-- /@w --> on the search path.  To make a permanent change to the
data frame itself, the simplest way is to resort once again to the
<code>$</code> notation:
</p>
<div class="example">
<pre class="example">&gt; lentils$u &lt;- v+w
</pre></div>

<p>However the new value of component <code>u</code> is not visible until the
data frame is detached and attached again.
</p>
<p>To detach a data frame, use the function
</p>
<div class="example">
<pre class="example">&gt; detach()
</pre></div>

<p>More precisely, this statement detaches from the search path the entity
currently at position&nbsp;2<!-- /@w -->.  Thus in the present context the variables
<code>u</code>, <code>v</code> and <code>w</code> would be no longer visible, except under
the list notation as <code>lentils$u</code> and so on.  Entities at positions
greater than 2 on the search path can be detached by giving their number
to <code>detach</code>, but it is much safer to always use a name, for example
by <code>detach(lentils)</code> or <code>detach(&quot;lentils&quot;)</code>
</p>
<blockquote>
<p><b>Note:</b> In R lists and data frames can only be attached at position 2 or
above, and what is attached is a <em>copy</em> of the original object.
You can alter the attached values <em>via</em> <code>assign</code>, but the
original list or data frame is unchanged.
</p></blockquote>

<hr>
<a name="Working-with-data-frames"></a>
<div class="header">
<p>
Next: <a href="#Attaching-arbitrary-lists" accesskey="n" rel="next">Attaching arbitrary lists</a>, Previous: <a href="#attach_0028_0029-and-detach_0028_0029" accesskey="p" rel="prev">attach() and detach()</a>, Up: <a href="#Data-frames" accesskey="u" rel="up">Data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Working-with-data-frames-1"></a>
<h4 class="subsection">6.3.3 Working with data frames</h4>

<p>A useful convention that allows you to work with many different problems
comfortably together in the same working directory is
</p>
<ul>
<li> gather together all variables for any well defined and separate problem
in a data frame under a suitably informative name;
</li><li> when working with a problem attach the appropriate data frame at
position&nbsp;2<!-- /@w -->, and use the working directory at level&nbsp;1<!-- /@w --> for
operational quantities and temporary variables;
</li><li> before leaving a problem, add any variables you wish to keep for future
reference to the data frame using the <code>$</code> form of assignment, and
then <code>detach()</code>;
</li><li> finally remove all unwanted variables from the working directory and
keep it as clean of left-over temporary variables as possible.
</li></ul>

<p>In this way it is quite simple to work with many problems in the same
directory, all of which have variables named <code>x</code>, <code>y</code> and
<code>z</code>, for example.
</p>
<hr>
<a name="Attaching-arbitrary-lists"></a>
<div class="header">
<p>
Next: <a href="#Managing-the-search-path" accesskey="n" rel="next">Managing the search path</a>, Previous: <a href="#Working-with-data-frames" accesskey="p" rel="prev">Working with data frames</a>, Up: <a href="#Data-frames" accesskey="u" rel="up">Data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Attaching-arbitrary-lists-1"></a>
<h4 class="subsection">6.3.4 Attaching arbitrary lists</h4>

<p><code>attach()</code> is a generic function that allows not only directories
and data frames to be attached to the search path, but other classes of
object as well.  In particular any object of mode <code>&quot;list&quot;</code> may be
attached in the same way:
</p>
<div class="example">
<pre class="example">&gt; attach(any.old.list)
</pre></div>

<p>Anything that has been attached can be detached by <code>detach</code>, by
position number or, preferably, by name.
</p>
<hr>
<a name="Managing-the-search-path"></a>
<div class="header">
<p>
Previous: <a href="#Attaching-arbitrary-lists" accesskey="p" rel="prev">Attaching arbitrary lists</a>, Up: <a href="#Data-frames" accesskey="u" rel="up">Data frames</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Managing-the-search-path-1"></a>
<h4 class="subsection">6.3.5 Managing the search path</h4>
<a name="index-search"></a>
<a name="index-Search-path"></a>

<p>The function <code>search</code> shows the current search path and so is
a very useful way to keep track of which data frames and lists (and
packages) have been attached and detached.  Initially it gives
</p>
<div class="example">
<pre class="example">&gt; search()
[1] &quot;.GlobalEnv&quot;   &quot;Autoloads&quot;    &quot;package:base&quot;
</pre></div>
<p>where <code>.GlobalEnv</code> is the workspace.<a name="DOCF19" href="#FOOT19"><sup>19</sup></a>
</p>
<p>After <code>lentils</code> is attached we have
</p>
<div class="example">
<pre class="example">&gt; search()
[1] &quot;.GlobalEnv&quot;   &quot;lentils&quot;      &quot;Autoloads&quot;    &quot;package:base&quot;
&gt; ls(2)
[1] &quot;u&quot; &quot;v&quot; &quot;w&quot;
</pre></div>

<p>and as we see <code>ls</code> (or <code>objects</code>) can be used to examine the
contents of any position on the search path.
</p>
<p>Finally, we detach the data frame and confirm it has been removed from
the search path.
</p>
<div class="example">
<pre class="example">&gt; detach(&quot;lentils&quot;)
&gt; search()
[1] &quot;.GlobalEnv&quot;   &quot;Autoloads&quot;    &quot;package:base&quot;
</pre></div>

<hr>
<a name="Reading-data-from-files"></a>
<div class="header">
<p>
Next: <a href="#Probability-distributions" accesskey="n" rel="next">Probability distributions</a>, Previous: <a href="#Lists-and-data-frames" accesskey="p" rel="prev">Lists and data frames</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Reading-data-from-files-1"></a>
<h2 class="chapter">7 Reading data from files</h2>
<a name="index-Reading-data-from-files"></a>

<p>Large data objects will usually be read as values from external files
rather than entered during an R session at the keyboard.  R input
facilities are simple and their requirements are fairly strict and even
rather inflexible.  There is a clear presumption by the designers of
R that you will be able to modify your input files using other tools,
such as file editors or Perl<a name="DOCF20" href="#FOOT20"><sup>20</sup></a> to fit in with the
requirements of R.  Generally this is very simple.
</p>
<p>If variables are to be held mainly in data frames, as we strongly
suggest they should be, an entire data frame can be read directly with
the <code>read.table()</code> function.  There is also a more primitive input
function, <code>scan()</code>, that can be called directly.
</p>
<p>For more details on importing data into R and also exporting data,
see the <em>R Data Import/Export</em> manual.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-read_002etable_0028_0029-function" accesskey="1">The read.table() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-scan_0028_0029-function" accesskey="2">The scan() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Accessing-builtin-datasets" accesskey="3">Accessing builtin datasets</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Editing-data" accesskey="4">Editing data</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-read_002etable_0028_0029-function"></a>
<div class="header">
<p>
Next: <a href="#The-scan_0028_0029-function" accesskey="n" rel="next">The scan() function</a>, Previous: <a href="#Reading-data-from-files" accesskey="p" rel="prev">Reading data from files</a>, Up: <a href="#Reading-data-from-files" accesskey="u" rel="up">Reading data from files</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-read_002etable_0028_0029-function-1"></a>
<h3 class="section">7.1 The <code>read.table()</code> function</h3>
<a name="index-read_002etable"></a>

<p>To read an entire data frame directly, the external file will normally
have a special form.
</p>
<ul>
<li> The first line of the file should have a <em>name</em> for each variable
in the data frame.

</li><li> Each additional line of the file has as its first item a <em>row label</em>
and the values for each variable.
</li></ul>

<p>If the file has one fewer item in its first line than in its second, this
arrangement is presumed to be in force.  So the first few lines of a file
to be read as a data frame might look as follows.
</p>
<blockquote>
<table summary="" class="cartouche" border="1"><tr><td>
<div class="example">
<pre class="example"><span class="roman">Input file form with names and row labels:</span>

     Price    Floor     Area   Rooms     Age  Cent.heat
01   52.00    111.0      830     5       6.2      no
02   54.75    128.0      710     5       7.5      no
03   57.50    101.0     1000     5       4.2      no
04   57.50    131.0      690     6       8.8      no
05   59.75     93.0      900     5       1.9     yes
...
</pre></div>
</td></tr></table>
</blockquote>

<p>By default numeric items (except row labels) are read as numeric
variables and non-numeric variables, such as <code>Cent.heat</code> in the
example, as factors.  This can be changed if necessary.
</p>
<p>The function <code>read.table()</code> can then be used to read the data frame
directly
</p>
<div class="example">
<pre class="example">&gt; HousePrice &lt;- read.table(&quot;houses.data&quot;)
</pre></div>

<p>Often you will want to omit including the row labels directly and use the
default labels.  In this case the file may omit the row label column as in
the following.
</p>
<blockquote>
<table summary="" class="cartouche" border="1"><tr><td>
<div class="example">
<pre class="example"><span class="roman">Input file form without row labels:</span>

Price    Floor     Area   Rooms     Age  Cent.heat
52.00    111.0      830     5       6.2      no
54.75    128.0      710     5       7.5      no
57.50    101.0     1000     5       4.2      no
57.50    131.0      690     6       8.8      no
59.75     93.0      900     5       1.9     yes
...
</pre></div>
</td></tr></table>
</blockquote>

<p>The data frame may then be read as
</p>
<div class="example">
<pre class="example">&gt; HousePrice &lt;- read.table(&quot;houses.data&quot;, header=TRUE)
</pre></div>

<p>where the <code>header=TRUE</code> option specifies that the first line is a
line of headings, and hence, by implication from the form of the file,
that no explicit row labels are given.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-scan_0028_0029-function" accesskey="1">The scan() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-scan_0028_0029-function"></a>
<div class="header">
<p>
Next: <a href="#Accessing-builtin-datasets" accesskey="n" rel="next">Accessing builtin datasets</a>, Previous: <a href="#The-read_002etable_0028_0029-function" accesskey="p" rel="prev">The read.table() function</a>, Up: <a href="#Reading-data-from-files" accesskey="u" rel="up">Reading data from files</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-scan_0028_0029-function-1"></a>
<h3 class="section">7.2 The <code>scan()</code> function</h3>
<a name="index-scan"></a>

<p>Suppose the data vectors are of equal length and are to be read in
parallel.  Further suppose that there are three vectors, the first of
mode character and the remaining two of mode numeric, and the file is
<samp>input.dat</samp>.  The first step is to use <code>scan()</code> to read in the
three vectors as a list, as follows
</p>
<div class="example">
<pre class="example">&gt; inp &lt;- scan(&quot;input.dat&quot;, list(&quot;&quot;,0,0))
</pre></div>

<p>The second argument is a dummy list structure that establishes the mode
of the three vectors to be read.  The result, held in <code>inp</code>, is a
list whose components are the three vectors read in.  To separate the
data items into three separate vectors, use assignments like
</p>
<div class="example">
<pre class="example">&gt; label &lt;- inp[[1]]; x &lt;- inp[[2]]; y &lt;- inp[[3]]
</pre></div>

<p>More conveniently, the dummy list can have named components, in which
case the names can be used to access the vectors read in.  For example
</p>
<div class="example">
<pre class="example">&gt; inp &lt;- scan(&quot;input.dat&quot;, list(id=&quot;&quot;, x=0, y=0))
</pre></div>

<p>If you wish to access the variables separately they may either be
re-assigned to variables in the working frame:
</p>
<div class="example">
<pre class="example">&gt; label &lt;- inp$id; x &lt;- inp$x; y &lt;- inp$y
</pre></div>

<p>or the list may be attached at position&nbsp;2<!-- /@w --> of the search path
(see <a href="#Attaching-arbitrary-lists">Attaching arbitrary lists</a>).
</p>
<p>If the second argument is a single value and not a list, a single vector
is read in, all components of which must be of the same mode as the
dummy value.
</p>
<div class="example">
<pre class="example">&gt; X &lt;- matrix(scan(&quot;light.dat&quot;, 0), ncol=5, byrow=TRUE)
</pre></div>

<p>There are more elaborate input facilities available and these are
detailed in the manuals.
</p>
<hr>
<a name="Accessing-builtin-datasets"></a>
<div class="header">
<p>
Next: <a href="#Editing-data" accesskey="n" rel="next">Editing data</a>, Previous: <a href="#The-scan_0028_0029-function" accesskey="p" rel="prev">The scan() function</a>, Up: <a href="#Reading-data-from-files" accesskey="u" rel="up">Reading data from files</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Accessing-builtin-datasets-1"></a>
<h3 class="section">7.3 Accessing builtin datasets</h3>
<a name="index-Accessing-builtin-datasets"></a>
<a name="index-data"></a>

<p>Around 100 datasets are supplied with R (in package <strong>datasets</strong>),
and others are available in packages (including the recommended packages
supplied with R).  To see the list of datasets currently available
use
</p>
<div class="example">
<pre class="example">data()
</pre></div>

<p>All the datasets supplied with R are available directly by name.
However, many packages still use the obsolete convention in which
<code>data</code> was also used to load datasets into R, for example
</p>
<div class="example">
<pre class="example">data(infert)
</pre></div>

<p>and this can still be used with the standard packages (as in this
example).  In most cases this will load an R object of the same name.
However, in a few cases it loads several objects, so see the on-line
help for the object to see what to expect.
</p>
<a name="Loading-data-from-other-R-packages"></a>
<h4 class="subsection">7.3.1 Loading data from other R packages</h4>

<p>To access data from a particular package, use the <code>package</code>
argument, for example
</p>
<div class="example">
<pre class="example">data(package=&quot;rpart&quot;)
data(Puromycin, package=&quot;datasets&quot;)
</pre></div>

<p>If a package has been attached by <code>library</code>, its datasets are
automatically included in the search.
</p>
<p>User-contributed packages can be a rich source of datasets.
</p>
<hr>
<a name="Editing-data"></a>
<div class="header">
<p>
Previous: <a href="#Accessing-builtin-datasets" accesskey="p" rel="prev">Accessing builtin datasets</a>, Up: <a href="#Reading-data-from-files" accesskey="u" rel="up">Reading data from files</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Editing-data-1"></a>
<h3 class="section">7.4 Editing data</h3>

<a name="index-edit"></a>
<p>When invoked on a data frame or matrix, <code>edit</code> brings up a separate
spreadsheet-like environment for editing.  This is useful for making
small changes once a data set has been read.  The command
</p>
<div class="example">
<pre class="example">&gt; xnew &lt;- edit(xold)
</pre></div>

<p>will allow you to edit your data set <code>xold</code>, and on completion the
changed object is assigned to <code>xnew</code>.  If you want to alter the
original dataset <code>xold</code>, the simplest way is to use
<code>fix(xold)</code>, which is equivalent to <code>xold &lt;- edit(xold)</code>.
</p>
<p>Use
</p>
<div class="example">
<pre class="example">&gt; xnew &lt;- edit(data.frame())
</pre></div>

<p>to enter new data via the spreadsheet interface.
</p>

<hr>
<a name="Probability-distributions"></a>
<div class="header">
<p>
Next: <a href="#Loops-and-conditional-execution" accesskey="n" rel="next">Loops and conditional execution</a>, Previous: <a href="#Reading-data-from-files" accesskey="p" rel="prev">Reading data from files</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Probability-distributions-1"></a>
<h2 class="chapter">8 Probability distributions</h2>
<a name="index-Probability-distributions"></a>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#R-as-a-set-of-statistical-tables" accesskey="1">R as a set of statistical tables</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Examining-the-distribution-of-a-set-of-data" accesskey="2">Examining the distribution of a set of data</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#One_002d-and-two_002dsample-tests" accesskey="3">One- and two-sample tests</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="R-as-a-set-of-statistical-tables"></a>
<div class="header">
<p>
Next: <a href="#Examining-the-distribution-of-a-set-of-data" accesskey="n" rel="next">Examining the distribution of a set of data</a>, Previous: <a href="#Probability-distributions" accesskey="p" rel="prev">Probability distributions</a>, Up: <a href="#Probability-distributions" accesskey="u" rel="up">Probability distributions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="R-as-a-set-of-statistical-tables-1"></a>
<h3 class="section">8.1 R as a set of statistical tables</h3>

<p>One convenient use of R is to provide a comprehensive set of
statistical tables.  Functions are provided to evaluate the cumulative
distribution function P(X &lt;= x),
the probability density function and the quantile function (given
<em>q</em>, the smallest <em>x</em> such that P(X &lt;= x) &gt; q),
and to simulate from the distribution.
</p>
<blockquote>
<table summary="">
<thead><tr><th>Distribution</th><th>R name</th><th>additional arguments</th></tr></thead>
<tr><td>beta</td><td><code>beta</code></td><td><code>shape1, shape2, ncp</code></td></tr>
<tr><td>binomial</td><td><code>binom</code></td><td><code>size, prob</code></td></tr>
<tr><td>Cauchy</td><td><code>cauchy</code></td><td><code>location, scale</code></td></tr>
<tr><td>chi-squared</td><td><code>chisq</code></td><td><code>df, ncp</code></td></tr>
<tr><td>exponential</td><td><code>exp</code></td><td><code>rate</code></td></tr>
<tr><td>F</td><td><code>f</code></td><td><code>df1, df2, ncp</code></td></tr>
<tr><td>gamma</td><td><code>gamma</code></td><td><code>shape, scale</code></td></tr>
<tr><td>geometric</td><td><code>geom</code></td><td><code>prob</code></td></tr>
<tr><td>hypergeometric</td><td><code>hyper</code></td><td><code>m, n, k</code></td></tr>
<tr><td>log-normal</td><td><code>lnorm</code></td><td><code>meanlog, sdlog</code></td></tr>
<tr><td>logistic</td><td><code>logis</code></td><td><code>location, scale</code></td></tr>
<tr><td>negative binomial</td><td><code>nbinom</code></td><td><code>size, prob</code></td></tr>
<tr><td>normal</td><td><code>norm</code></td><td><code>mean, sd</code></td></tr>
<tr><td>Poisson</td><td><code>pois</code></td><td><code>lambda</code></td></tr>
<tr><td>signed rank</td><td><code>signrank</code></td><td><code>n</code></td></tr>
<tr><td>Student&rsquo;s t</td><td><code>t</code></td><td><code>df, ncp</code></td></tr>
<tr><td>uniform</td><td><code>unif</code></td><td><code>min, max</code></td></tr>
<tr><td>Weibull</td><td><code>weibull</code></td><td><code>shape, scale</code></td></tr>
<tr><td>Wilcoxon</td><td><code>wilcox</code></td><td><code>m, n</code></td></tr>
</table>
</blockquote>

<p>Prefix the name given here by &lsquo;<samp>d</samp>&rsquo; for the density, &lsquo;<samp>p</samp>&rsquo; for the
CDF, &lsquo;<samp>q</samp>&rsquo; for the quantile function and &lsquo;<samp>r</samp>&rsquo; for simulation
(<em>r</em>andom deviates).  The first argument is <code>x</code> for
<code>d<var>xxx</var></code>, <code>q</code> for <code>p<var>xxx</var></code>, <code>p</code> for
<code>q<var>xxx</var></code> and <code>n</code> for <code>r<var>xxx</var></code> (except for
<code>rhyper</code>, <code>rsignrank</code> and <code>rwilcox</code>, for which it is
<code>nn</code>).  In not quite all cases is the non-centrality parameter
<code>ncp</code> currently available: see the on-line help for details.
</p>
<p>The <code>p<var>xxx</var></code> and <code>q<var>xxx</var></code> functions all have logical
arguments <code>lower.tail</code> and <code>log.p</code> and the <code>d<var>xxx</var></code>
ones have <code>log</code>.  This allows, e.g., getting the cumulative (or
&ldquo;integrated&rdquo;) <em>hazard</em> function, H(t) = - log(1 - F(t)), by
</p>
<div class="example">
<pre class="example"> - p<var>xxx</var>(t, ..., lower.tail = FALSE, log.p = TRUE)
</pre></div>

<p>or more accurate log-likelihoods (by <code>d<var>xxx</var>(..., log =
TRUE)</code>), directly.
</p>
<p>In addition there are functions <code>ptukey</code> and <code>qtukey</code> for the
distribution of the studentized range of samples from a normal
distribution, and <code>dmultinom</code> and <code>rmultinom</code> for the
multinomial distribution. Further distributions are available in
contributed packages, notably <a href="https://CRAN.R-project.org/package=SuppDists"><strong>SuppDists</strong></a>.
</p>
<p>Here are some examples
</p>
<div class="example">
<pre class="example">&gt; ## <span class="roman">2-tailed p-value for t distribution</span>
&gt; 2*pt(-2.43, df = 13)
&gt; ## <span class="roman">upper 1% point for an F(2, 7) distribution</span>
&gt; qf(0.01, 2, 7, lower.tail = FALSE)
</pre></div>

<p>See the on-line help on <code>RNG</code> for how random-number generation is
done in R.
</p>
<hr>
<a name="Examining-the-distribution-of-a-set-of-data"></a>
<div class="header">
<p>
Next: <a href="#One_002d-and-two_002dsample-tests" accesskey="n" rel="next">One- and two-sample tests</a>, Previous: <a href="#R-as-a-set-of-statistical-tables" accesskey="p" rel="prev">R as a set of statistical tables</a>, Up: <a href="#Probability-distributions" accesskey="u" rel="up">Probability distributions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Examining-the-distribution-of-a-set-of-data-1"></a>
<h3 class="section">8.2 Examining the distribution of a set of data</h3>

<p>Given a (univariate) set of data we can examine its distribution in a
large number of ways.  The simplest is to examine the numbers.  Two
slightly different summaries are given by <code>summary</code> and
<code>fivenum</code>
<a name="index-summary"></a>
<a name="index-fivenum"></a>
and a display of the numbers by <code>stem</code> (a &ldquo;stem and leaf&rdquo; plot).
<a name="index-stem"></a>
</p>
<div class="example">
<pre class="example">&gt; attach(faithful)
&gt; summary(eruptions)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
  1.600   2.163   4.000   3.488   4.454   5.100
&gt; fivenum(eruptions)
[1] 1.6000 2.1585 4.0000 4.4585 5.1000
&gt; stem(eruptions)

  The decimal point is 1 digit(s) to the left of the |

  16 | 070355555588
  18 | 000022233333335577777777888822335777888
  20 | 00002223378800035778
  22 | 0002335578023578
  24 | 00228
  26 | 23
  28 | 080
  30 | 7
  32 | 2337
  34 | 250077
  36 | 0000823577
  38 | 2333335582225577
  40 | 0000003357788888002233555577778
  42 | 03335555778800233333555577778
  44 | 02222335557780000000023333357778888
  46 | 0000233357700000023578
  48 | 00000022335800333
  50 | 0370
</pre></div>

<p>A stem-and-leaf plot is like a histogram, and R has a function
<code>hist</code> to plot histograms.
<a name="index-hist"></a>
</p>
<div class="example">
<pre class="example">&gt; hist(eruptions)
## <span class="roman">make the bins smaller, make a plot of density</span>
&gt; hist(eruptions, seq(1.6, 5.2, 0.2), prob=TRUE)
&gt; lines(density(eruptions, bw=0.1))
&gt; rug(eruptions) # <span class="roman">show the actual data points</span>
</pre></div>

<a name="index-density"></a>
<a name="index-Density-estimation"></a>
<p>More elegant density plots can be made by <code>density</code>, and we added a
line produced by <code>density</code> in this example.  The bandwidth
<code>bw</code> was chosen by trial-and-error as the default gives too much
smoothing (it usually does for &ldquo;interesting&rdquo; densities).  (Better
automated methods of bandwidth choice are available, and in this example
<code>bw = &quot;SJ&quot;</code> gives a good result.)
</p>
<img src="images/hist.png" alt="images/hist">

<p>We can plot the empirical cumulative distribution function by using the
function <code>ecdf</code>.
<a name="index-ecdf"></a>
<a name="index-Empirical-CDFs"></a>
</p>
<div class="example">
<pre class="example">&gt; plot(ecdf(eruptions), do.points=FALSE, verticals=TRUE)
</pre></div>

<p>This distribution is obviously far from any standard distribution.
How about the right-hand mode, say eruptions of longer than 3 minutes?
Let us fit a normal distribution and overlay the fitted CDF.
</p>
<div class="example">
<pre class="example">&gt; long &lt;- eruptions[eruptions &gt; 3]
&gt; plot(ecdf(long), do.points=FALSE, verticals=TRUE)
&gt; x &lt;- seq(3, 5.4, 0.01)
&gt; lines(x, pnorm(x, mean=mean(long), sd=sqrt(var(long))), lty=3)
</pre></div>

<img src="images/ecdf.png" alt="images/ecdf">

<p>Quantile-quantile (Q-Q) plots can help us examine this more carefully.
<a name="index-Quantile_002dquantile-plots"></a>
<a name="index-qqnorm"></a>
<a name="index-qqline"></a>
</p>
<div class="example">
<pre class="example">par(pty=&quot;s&quot;)       # arrange for a square figure region
qqnorm(long); qqline(long)
</pre></div>

<p>which shows a reasonable fit but a shorter right tail than one would
expect from a normal distribution.  Let us compare this with some
simulated data from a <em>t</em> distribution
</p>
<img src="images/QQ.png" alt="images/QQ">

<div class="example">
<pre class="example">x &lt;- rt(250, df = 5)
qqnorm(x); qqline(x)
</pre></div>

<p>which will usually (if it is a random sample) show longer tails than
expected for a normal.  We can make a Q-Q plot against the generating
distribution by
</p>
<div class="example">
<pre class="example">qqplot(qt(ppoints(250), df = 5), x, xlab = &quot;Q-Q plot for t dsn&quot;)
qqline(x)
</pre></div>

<p>Finally, we might want a more formal test of agreement with normality
(or not).  R provides the Shapiro-Wilk test
<a name="index-Shapiro_002dWilk-test"></a>
<a name="index-shapiro_002etest"></a>
</p>
<div class="example">
<pre class="example">&gt; shapiro.test(long)

         Shapiro-Wilk normality test

data:  long
W = 0.9793, p-value = 0.01052
</pre></div>

<p>and the Kolmogorov-Smirnov test
<a name="index-Kolmogorov_002dSmirnov-test"></a>
<a name="index-ks_002etest"></a>
</p>
<div class="example">
<pre class="example">&gt; ks.test(long, &quot;pnorm&quot;, mean = mean(long), sd = sqrt(var(long)))

         One-sample Kolmogorov-Smirnov test

data:  long
D = 0.0661, p-value = 0.4284
alternative hypothesis: two.sided
</pre></div>

<p>(Note that the distribution theory is not valid here as we
have estimated the parameters of the normal distribution from the same
sample.)
</p>
<hr>
<a name="One_002d-and-two_002dsample-tests"></a>
<div class="header">
<p>
Previous: <a href="#Examining-the-distribution-of-a-set-of-data" accesskey="p" rel="prev">Examining the distribution of a set of data</a>, Up: <a href="#Probability-distributions" accesskey="u" rel="up">Probability distributions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="One_002d-and-two_002dsample-tests-1"></a>
<h3 class="section">8.3 One- and two-sample tests</h3>
<a name="index-One_002d-and-two_002dsample-tests"></a>

<p>So far we have compared a single sample to a normal distribution.  A
much more common operation is to compare aspects of two samples.  Note
that in R, all &ldquo;classical&rdquo; tests including the ones used below are
in package <strong>stats</strong> which is normally loaded.
</p>
<p>Consider the following sets of data on the latent heat of the fusion of
ice (<em>cal/gm</em>) from Rice (1995, p.490)
</p>
<div class="example">
<pre class="example">Method A: 79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97
          80.05 80.03 80.02 80.00 80.02
Method B: 80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97
</pre></div>

<p>Boxplots provide a simple graphical comparison of the two samples.
</p>
<div class="example">
<pre class="example">A &lt;- scan()
79.98 80.04 80.02 80.04 80.03 80.03 80.04 79.97
80.05 80.03 80.02 80.00 80.02

B &lt;- scan()
80.02 79.94 79.98 79.97 79.97 80.03 79.95 79.97

boxplot(A, B)
</pre></div>
<a name="index-boxplot"></a>
<a name="index-Box-plots"></a>

<p>which indicates that the first group tends to give higher results than
the second.
</p>
<img src="images/ice.png" alt="images/ice">

<p>To test for the equality of the means of the two examples, we can use
an <em>unpaired</em> <em>t</em>-test by
<a name="index-Student_0027s-t-test"></a>
<a name="index-t_002etest"></a>
</p>
<div class="example">
<pre class="example">&gt; t.test(A, B)

         Welch Two Sample t-test

data:  A and B
t = 3.2499, df = 12.027, p-value = 0.00694
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.01385526 0.07018320
sample estimates:
mean of x mean of y
 80.02077  79.97875
</pre></div>

<p>which does indicate a significant difference, assuming normality.  By
default the R function does not assume equality of variances in the
two samples (in contrast to the similar <small>S-PLUS</small> <code>t.test</code>
function).  We can use the F test to test for equality in the variances,
provided that the two samples are from normal populations.
</p>
<div class="example">
<pre class="example">&gt; var.test(A, B)

         F test to compare two variances

data:  A and B
F = 0.5837, num df = 12, denom df =  7, p-value = 0.3938
alternative hypothesis: true ratio of variances is not equal to 1
95 percent confidence interval:
 0.1251097 2.1052687
sample estimates:
ratio of variances
         0.5837405
</pre></div>
<a name="index-var_002etest"></a>

<p>which shows no evidence of a significant difference, and so we can use
the classical <em>t</em>-test that assumes equality of the variances.
</p>
<div class="example">
<pre class="example">&gt; t.test(A, B, var.equal=TRUE)

         Two Sample t-test

data:  A and B
t = 3.4722, df = 19, p-value = 0.002551
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 0.01669058 0.06734788
sample estimates:
mean of x mean of y
 80.02077  79.97875
</pre></div>

<p>All these tests assume normality of the two samples.  The two-sample
Wilcoxon (or Mann-Whitney) test only assumes a common continuous
distribution under the null hypothesis.
</p>
<a name="index-Wilcoxon-test"></a>
<a name="index-wilcox_002etest"></a>
<div class="example">
<pre class="example">&gt; wilcox.test(A, B)

         Wilcoxon rank sum test with continuity correction

data:  A and B
W = 89, p-value = 0.007497
alternative hypothesis: true location shift is not equal to 0

Warning message:
Cannot compute exact p-value with ties in: wilcox.test(A, B)
</pre></div>

<p>Note the warning: there are several ties in each sample, which suggests
strongly that these data are from a discrete distribution (probably due
to rounding).
</p>
<p>There are several ways to compare graphically the two samples.  We have
already seen a pair of boxplots.  The following
</p>
<div class="example">
<pre class="example">&gt; plot(ecdf(A), do.points=FALSE, verticals=TRUE, xlim=range(A, B))
&gt; plot(ecdf(B), do.points=FALSE, verticals=TRUE, add=TRUE)
</pre></div>

<p>will show the two empirical CDFs, and <code>qqplot</code> will perform a Q-Q
plot of the two samples.  The Kolmogorov-Smirnov test is of the maximal
vertical distance between the two ecdf&rsquo;s, assuming a common continuous
distribution:
</p>
<div class="example">
<pre class="example">&gt; ks.test(A, B)

         Two-sample Kolmogorov-Smirnov test

data:  A and B
D = 0.5962, p-value = 0.05919
alternative hypothesis: two-sided

Warning message:
cannot compute correct p-values with ties in: ks.test(A, B)
</pre></div>

<hr>
<a name="Loops-and-conditional-execution"></a>
<div class="header">
<p>
Next: <a href="#Writing-your-own-functions" accesskey="n" rel="next">Writing your own functions</a>, Previous: <a href="#Probability-distributions" accesskey="p" rel="prev">Probability distributions</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Grouping_002c-loops-and-conditional-execution"></a>
<h2 class="chapter">9 Grouping, loops and conditional execution</h2>
<a name="index-Loops-and-conditional-execution"></a>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Grouped-expressions" accesskey="1">Grouped expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Control-statements" accesskey="2">Control statements</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Grouped-expressions"></a>
<div class="header">
<p>
Next: <a href="#Control-statements" accesskey="n" rel="next">Control statements</a>, Previous: <a href="#Loops-and-conditional-execution" accesskey="p" rel="prev">Loops and conditional execution</a>, Up: <a href="#Loops-and-conditional-execution" accesskey="u" rel="up">Loops and conditional execution</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Grouped-expressions-1"></a>
<h3 class="section">9.1 Grouped expressions</h3>
<a name="index-Grouped-expressions"></a>

<p>R is an expression language in the sense that its only command type
is a function or expression which returns a result.  Even an assignment
is an expression whose result is the value assigned, and it may be used
wherever any expression may be used; in particular multiple assignments
are possible.
</p>
<p>Commands may be grouped together in braces, <code>{<var>expr_1</var>;
<var>&hellip;</var>; <var>expr_m</var>}</code>, in which case the value of the group
is the result of the last expression in the group evaluated.  Since such
a group is also an expression it may, for example, be itself included in
parentheses and used as part of an even larger expression, and so on.
</p>
<hr>
<a name="Control-statements"></a>
<div class="header">
<p>
Previous: <a href="#Grouped-expressions" accesskey="p" rel="prev">Grouped expressions</a>, Up: <a href="#Loops-and-conditional-execution" accesskey="u" rel="up">Loops and conditional execution</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Control-statements-1"></a>
<h3 class="section">9.2 Control statements</h3>
<a name="index-Control-statements"></a>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Conditional-execution" accesskey="1">Conditional execution</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Repetitive-execution" accesskey="2">Repetitive execution</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Conditional-execution"></a>
<div class="header">
<p>
Next: <a href="#Repetitive-execution" accesskey="n" rel="next">Repetitive execution</a>, Previous: <a href="#Control-statements" accesskey="p" rel="prev">Control statements</a>, Up: <a href="#Control-statements" accesskey="u" rel="up">Control statements</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Conditional-execution_003a-if-statements"></a>
<h4 class="subsection">9.2.1 Conditional execution: <code>if</code> statements</h4>
<a name="index-if"></a>

<p>The language has available a conditional construction of the form
</p>
<div class="example">
<pre class="example">&gt; if (<var>expr_1</var>) <var>expr_2</var> else <var>expr_3</var>
</pre></div>
<a name="index-if-1"></a>
<a name="index-else"></a>

<p>where <var>expr_1</var> must evaluate to a single logical value and the
result of the entire expression is then evident.
</p>
<a name="index-_0026_0026"></a>
<a name="index-_007c_007c"></a>
<p>The &ldquo;short-circuit&rdquo; operators <code>&amp;&amp;</code> and <code>||</code> are often used
as part of the condition in an <code>if</code> statement.  Whereas <code>&amp;</code>
and <code>|</code> apply element-wise to vectors, <code>&amp;&amp;</code> and <code>||</code>
apply to vectors of length one, and only evaluate their second argument
if necessary.
</p>
<a name="index-ifelse"></a>
<p>There is a vectorized version of the <code>if</code>/<code>else</code> construct,
the <code>ifelse</code> function.  This has the form <code>ifelse(condition, a,
b)</code> and returns a vector of the same length as <code>condition</code>, with
elements <code>a[i]</code> if <code>condition[i]</code> is true, otherwise
<code>b[i]</code> (where <code>a</code> and <code>b</code> are recycled as necessary).
</p>

<hr>
<a name="Repetitive-execution"></a>
<div class="header">
<p>
Previous: <a href="#Conditional-execution" accesskey="p" rel="prev">Conditional execution</a>, Up: <a href="#Control-statements" accesskey="u" rel="up">Control statements</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Repetitive-execution_003a-for-loops_002c-repeat-and-while"></a>
<h4 class="subsection">9.2.2 Repetitive execution: <code>for</code> loops, <code>repeat</code> and <code>while</code></h4>
<a name="index-for"></a>

<p>There is also a <code>for</code> loop construction which has the form
</p>
<div class="example">
<pre class="example">&gt; for (<code><var>name</var></code> in <var>expr_1</var>) <var>expr_2</var>
</pre></div>

<p>where <code><var>name</var></code> is the loop variable.  <var>expr_1</var> is a
vector expression, (often a sequence like <code>1:20</code>), and
<var>expr_2</var> is often a grouped expression with its sub-expressions
written in terms of the dummy <em>name</em>.  <var>expr_2</var> is repeatedly
evaluated as <var>name</var> ranges through the values in the vector result
of <var>expr_1</var>.
</p>
<p>As an example, suppose <code>ind</code> is a vector of class indicators and we
wish to produce separate plots of <code>y</code> versus <code>x</code> within
classes.  One possibility here is to use <code>coplot()</code>,<a name="DOCF21" href="#FOOT21"><sup>21</sup></a>
which will produce an array of plots corresponding to each level of the
factor.  Another way to do this, now putting all plots on the one
display, is as follows:
</p>
<div class="example">
<pre class="example">&gt; xc &lt;- split(x, ind)
&gt; yc &lt;- split(y, ind)
&gt; for (i in 1:length(yc)) {
    plot(xc[[i]], yc[[i]])
    abline(lsfit(xc[[i]], yc[[i]]))
  }
</pre></div>

<a name="index-split"></a>

<p>(Note the function <code>split()</code> which produces a list of vectors
obtained by splitting a larger vector according to the classes specified
by a factor.  This is a useful function, mostly used in connection
with boxplots.  See the <code>help</code> facility for further details.)
</p>
<blockquote>
<p><strong>Warning</strong>: <code>for()</code> loops are used in R code much less
often than in compiled languages.  Code that takes a &lsquo;whole object&rsquo; view
is likely to be both clearer and faster in R.
</p></blockquote>

<p>Other looping facilities include the
</p>
<div class="example">
<pre class="example">&gt; repeat <var>expr</var>
</pre></div>
<a name="index-repeat"></a>

<p>statement and the
</p>
<div class="example">
<pre class="example">&gt; while (<var>condition</var>) <var>expr</var>
</pre></div>
<a name="index-while"></a>

<p>statement.
</p>
<p>The <code>break</code> statement can be used to terminate any loop, possibly
abnormally.  This is the only way to terminate <code>repeat</code> loops.
<a name="index-break"></a>
</p>
<p>The <code>next</code> statement can be used to discontinue one particular
cycle and skip to the &ldquo;next&rdquo;.
<a name="index-next"></a>
</p>
<p>Control statements are most often used in connection with
<em>functions</em> which are discussed in <a href="#Writing-your-own-functions">Writing your own functions</a>, and where more examples will emerge.
</p>

<hr>
<a name="Writing-your-own-functions"></a>
<div class="header">
<p>
Next: <a href="#Statistical-models-in-R" accesskey="n" rel="next">Statistical models in R</a>, Previous: <a href="#Loops-and-conditional-execution" accesskey="p" rel="prev">Loops and conditional execution</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Writing-your-own-functions-1"></a>
<h2 class="chapter">10 Writing your own functions</h2>
<a name="index-Writing-functions"></a>

<p>As we have seen informally along the way, the R language allows the
user to create objects of mode <em>function</em>.  These are true R
functions that are stored in a special internal form and may be used in
further expressions and so on.  In the process, the language gains
enormously in power, convenience and elegance, and learning to write
useful functions is one of the main ways to make your use of R
comfortable and productive.
</p>
<p>It should be emphasized that most of the functions supplied as part of
the R system, such as <code>mean()</code>, <code>var()</code>,
<code>postscript()</code> and so on, are themselves written in R and thus
do not differ materially from user written functions.
</p>
<p>A function is defined by an assignment of the form
</p>
<div class="example">
<pre class="example">&gt; <var>name</var> &lt;- function(<var>arg_1</var>, <var>arg_2</var>, &hellip;) <var>expression</var>
</pre></div>
<a name="index-function"></a>

<p>The <var>expression</var> is an R expression, (usually a grouped
expression), that uses the arguments, <var>arg_i</var>, to calculate a value.
The value of the expression is the value returned for the function.
</p>
<p>A call to the function then usually takes the form
<code><var>name</var>(<var>expr_1</var>, <var>expr_2</var>, &hellip;)</code> and may occur
anywhere a function call is legitimate.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Simple-examples" accesskey="1">Simple examples</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Defining-new-binary-operators" accesskey="2">Defining new binary operators</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Named-arguments-and-defaults" accesskey="3">Named arguments and defaults</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-three-dots-argument" accesskey="4">The three dots argument</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Assignment-within-functions" accesskey="5">Assignment within functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#More-advanced-examples" accesskey="6">More advanced examples</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Scope" accesskey="7">Scope</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Customizing-the-environment" accesskey="8">Customizing the environment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Object-orientation" accesskey="9">Object orientation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Simple-examples"></a>
<div class="header">
<p>
Next: <a href="#Defining-new-binary-operators" accesskey="n" rel="next">Defining new binary operators</a>, Previous: <a href="#Writing-your-own-functions" accesskey="p" rel="prev">Writing your own functions</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Simple-examples-1"></a>
<h3 class="section">10.1 Simple examples</h3>

<p>As a first example, consider a function to calculate the two sample
<em>t</em>-statistic, showing &ldquo;all the steps&rdquo;.  This is an artificial
example, of course, since there are other, simpler ways of achieving the
same end.
</p>
<p>The function is defined as follows:
</p>
<div class="example">
<pre class="example">&gt; twosam &lt;- function(y1, y2) {
    n1  &lt;- length(y1); n2  &lt;- length(y2)
    yb1 &lt;- mean(y1);   yb2 &lt;- mean(y2)
    s1  &lt;- var(y1);    s2  &lt;- var(y2)
    s &lt;- ((n1-1)*s1 + (n2-1)*s2)/(n1+n2-2)
    tst &lt;- (yb1 - yb2)/sqrt(s*(1/n1 + 1/n2))
    tst
  }
</pre></div>

<p>With this function defined, you could perform two sample <em>t</em>-tests
using a call such as
</p>
<div class="example">
<pre class="example">&gt; tstat &lt;- twosam(data$male, data$female); tstat
</pre></div>

<p>As a second example, consider a function to emulate directly the
<small>MATLAB</small> backslash command, which returns the coefficients of the
orthogonal projection of the vector <em>y</em> onto the column space of
the matrix, <em>X</em>.  (This is ordinarily called the least squares
estimate of the regression coefficients.)  This would ordinarily be
done with the <code>qr()</code> function; however this is sometimes a bit
tricky to use directly and it pays to have a simple function such as the
following to use it safely.
</p>
<p>Thus given a <em>n</em> by <em>1</em> vector <em>y</em> and an <em>n</em> by
<em>p</em> matrix <em>X</em> then <em>X \ y</em> is defined as
(X&rsquo;X)^{-}X&rsquo;y, where (X&rsquo;X)^{-}
is a generalized inverse of <em>X'X</em>.
</p>
<div class="example">
<pre class="example">&gt; bslash &lt;- function(X, y) {
  X &lt;- qr(X)
  qr.coef(X, y)
}
</pre></div>

<p>After this object is created it may be used in statements such as
</p>
<div class="example">
<pre class="example">&gt; regcoeff &lt;- bslash(Xmat, yvar)
</pre></div>

<p>and so on.
</p>
<p>The classical R function <code>lsfit()</code> does this job quite well, and
more<a name="DOCF22" href="#FOOT22"><sup>22</sup></a>.  It in turn uses the functions <code>qr()</code> and <code>qr.coef()</code>
in the slightly counterintuitive way above to do this part of the
calculation.  Hence there is probably some value in having just this
part isolated in a simple to use function if it is going to be in
frequent use.  If so, we may wish to make it a matrix binary operator
for even more convenient use.
</p>
<hr>
<a name="Defining-new-binary-operators"></a>
<div class="header">
<p>
Next: <a href="#Named-arguments-and-defaults" accesskey="n" rel="next">Named arguments and defaults</a>, Previous: <a href="#Simple-examples" accesskey="p" rel="prev">Simple examples</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Defining-new-binary-operators-1"></a>
<h3 class="section">10.2 Defining new binary operators</h3>
<a name="index-Binary-operators"></a>

<p>Had we given the <code>bslash()</code> function a different name, namely one of
the form
</p>
<div class="example">
<pre class="example">%<var>anything</var>%
</pre></div>

<p>it could have been used as a <em>binary operator</em> in expressions
rather than in function form.  Suppose, for example, we choose <code>!</code>
for the internal character.  The function definition would then start as
</p>
<div class="example">
<pre class="example">&gt; &quot;%!%&quot; &lt;- function(X, y) { &hellip; }
</pre></div>

<p>(Note the use of quote marks.)  The function could then be used as
<code>X %!% y</code>.  (The backslash symbol itself is not a convenient choice
as it presents special problems in this context.)
</p>
<p>The matrix multiplication operator, <code>%*%</code>, and the outer product
matrix operator <code>%o%</code> are other examples of binary operators
defined in this way.
</p>
<hr>
<a name="Named-arguments-and-defaults"></a>
<div class="header">
<p>
Next: <a href="#The-three-dots-argument" accesskey="n" rel="next">The three dots argument</a>, Previous: <a href="#Defining-new-binary-operators" accesskey="p" rel="prev">Defining new binary operators</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Named-arguments-and-defaults-1"></a>
<h3 class="section">10.3 Named arguments and defaults</h3>
<a name="index-Named-arguments"></a>
<a name="index-Default-values"></a>

<p>As first noted in <a href="#Generating-regular-sequences">Generating regular sequences</a>, if arguments to
called functions are given in the &ldquo;<code><var>name</var>=<var>object</var></code>&rdquo;
form, they may be given in any order.  Furthermore the argument sequence
may begin in the unnamed, positional form, and specify named arguments
after the positional arguments.
</p>
<p>Thus if there is a function <code>fun1</code> defined by
</p>
<div class="example">
<pre class="example">&gt; fun1 &lt;- function(data, data.frame, graph, limit) {
    <span class="roman">[function body omitted]</span>
  }
</pre></div>

<p>then the function may be invoked in several ways, for example
</p>
<div class="example">
<pre class="example">&gt; ans &lt;- fun1(d, df, TRUE, 20)
&gt; ans &lt;- fun1(d, df, graph=TRUE, limit=20)
&gt; ans &lt;- fun1(data=d, limit=20, graph=TRUE, data.frame=df)
</pre></div>

<p>are all equivalent.
</p>
<p>In many cases arguments can be given commonly appropriate default
values, in which case they may be omitted altogether from the call when
the defaults are appropriate.  For example, if <code>fun1</code> were defined
as
</p>
<div class="example">
<pre class="example">&gt; fun1 &lt;- function(data, data.frame, graph=TRUE, limit=20) { &hellip; }
</pre></div>

<p>it could be called as
</p>
<div class="example">
<pre class="example">&gt; ans &lt;- fun1(d, df)
</pre></div>

<p>which is now equivalent to the three cases above, or as
</p>
<div class="example">
<pre class="example">&gt; ans &lt;- fun1(d, df, limit=10)
</pre></div>

<p>which changes one of the defaults.
</p>
<p>It is important to note that defaults may be arbitrary expressions, even
involving other arguments to the same function; they are not restricted
to be constants as in our simple example here.
</p>
<hr>
<a name="The-three-dots-argument"></a>
<div class="header">
<p>
Next: <a href="#Assignment-within-functions" accesskey="n" rel="next">Assignment within functions</a>, Previous: <a href="#Named-arguments-and-defaults" accesskey="p" rel="prev">Named arguments and defaults</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-_2026-argument"></a>
<h3 class="section">10.4 The &lsquo;<samp>&hellip;</samp>&rsquo; argument</h3>


<p>Another frequent requirement is to allow one function to pass on
argument settings to another.  For example many graphics functions use
the function <code>par()</code> and functions like <code>plot()</code> allow the
user to pass on graphical parameters to <code>par()</code> to control the
graphical output.  (See <a href="#The-par_0028_0029-function">The par() function</a>, for more details on the
<code>par()</code> function.)  This can be done by including an extra
argument, literally &lsquo;<samp>&hellip;</samp>&rsquo;, of the function, which may then be
passed on.  An outline example is given below.
</p>
<div class="example">
<pre class="example">fun1 &lt;- function(data, data.frame, graph=TRUE, limit=20, ...) {
  <span class="roman">[omitted statements]</span>
  if (graph)
    par(pch=&quot;*&quot;, ...)
  <span class="roman">[more omissions]</span>
}
</pre></div>

<p>Less frequently, a function will need to refer to components of
&lsquo;<samp>&hellip;</samp>&rsquo;.  The expression <code>list(...)</code> evaluates all such
arguments and returns them in a named list, while <code>..1</code>,
<code>..2</code>, etc. evaluate them one at a time, with &lsquo;<samp>..n</samp>&rsquo;
returning the n&rsquo;th unmatched argument.
</p>
<hr>
<a name="Assignment-within-functions"></a>
<div class="header">
<p>
Next: <a href="#More-advanced-examples" accesskey="n" rel="next">More advanced examples</a>, Previous: <a href="#The-three-dots-argument" accesskey="p" rel="prev">The three dots argument</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Assignments-within-functions"></a>
<h3 class="section">10.5 Assignments within functions</h3>

<p>Note that <em>any ordinary assignments done within the function are
local and temporary and are lost after exit from the function</em>.  Thus
the assignment <code>X &lt;- qr(X)</code> does not affect the value of the
argument in the calling program.
</p>
<p>To understand completely the rules governing the scope of R assignments
the reader needs to be familiar with the notion of an evaluation
<em>frame</em>.  This is a somewhat advanced, though hardly difficult,
topic and is not covered further here.
</p>
<p>If global and permanent assignments are intended within a function, then
either the &ldquo;superassignment&rdquo; operator, <code>&lt;&lt;-</code> or the function
<code>assign()</code> can be used.  See the <code>help</code> document for details.
<small>S-PLUS</small> users should be aware that <code>&lt;&lt;-</code> has different semantics
in R.  These are discussed further in <a href="#Scope">Scope</a>.
</p>
<hr>
<a name="More-advanced-examples"></a>
<div class="header">
<p>
Next: <a href="#Scope" accesskey="n" rel="next">Scope</a>, Previous: <a href="#Assignment-within-functions" accesskey="p" rel="prev">Assignment within functions</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="More-advanced-examples-1"></a>
<h3 class="section">10.6 More advanced examples</h3>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Efficiency-factors-in-block-designs" accesskey="1">Efficiency factors in block designs</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Dropping-all-names-in-a-printed-array" accesskey="2">Dropping all names in a printed array</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Recursive-numerical-integration" accesskey="3">Recursive numerical integration</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Efficiency-factors-in-block-designs"></a>
<div class="header">
<p>
Next: <a href="#Dropping-all-names-in-a-printed-array" accesskey="n" rel="next">Dropping all names in a printed array</a>, Previous: <a href="#More-advanced-examples" accesskey="p" rel="prev">More advanced examples</a>, Up: <a href="#More-advanced-examples" accesskey="u" rel="up">More advanced examples</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Efficiency-factors-in-block-designs-1"></a>
<h4 class="subsection">10.6.1 Efficiency factors in block designs</h4>

<p>As a more complete, if a little pedestrian, example of a function,
consider finding the efficiency factors for a block design.  (Some
aspects of this problem have already been discussed in <a href="#Index-matrices">Index matrices</a>.)
</p>
<p>A block design is defined by two factors, say <code>blocks</code> (<code>b</code>
levels) and <code>varieties</code> (<code>v</code> levels).  If <em>R</em> and
<em>K</em> are the <em>v</em> by <em>v</em> and <em>b</em> by <em>b</em>
<em>replications</em> and <em>block size</em> matrices, respectively, and
<em>N</em> is the <em>b</em> by <em>v</em> incidence matrix, then the
efficiency factors are defined as the eigenvalues of the matrix
E = I_v - R^{-1/2}N&rsquo;K^{-1}NR^{-1/2} = I_v - A&rsquo;A, where
A = K^{-1/2}NR^{-1/2}.
One way to write the function is given below.
</p>
<div class="example">
<pre class="example">&gt; bdeff &lt;- function(blocks, varieties) {
    blocks &lt;- as.factor(blocks)             # <span class="roman">minor safety move</span>
    b &lt;- length(levels(blocks))
    varieties &lt;- as.factor(varieties)       # <span class="roman">minor safety move</span>
    v &lt;- length(levels(varieties))
    K &lt;- as.vector(table(blocks))           # <span class="roman">remove dim attr</span>
    R &lt;- as.vector(table(varieties))        # <span class="roman">remove dim attr</span>
    N &lt;- table(blocks, varieties)
    A &lt;- 1/sqrt(K) * N * rep(1/sqrt(R), rep(b, v))
    sv &lt;- svd(A)
    list(eff=1 - sv$d^2, blockcv=sv$u, varietycv=sv$v)
}
</pre></div>

<p>It is numerically slightly better to work with the singular value
decomposition on this occasion rather than the eigenvalue routines.
</p>
<p>The result of the function is a list giving not only the efficiency
factors as the first component, but also the block and variety canonical
contrasts, since sometimes these give additional useful qualitative
information.
</p>
<hr>
<a name="Dropping-all-names-in-a-printed-array"></a>
<div class="header">
<p>
Next: <a href="#Recursive-numerical-integration" accesskey="n" rel="next">Recursive numerical integration</a>, Previous: <a href="#Efficiency-factors-in-block-designs" accesskey="p" rel="prev">Efficiency factors in block designs</a>, Up: <a href="#More-advanced-examples" accesskey="u" rel="up">More advanced examples</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Dropping-all-names-in-a-printed-array-1"></a>
<h4 class="subsection">10.6.2 Dropping all names in a printed array</h4>

<p>For printing purposes with large matrices or arrays, it is often useful
to print them in close block form without the array names or numbers.
Removing the <code>dimnames</code> attribute will not achieve this effect, but
rather the array must be given a <code>dimnames</code> attribute consisting of
empty strings.  For example to print a matrix, <code>X</code>
</p>
<div class="example">
<pre class="example">&gt; temp &lt;- X
&gt; dimnames(temp) &lt;- list(rep(&quot;&quot;, nrow(X)), rep(&quot;&quot;, ncol(X)))
&gt; temp; rm(temp)
</pre></div>

<p>This can be much more conveniently done using a function,
<code>no.dimnames()</code>, shown below, as a &ldquo;wrap around&rdquo; to achieve the
same result.  It also illustrates how some effective and useful user
functions can be quite short.
</p>
<div class="example">
<pre class="example">no.dimnames &lt;- function(a) {
  ## <span class="roman">Remove all dimension names from an array for compact printing.</span>
  d &lt;- list()
  l &lt;- 0
  for(i in dim(a)) {
    d[[l &lt;- l + 1]] &lt;- rep(&quot;&quot;, i)
  }
  dimnames(a) &lt;- d
  a
}
</pre></div>

<p>With this function defined, an array may be printed in close format
using
</p>
<div class="example">
<pre class="example">&gt; no.dimnames(X)
</pre></div>

<p>This is particularly useful for large integer arrays, where patterns are
the real interest rather than the values.
</p>
<hr>
<a name="Recursive-numerical-integration"></a>
<div class="header">
<p>
Previous: <a href="#Dropping-all-names-in-a-printed-array" accesskey="p" rel="prev">Dropping all names in a printed array</a>, Up: <a href="#More-advanced-examples" accesskey="u" rel="up">More advanced examples</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Recursive-numerical-integration-1"></a>
<h4 class="subsection">10.6.3 Recursive numerical integration</h4>

<p>Functions may be recursive, and may themselves define functions within
themselves.  Note, however, that such functions, or indeed variables,
are not inherited by called functions in higher evaluation frames as
they would be if they were on the search path.
</p>
<p>The example below shows a naive way of performing one-dimensional
numerical integration.  The integrand is evaluated at the end points of
the range and in the middle.  If the one-panel trapezium rule answer is
close enough to the two panel, then the latter is returned as the value.
Otherwise the same process is recursively applied to each panel.  The
result is an adaptive integration process that concentrates function
evaluations in regions where the integrand is farthest from linear.
There is, however, a heavy overhead, and the function is only
competitive with other algorithms when the integrand is both smooth and
very difficult to evaluate.
</p>
<p>The example is also given partly as a little puzzle in R programming.
</p>
<div class="example">
<pre class="example">area &lt;- function(f, a, b, eps = 1.0e-06, lim = 10) {
  fun1 &lt;- function(f, a, b, fa, fb, a0, eps, lim, fun) {
    ## <span class="roman">function &lsquo;fun1&rsquo; is only visible inside &lsquo;area&rsquo;</span>
    d &lt;- (a + b)/2
    h &lt;- (b - a)/4
    fd &lt;- f(d)
    a1 &lt;- h * (fa + fd)
    a2 &lt;- h * (fd + fb)
    if(abs(a0 - a1 - a2) &lt; eps || lim == 0)
      return(a1 + a2)
    else {
      return(fun(f, a, d, fa, fd, a1, eps, lim - 1, fun) +
             fun(f, d, b, fd, fb, a2, eps, lim - 1, fun))
    }
  }
  fa &lt;- f(a)
  fb &lt;- f(b)
  a0 &lt;- ((fa + fb) * (b - a))/2
  fun1(f, a, b, fa, fb, a0, eps, lim, fun1)
}
</pre></div>

<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Scope" accesskey="1">Scope</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Object-orientation" accesskey="2">Object orientation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Scope"></a>
<div class="header">
<p>
Next: <a href="#Customizing-the-environment" accesskey="n" rel="next">Customizing the environment</a>, Previous: <a href="#More-advanced-examples" accesskey="p" rel="prev">More advanced examples</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Scope-1"></a>
<h3 class="section">10.7 Scope</h3>
<a name="index-Scope"></a>

<p>The discussion in this section is somewhat more technical than in other
parts of this document.  However, it details one of the major differences
between <small>S-PLUS</small> and R.
</p>
<p>The symbols which occur in the body of a function can be divided into
three classes; formal parameters, local variables and free variables.
The formal parameters of a function are those occurring in the argument
list of the function.  Their values are determined by the process of
<em>binding</em> the actual function arguments to the formal parameters.
Local variables are those whose values are determined by the evaluation
of expressions in the body of the functions.  Variables which are not
formal parameters or local variables are called free variables.  Free
variables become local variables if they are assigned to.  Consider the
following function definition.
</p>
<div class="example">
<pre class="example">f &lt;- function(x) {
  y &lt;- 2*x
  print(x)
  print(y)
  print(z)
}
</pre></div>

<p>In this function, <code>x</code> is a formal parameter, <code>y</code> is a local
variable and <code>z</code> is a free variable.
</p>
<p>In R the free variable bindings are resolved by first looking in the
environment in which the function was created.  This is called
<em>lexical scope</em>.  First we define a function called <code>cube</code>.
</p>
<div class="example">
<pre class="example">cube &lt;- function(n) {
  sq &lt;- function() n*n
  n*sq()
}
</pre></div>

<p>The variable <code>n</code> in the function <code>sq</code> is not an argument to that
function.  Therefore it is a free variable and the scoping rules must be
used to ascertain the value that is to be associated with it.  Under static
scope (<small>S-PLUS</small>) the value is that associated with a global variable named
<code>n</code>.  Under lexical scope (R) it is the parameter to the function
<code>cube</code> since that is the active binding for the variable <code>n</code> at
the time the function <code>sq</code> was defined.  The difference between
evaluation in R and evaluation in <small>S-PLUS</small> is that <small>S-PLUS</small> looks for a
global variable called <code>n</code> while R first looks for a variable
called <code>n</code> in the environment created when <code>cube</code> was invoked.
</p>
<div class="example">
<pre class="example">## <span class="roman">first evaluation in S</span>
S&gt; cube(2)
Error in sq(): Object &quot;n&quot; not found
Dumped
S&gt; n &lt;- 3
S&gt; cube(2)
[1] 18
## <span class="roman">then the same function evaluated in R</span>
R&gt; cube(2)
[1] 8
</pre></div>

<p>Lexical scope can also be used to give functions <em>mutable state</em>.
In the following example we show how R can be used to mimic a bank
account.  A functioning bank account needs to have a balance or total, a
function for making withdrawals, a function for making deposits and a
function for stating the current balance.  We achieve this by creating
the three functions within <code>account</code> and then returning a list
containing them.  When <code>account</code> is invoked it takes a numerical
argument <code>total</code> and returns a list containing the three functions.
Because these functions are defined in an environment which contains
<code>total</code>, they will have access to its value.
</p>
<p>The special assignment operator, <code>&lt;&lt;-</code>,
<a name="index-_003c_003c_002d"></a>
is used to change the value associated with <code>total</code>.  This operator
looks back in enclosing environments for an environment that contains
the symbol <code>total</code> and when it finds such an environment it
replaces the value, in that environment, with the value of right hand
side.  If the global or top-level environment is reached without finding
the symbol <code>total</code> then that variable is created and assigned to
there.  For most users <code>&lt;&lt;-</code> creates a global variable and assigns
the value of the right hand side to it<a name="DOCF23" href="#FOOT23"><sup>23</sup></a>.  Only when <code>&lt;&lt;-</code> has
been used in a function that was returned as the value of another
function will the special behavior described here occur.
</p>
<div class="example">
<pre class="example">open.account &lt;- function(total) {
  list(
    deposit = function(amount) {
      if(amount &lt;= 0)
        stop(&quot;Deposits must be positive!\n&quot;)
      total &lt;&lt;- total + amount
      cat(amount, &quot;deposited.  Your balance is&quot;, total, &quot;\n\n&quot;)
    },
    withdraw = function(amount) {
      if(amount &gt; total)
        stop(&quot;You don't have that much money!\n&quot;)
      total &lt;&lt;- total - amount
      cat(amount, &quot;withdrawn.  Your balance is&quot;, total, &quot;\n\n&quot;)
    },
    balance = function() {
      cat(&quot;Your balance is&quot;, total, &quot;\n\n&quot;)
    }
  )
}

ross &lt;- open.account(100)
robert &lt;- open.account(200)

ross$withdraw(30)
ross$balance()
robert$balance()

ross$deposit(50)
ross$balance()
ross$withdraw(500)
</pre></div>

<hr>
<a name="Customizing-the-environment"></a>
<div class="header">
<p>
Next: <a href="#Object-orientation" accesskey="n" rel="next">Object orientation</a>, Previous: <a href="#Scope" accesskey="p" rel="prev">Scope</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Customizing-the-environment-1"></a>
<h3 class="section">10.8 Customizing the environment</h3>
<a name="index-Customizing-the-environment"></a>

<p>Users can customize their environment in several different ways.  There
is a site initialization file and every directory can have its own
special initialization file.  Finally, the special functions
<code>.First</code> and <code>.Last</code> can be used.
</p>
<p>The location of the site initialization file is taken from the value of
the <code>R_PROFILE</code> environment variable.  If that variable is unset,
the file <samp>Rprofile.site</samp> in the R home subdirectory <samp>etc</samp> is
used.  This file should contain the commands that you want to execute
every time R is started under your system.  A second, personal,
profile file named <samp>.Rprofile</samp><a name="DOCF24" href="#FOOT24"><sup>24</sup></a> can be placed in any directory.  If R is invoked in that
directory then that file will be sourced.  This file gives individual
users control over their workspace and allows for different startup
procedures in different working directories.  If no <samp>.Rprofile</samp>
file is found in the startup directory, then R looks for a
<samp>.Rprofile</samp> file in the user&rsquo;s home directory and uses that (if it
exists).  If the environment variable <code>R_PROFILE_USER</code> is set, the
file it points to is used instead of the <samp>.Rprofile</samp> files.
</p>
<p>Any function named <code>.First()</code> in either of the two profile files or
in the <samp>.RData</samp> image has a special status.  It is automatically
performed at the beginning of an R session and may be used to
initialize the environment.  For example, the definition in the example
below alters the prompt to <code>$</code> and sets up various other useful
things that can then be taken for granted in the rest of the session.
</p>
<p>Thus, the sequence in which files are executed is, <samp>Rprofile.site</samp>,
the user profile, <samp>.RData</samp> and then <code>.First()</code>.  A definition
in later files will mask definitions in earlier files.
</p>
<div class="example">
<pre class="example">&gt; .First &lt;- function() {
  options(prompt=&quot;$ &quot;, continue=&quot;+\t&quot;)  # <span class="roman"><code>$</code> is the prompt</span>
  options(digits=5, length=999)         # <span class="roman">custom numbers and printout</span>
  x11()                                 # <span class="roman">for graphics</span>
  par(pch = &quot;+&quot;)                        # <span class="roman">plotting character</span>
  source(file.path(Sys.getenv(&quot;HOME&quot;), &quot;R&quot;, &quot;mystuff.R&quot;))
                                        # <span class="roman">my personal functions</span>
  library(MASS)                         # <span class="roman">attach a package</span>
}
</pre></div>
<a name="index-_002eFirst"></a>

<p>Similarly a function <code>.Last()</code>, if defined, is (normally) executed
at the very end of the session.  An example is given below.
</p>
<div class="example">
<pre class="example">&gt; .Last &lt;- function() {
  graphics.off()                        # <span class="roman">a small safety measure.</span>
  cat(paste(date(),&quot;\nAdios\n&quot;))        # <span class="roman">Is it time for lunch?</span>
}
</pre></div>
<a name="index-_002eLast"></a>

<hr>
<a name="Object-orientation"></a>
<div class="header">
<p>
Previous: <a href="#Customizing-the-environment" accesskey="p" rel="prev">Customizing the environment</a>, Up: <a href="#Writing-your-own-functions" accesskey="u" rel="up">Writing your own functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Classes_002c-generic-functions-and-object-orientation"></a>
<h3 class="section">10.9 Classes, generic functions and object orientation</h3>
<a name="index-Classes-1"></a>
<a name="index-Generic-functions"></a>
<a name="index-Object-orientation"></a>

<p>The class of an object determines how it will be treated by what are
known as <em>generic</em> functions.  Put the other way round, a generic
function performs a task or action on its arguments <em>specific to
the class of the argument itself</em>.  If the argument lacks any <code>class</code>
attribute, or has a class not catered for specifically by the generic
function in question, there is always a <em>default action</em> provided.
</p>
<p>An example makes things clearer.  The class mechanism offers the user
the facility of designing and writing generic functions for special
purposes.  Among the other generic functions are <code>plot()</code> for
displaying objects graphically, <code>summary()</code> for summarizing
analyses of various types, and <code>anova()</code> for comparing statistical
models.
</p>
<p>The number of generic functions that can treat a class in a specific way
can be quite large.  For example, the functions that can accommodate in
some fashion objects of class <code>&quot;data.frame&quot;</code> include
</p>
<div class="example">
<pre class="example">[     [[&lt;-    any    as.matrix
[&lt;-   mean    plot   summary
</pre></div>

<a name="index-methods"></a>
<p>A currently complete list can be got by using the <code>methods()</code>
function:
</p>
<div class="example">
<pre class="example">&gt; methods(class=&quot;data.frame&quot;)
</pre></div>

<p>Conversely the number of classes a generic function can handle can also
be quite large.  For example the <code>plot()</code> function has a default
method and variants for objects of classes <code>&quot;data.frame&quot;</code>,
<code>&quot;density&quot;</code>, <code>&quot;factor&quot;</code>, and more.  A complete list can be got
again by using the <code>methods()</code> function:
</p>
<div class="example">
<pre class="example">&gt; methods(plot)
</pre></div>

<p>For many generic functions the function body is quite short, for example
</p>
<div class="example">
<pre class="example">&gt; coef
function (object, ...)
UseMethod(&quot;coef&quot;)
</pre></div>

<p>The presence of <code>UseMethod</code> indicates this is a generic function.
To see what methods are available we can use <code>methods()</code>
</p>
<div class="example">
<pre class="example">&gt; methods(coef)
[1] coef.aov*         coef.Arima*       coef.default*     coef.listof*
[5] coef.nls*         coef.summary.nls*

   Non-visible functions are asterisked
</pre></div>

<p>In this example there are six methods, none of which can be seen by
typing its name.  We can read these by either of
</p>
<a name="index-getAnywhere"></a>
<a name="index-getS3method"></a>
<div class="example">
<pre class="example">&gt; getAnywhere(&quot;coef.aov&quot;)
A single object matching ‘coef.aov’ was found
It was found in the following places
  registered S3 method for coef from namespace stats
  namespace:stats
with value

function (object, ...)
{
    z &lt;- object$coef
    z[!is.na(z)]
}

&gt; getS3method(&quot;coef&quot;, &quot;aov&quot;)
function (object, ...)
{
    z &lt;- object$coef
    z[!is.na(z)]
}
</pre></div>

<p>A function named <code><var>gen</var>.<var>cl</var></code> will be invoked by the
generic <code><var>gen</var></code> for class <code><var>cl</var></code>, so do not name
functions in this style unless they are intended to be methods.
</p>
<p>The reader is referred to the <em>R Language Definition</em> for a more
complete discussion of this mechanism.
</p>

<hr>
<a name="Statistical-models-in-R"></a>
<div class="header">
<p>
Next: <a href="#Graphics" accesskey="n" rel="next">Graphics</a>, Previous: <a href="#Writing-your-own-functions" accesskey="p" rel="prev">Writing your own functions</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Statistical-models-in-R-1"></a>
<h2 class="chapter">11 Statistical models in R</h2>
<a name="index-Statistical-models"></a>

<p>This section presumes the reader has some familiarity with statistical
methodology, in particular with regression analysis and the analysis of
variance.  Later we make some rather more ambitious presumptions, namely
that something is known about generalized linear models and nonlinear
regression.
</p>
<p>The requirements for fitting statistical models are sufficiently well
defined to make it possible to construct general tools that apply in a
broad spectrum of problems.
</p>
<p>R provides an interlocking suite of facilities that make fitting
statistical models very simple.  As we mention in the introduction, the
basic output is minimal, and one needs to ask for the details by calling
extractor functions.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Formulae-for-statistical-models" accesskey="1">Formulae for statistical models</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Linear-models" accesskey="2">Linear models</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Generic-functions-for-extracting-model-information" accesskey="3">Generic functions for extracting model information</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Analysis-of-variance-and-model-comparison" accesskey="4">Analysis of variance and model comparison</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Updating-fitted-models" accesskey="5">Updating fitted models</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Generalized-linear-models" accesskey="6">Generalized linear models</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Nonlinear-least-squares-and-maximum-likelihood-models" accesskey="7">Nonlinear least squares and maximum likelihood models</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Some-non_002dstandard-models" accesskey="8">Some non-standard models</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Formulae-for-statistical-models"></a>
<div class="header">
<p>
Next: <a href="#Linear-models" accesskey="n" rel="next">Linear models</a>, Previous: <a href="#Statistical-models-in-R" accesskey="p" rel="prev">Statistical models in R</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Defining-statistical-models_003b-formulae"></a>
<h3 class="section">11.1 Defining statistical models; formulae</h3>
<a name="index-Formulae"></a>

<p>The template for a statistical model is a linear regression model with
independent, homoscedastic errors
</p>
<div class="display">
<pre class="display">y_i = sum_{j=0}^p beta_j x_{ij} + e_i, &nbsp;&nbsp;&nbsp;&nbsp;i = 1, &hellip;, n,
</pre></div>
<p>where the e_i are NID(0, sigma^2).
In matrix terms this would be written
</p>
<div class="display">
<pre class="display">y = X &nbsp;beta + e
</pre></div>

<p>where the <em>y</em> is the response vector, <em>X</em> is the <em>model
matrix</em> or <em>design matrix</em> and has columns
<em>x_0, x_1, &hellip;, x_p</em>,
the determining variables.  Very often <em>x_0</em>
will be a column of ones defining an <em>intercept</em> term.
</p>
<a name="Examples"></a>
<h4 class="subsubheading">Examples</h4>

<p>Before giving a formal specification, a few examples may usefully set
the picture.
</p>
<p>Suppose <code>y</code>, <code>x</code>, <code>x0</code>, <code>x1</code>, <code>x2</code>, &hellip; are
numeric variables, <code>X</code> is a matrix and <code>A</code>, <code>B</code>,
<code>C</code>, &hellip; are factors.  The following formulae on the left
side below specify statistical models as described on the right.
</p>
<dl compact="compact">
<dt><code>y ~ x</code></dt>
<dt><code>y ~ 1 + x</code></dt>
<dd><p>Both imply the same simple linear regression model of <em>y</em> on
<em>x</em>.  The first has an implicit intercept term, and the second an
explicit one.
</p>
</dd>
<dt><code>y ~ 0 + x</code></dt>
<dt><code>y ~ -1 + x</code></dt>
<dt><code>y ~ x - 1</code></dt>
<dd><p>Simple linear regression of <em>y</em> on <em>x</em> through the origin
(that is, without an intercept term).
</p>
</dd>
<dt><code>log(y) ~ x1 + x2</code></dt>
<dd><p>Multiple regression of the transformed variable,
log(y),
on <em>x1</em> and <em>x2</em> (with an implicit intercept term).
</p>
</dd>
<dt><code>y ~ poly(x,2)</code></dt>
<dt><code>y ~ 1 + x + I(x^2)</code></dt>
<dd><p>Polynomial regression of <em>y</em> on <em>x</em> of degree 2.  The first
form uses orthogonal polynomials, and the second uses explicit powers,
as basis.
</p>
</dd>
<dt><code>y ~ X + poly(x,2)</code></dt>
<dd><p>Multiple regression <em>y</em> with model matrix consisting of the matrix
<em>X</em> as well as polynomial terms in <em>x</em> to degree 2.
</p>
</dd>
<dt><code>y ~ A</code></dt>
<dd><p>Single classification analysis of variance model of <em>y</em>, with
classes determined by <em>A</em>.
</p>
</dd>
<dt><code>y ~ A + x</code></dt>
<dd><p>Single classification analysis of covariance model of <em>y</em>, with
classes determined by <em>A</em>, and with covariate <em>x</em>.
</p>
</dd>
<dt><code>y ~ A*B</code></dt>
<dt><code>y ~ A + B + A:B</code></dt>
<dt><code>y ~ B %in% A</code></dt>
<dt><code>y ~ A/B</code></dt>
<dd><p>Two factor non-additive model of <em>y</em> on <em>A</em> and <em>B</em>.  The
first two specify the same crossed classification and the second two
specify the same nested classification.  In abstract terms all four
specify the same model subspace.
</p>
</dd>
<dt><code>y ~ (A + B + C)^2</code></dt>
<dt><code>y ~ A*B*C - A:B:C</code></dt>
<dd><p>Three factor experiment but with a model containing main effects and two
factor interactions only.  Both formulae specify the same model.
</p>
</dd>
<dt><code>y ~ A * x</code></dt>
<dt><code>y ~ A/x</code></dt>
<dt><code>y ~ A/(1 + x) - 1</code></dt>
<dd><p>Separate simple linear regression models of <em>y</em> on <em>x</em> within
the levels of <em>A</em>, with different codings.  The last form produces
explicit estimates of as many different intercepts and slopes as there
are levels in <em>A</em>.
</p>
</dd>
<dt><code>y ~ A*B + Error(C)</code></dt>
<dd><p>An experiment with two treatment factors, <em>A</em> and <em>B</em>, and
error strata determined by factor <em>C</em>.  For example a split plot
experiment, with whole plots (and hence also subplots), determined by
factor <em>C</em>.
</p></dd>
</dl>

<a name="index-_007e"></a>
<p>The operator <code>~</code> is used to define a <em>model formula</em> in R.
The form, for an ordinary linear model, is
</p>
<div class="example">
<pre class="example"><var>response</var> ~ <var>op_1</var> <var>term_1</var> <var>op_2</var> <var>term_2</var> <var>op_3</var> <var>term_3</var> <var>&hellip;</var>
</pre></div>

<p>where
</p>
<dl compact="compact">
<dt><var>response</var></dt>
<dd><p>is a vector or matrix, (or expression evaluating to a vector or matrix)
defining the response variable(s).
</p></dd>
<dt><var>op_i</var></dt>
<dd><p>is an operator, either <code>+</code> or <code>-</code>, implying the inclusion or
exclusion of a term in the model, (the first is optional).
</p></dd>
<dt><var>term_i</var></dt>
<dd><p>is either
</p><ul>
<li> a vector or matrix expression, or <code>1</code>,
</li><li> a factor, or
</li><li> a <em>formula expression</em> consisting of factors, vectors or matrices
connected by <em>formula operators</em>.
</li></ul>
<p>In all cases each term defines a collection of columns either to be
added to or removed from the model matrix.  A <code>1</code> stands for an
intercept column and is by default included in the model matrix unless
explicitly removed.
</p>
</dd>
</dl>

<p>The <em>formula operators</em> are similar in effect to the Wilkinson and
Rogers notation used by such programs as Glim and Genstat.  One
inevitable change is that the operator &lsquo;<samp><code>.</code></samp>&rsquo; becomes
&lsquo;<samp><code>:</code></samp>&rsquo; since the period is a valid name character in R.
</p>
<p>The notation is summarized below (based on Chambers &amp; Hastie, 1992,
p.29):
</p>
<dl compact="compact">
<dt><code><var>Y</var> ~ <var>M</var></code></dt>
<dd><p><var>Y</var> is modeled as <var>M</var>.
</p>
</dd>
<dt><code><var>M_1</var> + <var>M_2</var></code></dt>
<dd><p>Include <var>M_1</var> and <var>M_2</var>.
</p>
</dd>
<dt><code><var>M_1</var> - <var>M_2</var></code></dt>
<dd><p>Include <var>M_1</var> leaving out terms of <var>M_2</var>.
</p>
</dd>
<dt><code><var>M_1</var> : <var>M_2</var></code></dt>
<dd><p>The tensor product of <var>M_1</var> and <var>M_2</var>.  If both terms are
factors, then the &ldquo;subclasses&rdquo; factor.
</p>
</dd>
<dt><code><var>M_1</var> %in% <var>M_2</var></code></dt>
<dd><p>Similar to <code><var>M_1</var>:<var>M_2</var></code>, but with a different coding.
</p>
</dd>
<dt><code><var>M_1</var> * <var>M_2</var></code></dt>
<dd><p><code><var>M_1</var> + <var>M_2</var> + <var>M_1</var>:<var>M_2</var></code>.
</p>
</dd>
<dt><code><var>M_1</var> / <var>M_2</var></code></dt>
<dd><p><code><var>M_1</var> + <var>M_2</var> %in% <var>M_1</var></code>.
</p>
</dd>
<dt><code><var>M</var>^<var>n</var></code></dt>
<dd><p>All terms in <var>M</var> together with &ldquo;interactions&rdquo; up to order <var>n</var>
</p>
</dd>
<dt><code>I(<var>M</var>)</code></dt>
<dd><p>Insulate <var>M</var>.  Inside <var>M</var> all operators have their normal
arithmetic meaning, and that term appears in the model matrix.
</p></dd>
</dl>

<p>Note that inside the parentheses that usually enclose function arguments
all operators have their normal arithmetic meaning.  The function
<code>I()</code> is an identity function used to allow terms in model formulae
to be defined using arithmetic operators.
</p>
<p>Note particularly that the model formulae specify the <em>columns
of the model matrix</em>, the specification of the parameters being
implicit.  This is not the case in other contexts, for example in
specifying nonlinear models.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Contrasts" accesskey="1">Contrasts</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Contrasts"></a>
<div class="header">
<p>
Previous: <a href="#Formulae-for-statistical-models" accesskey="p" rel="prev">Formulae for statistical models</a>, Up: <a href="#Formulae-for-statistical-models" accesskey="u" rel="up">Formulae for statistical models</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Contrasts-1"></a>
<h4 class="subsection">11.1.1 Contrasts</h4>
<a name="index-Contrasts"></a>

<p>We need at least some idea how the model formulae specify the columns of
the model matrix.  This is easy if we have continuous variables, as each
provides one column of the model matrix (and the intercept will provide
a column of ones if included in the model).
</p>
<a name="index-Factors-1"></a>
<a name="index-Ordered-factors-1"></a>
<p>What about a <em>k</em>-level factor <code>A</code>?  The answer differs for
unordered and ordered factors.  For <em>unordered</em> factors <em>k -
1</em> columns are generated for the indicators of the second, &hellip;,
<em>k</em>th levels of the factor. (Thus the implicit parameterization is
to contrast the response at each level with that at the first.)  For
<em>ordered</em> factors the <em>k - 1</em> columns are the orthogonal
polynomials on <em>1, &hellip;, k</em>, omitting the constant term.
</p>
<p>Although the answer is already complicated, it is not the whole story.
First, if the intercept is omitted in a model that contains a factor
term, the first such term is encoded into <em>k</em> columns giving the
indicators for all the levels.  Second, the whole behavior can be
changed by the <code>options</code> setting for <code>contrasts</code>.  The default
setting in R is
</p>
<div class="example">
<pre class="example">options(contrasts = c(&quot;contr.treatment&quot;, &quot;contr.poly&quot;))
</pre></div>

<p>The main reason for mentioning this is that R and S have
different defaults for unordered factors, S using Helmert
contrasts.  So if you need to compare your results to those of a textbook
or paper which used <small>S-PLUS</small>, you will need to set
</p>
<div class="example">
<pre class="example">options(contrasts = c(&quot;contr.helmert&quot;, &quot;contr.poly&quot;))
</pre></div>

<p>This is a deliberate difference, as treatment contrasts (R&rsquo;s default)
are thought easier for newcomers to interpret.
</p>
<p>We have still not finished, as the contrast scheme to be used can be set
for each term in the model using the functions <code>contrasts</code> and
<code>C</code>.
<a name="index-contrasts"></a>
<a name="index-C"></a>
</p>
<p>We have not yet considered interaction terms: these generate the
products of the columns introduced for their component terms.
</p>
<p>Although the details are complicated, model formulae in R will
normally generate the models that an expert statistician would expect,
provided that marginality is preserved.  Fitting, for example, a model
with an interaction but not the corresponding main effects will in
general lead to surprising results, and is for experts only.
</p>

<hr>
<a name="Linear-models"></a>
<div class="header">
<p>
Next: <a href="#Generic-functions-for-extracting-model-information" accesskey="n" rel="next">Generic functions for extracting model information</a>, Previous: <a href="#Formulae-for-statistical-models" accesskey="p" rel="prev">Formulae for statistical models</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Linear-models-1"></a>
<h3 class="section">11.2 Linear models</h3>
<a name="index-Linear-models"></a>

<p>The basic function for fitting ordinary multiple models is <code>lm()</code>,
and a streamlined version of the call is as follows:
<a name="index-lm"></a>
</p>
<div class="example">
<pre class="example">&gt; <var>fitted.model</var> &lt;- lm(<var>formula</var>, data = <var>data.frame</var>)
</pre></div>

<p>For example
</p>
<div class="example">
<pre class="example">&gt; fm2 &lt;- lm(y ~ x1 + x2, data = production)
</pre></div>

<p>would fit a multiple regression model of <em>y</em> on <em>x1</em> and
<em>x2</em> (with implicit intercept term).
</p>
<p>The important (but technically optional) parameter <code>data =
production</code> specifies that any variables needed to construct the model
should come first from the <code>production</code> <em>data frame</em>.
<em>This is the case regardless of whether data frame
<code>production</code> has been attached on the search path or not</em>.
</p>
<hr>
<a name="Generic-functions-for-extracting-model-information"></a>
<div class="header">
<p>
Next: <a href="#Analysis-of-variance-and-model-comparison" accesskey="n" rel="next">Analysis of variance and model comparison</a>, Previous: <a href="#Linear-models" accesskey="p" rel="prev">Linear models</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Generic-functions-for-extracting-model-information-1"></a>
<h3 class="section">11.3 Generic functions for extracting model information</h3>

<p>The value of <code>lm()</code> is a fitted model object; technically a list of
results of class <code>&quot;lm&quot;</code>.  Information about the fitted model can
then be displayed, extracted, plotted and so on by using generic
functions that orient themselves to objects of class <code>&quot;lm&quot;</code>.  These
include
</p>
<div class="example">
<pre class="example">add1    deviance   formula      predict  step
alias   drop1      kappa        print    summary
anova   effects    labels       proj     vcov
coef    family     plot         residuals
</pre></div>

<p>A brief description of the most commonly used ones is given below.
</p>
<dl compact="compact">
<dd><a name="index-anova"></a>
</dd>
<dt><code>anova(<var>object_1</var>, <var>object_2</var>)</code></dt>
<dd><p>Compare a submodel with an outer model and produce an analysis of
variance table.
</p>
<a name="index-coefficients"></a>
<a name="index-coef"></a>
</dd>
<dt><code>coef(<var>object</var>)</code></dt>
<dd><p>Extract the regression coefficient (matrix).
</p>
<p>Long form: <code>coefficients(<var>object</var>)</code>.
</p>
<a name="index-deviance"></a>
</dd>
<dt><code>deviance(<var>object</var>)</code></dt>
<dd><p>Residual sum of squares, weighted if appropriate.
</p>
<a name="index-formula"></a>
</dd>
<dt><code>formula(<var>object</var>)</code></dt>
<dd><p>Extract the model formula.
</p>
<a name="index-plot"></a>
</dd>
<dt><code>plot(<var>object</var>)</code></dt>
<dd><p>Produce four plots, showing residuals, fitted values and some
diagnostics.
</p>
<a name="index-predict"></a>
</dd>
<dt><code>predict(<var>object</var>, newdata=<var>data.frame</var>)</code></dt>
<dd><p>The data frame supplied must have variables specified with the same
labels as the original.  The value is a vector or matrix of predicted
values corresponding to the determining variable values in
<var>data.frame</var>.
</p>

<a name="index-print"></a>
</dd>
<dt><code>print(<var>object</var>)</code></dt>
<dd><p>Print a concise version of the object.  Most often used implicitly.
</p>
<a name="index-residuals"></a>
<a name="index-resid"></a>
</dd>
<dt><code>residuals(<var>object</var>)</code></dt>
<dd><p>Extract the (matrix of) residuals, weighted as appropriate.
</p>
<p>Short form: <code>resid(<var>object</var>)</code>.
</p>
<a name="index-step"></a>
</dd>
<dt><code>step(<var>object</var>)</code></dt>
<dd><p>Select a suitable model by adding or dropping terms and preserving
hierarchies.  The model with the smallest value of AIC (Akaike&rsquo;s An
Information Criterion) discovered in the stepwise search is returned.
</p>
<a name="index-summary-1"></a>
</dd>
<dt><code>summary(<var>object</var>)</code></dt>
<dd><p>Print a comprehensive summary of the results of the regression analysis.
</p>
<a name="index-vcov"></a>
</dd>
<dt><code>vcov(<var>object</var>)</code></dt>
<dd><p>Returns the variance-covariance matrix of the main parameters of a
fitted model object.
</p></dd>
</dl>

<hr>
<a name="Analysis-of-variance-and-model-comparison"></a>
<div class="header">
<p>
Next: <a href="#Updating-fitted-models" accesskey="n" rel="next">Updating fitted models</a>, Previous: <a href="#Generic-functions-for-extracting-model-information" accesskey="p" rel="prev">Generic functions for extracting model information</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Analysis-of-variance-and-model-comparison-1"></a>
<h3 class="section">11.4 Analysis of variance and model comparison</h3>
<a name="index-Analysis-of-variance"></a>

<p>The model fitting function <code>aov(<var>formula</var>,
data=<var>data.frame</var>)</code>
<a name="index-aov"></a>
operates at the simplest level in a very similar way to the function
<code>lm()</code>, and most of the generic functions listed in the table in
<a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a> apply.
</p>
<p>It should be noted that in addition <code>aov()</code> allows an analysis of
models with multiple error strata such as split plot experiments, or
balanced incomplete block designs with recovery of inter-block
information.  The model formula
</p>
<div class="example">
<pre class="example"><var>response</var> ~ <var>mean.formula</var> + Error(<var>strata.formula</var>)
</pre></div>
<a name="index-Error"></a>

<p>specifies a multi-stratum experiment with error strata defined by the
<var>strata.formula</var>.  In the simplest case, <var>strata.formula</var> is
simply a factor, when it defines a two strata experiment, namely between
and within the levels of the factor.
</p>
<p>For example, with all determining variables factors, a model formula such
as that in:
</p>
<div class="example">
<pre class="example">&gt; fm &lt;- aov(yield ~ v + n*p*k + Error(farms/blocks), data=farm.data)
</pre></div>

<p>would typically be used to describe an experiment with mean model
<code>v + n*p*k</code> and three error strata, namely &ldquo;between farms&rdquo;,
&ldquo;within farms, between blocks&rdquo; and &ldquo;within blocks&rdquo;.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#ANOVA-tables" accesskey="1">ANOVA tables</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="ANOVA-tables"></a>
<div class="header">
<p>
Previous: <a href="#Analysis-of-variance-and-model-comparison" accesskey="p" rel="prev">Analysis of variance and model comparison</a>, Up: <a href="#Analysis-of-variance-and-model-comparison" accesskey="u" rel="up">Analysis of variance and model comparison</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="ANOVA-tables-1"></a>
<h4 class="subsection">11.4.1 ANOVA tables</h4>

<p>Note also that the analysis of variance table (or tables) are for a
sequence of fitted models.  The sums of squares shown are the decrease
in the residual sums of squares resulting from an inclusion of
<em>that term</em> in the model at <em>that place</em> in the sequence.
Hence only for orthogonal experiments will the order of inclusion be
inconsequential.
</p>
<p>For multistratum experiments the procedure is first to project the
response onto the error strata, again in sequence, and to fit the mean
model to each projection.  For further details, see Chambers &amp; Hastie
(1992).
</p>
<p>A more flexible alternative to the default full ANOVA table is to
compare two or more models directly using the <code>anova()</code> function.
<a name="index-anova-1"></a>
</p>
<div class="example">
<pre class="example">&gt; anova(<var>fitted.model.1</var>, <var>fitted.model.2</var>, &hellip;)
</pre></div>

<p>The display is then an ANOVA table showing the differences between the
fitted models when fitted in sequence.  The fitted models being compared
would usually be an hierarchical sequence, of course.  This does not
give different information to the default, but rather makes it easier to
comprehend and control.
</p>
<hr>
<a name="Updating-fitted-models"></a>
<div class="header">
<p>
Next: <a href="#Generalized-linear-models" accesskey="n" rel="next">Generalized linear models</a>, Previous: <a href="#Analysis-of-variance-and-model-comparison" accesskey="p" rel="prev">Analysis of variance and model comparison</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Updating-fitted-models-1"></a>
<h3 class="section">11.5 Updating fitted models</h3>
<a name="index-Updating-fitted-models"></a>

<p>The <code>update()</code> function is largely a convenience function that
allows a model to be fitted that differs from one previously fitted
usually by just a few additional or removed terms.  Its form is
<a name="index-update"></a>
</p>
<div class="example">
<pre class="example">&gt; <var>new.model</var> &lt;- update(<var>old.model</var>, <var>new.formula</var>)
</pre></div>

<p>In the <var>new.formula</var> the special name consisting of a period,
&lsquo;<samp><code>.</code></samp>&rsquo;,
<a name="index-_002e"></a>
only, can be used to stand for &ldquo;the corresponding part of the old model
formula&rdquo;.  For example,
</p>
<div class="example">
<pre class="example">&gt; fm05 &lt;- lm(y ~ x1 + x2 + x3 + x4 + x5, data = production)
&gt; fm6  &lt;- update(fm05, . ~ . + x6)
&gt; smf6 &lt;- update(fm6, sqrt(.) ~ .)
</pre></div>

<p>would fit a five variate multiple regression with variables (presumably)
from the data frame <code>production</code>, fit an additional model including
a sixth regressor variable, and fit a variant on the model where the
response had a square root transform applied.
</p>
<p>Note especially that if the <code>data=</code> argument is specified on the
original call to the model fitting function, this information is passed on
through the fitted model object to <code>update()</code> and its allies.
</p>
<p>The name &lsquo;<samp>.</samp>&rsquo; can also be used in other contexts, but with slightly
different meaning.  For example
</p>
<div class="example">
<pre class="example">&gt; fmfull &lt;- lm(y ~ . , data = production)
</pre></div>

<p>would fit a model with response <code>y</code> and regressor variables
<em>all other variables in the data frame <code>production</code></em>.
</p>
<p>Other functions for exploring incremental sequences of models are
<code>add1()</code>, <code>drop1()</code> and <code>step()</code>.
<a name="index-add1"></a>
<a name="index-drop1"></a>
<a name="index-step-1"></a>
The names of these give a good clue to their purpose, but for full
details see the on-line help.
</p>
<hr>
<a name="Generalized-linear-models"></a>
<div class="header">
<p>
Next: <a href="#Nonlinear-least-squares-and-maximum-likelihood-models" accesskey="n" rel="next">Nonlinear least squares and maximum likelihood models</a>, Previous: <a href="#Updating-fitted-models" accesskey="p" rel="prev">Updating fitted models</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Generalized-linear-models-1"></a>
<h3 class="section">11.6 Generalized linear models</h3>
<a name="index-Generalized-linear-models"></a>

<p>Generalized linear modeling is a development of linear models to
accommodate both non-normal response distributions and transformations
to linearity in a clean and straightforward way.  A generalized linear
model may be described in terms of the following sequence of
assumptions:
</p>
<ul>
<li> There is a response, <em>y</em>, of interest and stimulus variables
x_1, x_2, &hellip;,
whose values influence the distribution of the response.

</li><li> The stimulus variables influence the distribution of <em>y</em> through
<em>a single linear function, only</em>.  This linear function is called
the <em>linear predictor</em>, and is usually written
<div class="display">
<pre class="display">eta = beta_1 x_1 + beta_2 x_2 + &hellip; + beta_p x_p,
</pre></div>
<p>hence x_i has no influence on the distribution of <em>y</em> if and only if
beta_i is zero.
</p>
</li><li> The distribution of <em>y</em> is of the form
<div class="display">
<pre class="display">f_Y(y; mu, phi)
  = exp((A/phi) * (y lambda(mu) - gamma(lambda(mu))) + tau(y, phi))
</pre></div>
<p>where phi is a <em>scale parameter</em> (possibly known), and is constant
for all observations, <em>A</em> represents a prior weight, assumed known
but possibly varying with the observations, and $\mu$ is the mean of
<em>y</em>.
So it is assumed that the distribution of <em>y</em> is determined by its
mean and possibly a scale parameter as well.
</p>
</li><li> The mean, mu, is a smooth invertible function of the linear predictor:
<div class="display">
<pre class="display">mu = m(eta),    eta = m^{-1}(mu) = ell(mu)
</pre></div>
<p>and this inverse function, ell(), is called the <em>link function</em>.
</p>
</li></ul>

<p>These assumptions are loose enough to encompass a wide class of models
useful in statistical practice, but tight enough to allow the
development of a unified methodology of estimation and inference, at
least approximately.  The reader is referred to any of the current
reference works on the subject for full details, such as McCullagh &amp;
Nelder (1989) or Dobson (1990).
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Families" accesskey="1">Families</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#The-glm_0028_0029-function" accesskey="2">The glm() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Families"></a>
<div class="header">
<p>
Next: <a href="#The-glm_0028_0029-function" accesskey="n" rel="next">The glm() function</a>, Previous: <a href="#Generalized-linear-models" accesskey="p" rel="prev">Generalized linear models</a>, Up: <a href="#Generalized-linear-models" accesskey="u" rel="up">Generalized linear models</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Families-1"></a>
<h4 class="subsection">11.6.1 Families</h4>
<a name="index-Families"></a>

<p>The class of generalized linear models handled by facilities supplied in
R includes <em>gaussian</em>, <em>binomial</em>, <em>poisson</em>,
<em>inverse gaussian</em> and <em>gamma</em> response distributions and also
<em>quasi-likelihood</em> models where the response distribution is not
explicitly specified.  In the latter case the <em>variance function</em>
must be specified as a function of the mean, but in other cases this
function is implied by the response distribution.
</p>
<p>Each response distribution admits a variety of link functions to connect
the mean with the linear predictor.  Those automatically available are
shown in the following table:
</p>
<blockquote>
<table summary="">
<thead><tr><th width="25%">Family name</th><th width="55%">Link functions</th></tr></thead>
<tr><td width="25%"><code>binomial</code></td><td width="55%"><code>logit</code>, <code>probit</code>, <code>log</code>, <code>cloglog</code></td></tr>
<tr><td width="25%"><code>gaussian</code></td><td width="55%"><code>identity</code>, <code>log</code>, <code>inverse</code></td></tr>
<tr><td width="25%"><code>Gamma</code></td><td width="55%"><code>identity</code>, <code>inverse</code>, <code>log</code></td></tr>
<tr><td width="25%"><code>inverse.gaussian</code></td><td width="55%"><code>1/mu^2</code>, <code>identity</code>, <code>inverse</code>, <code>log</code></td></tr>
<tr><td width="25%"><code>poisson</code></td><td width="55%"><code>identity</code>, <code>log</code>, <code>sqrt</code></td></tr>
<tr><td width="25%"><code>quasi</code></td><td width="55%"><code>logit</code>, <code>probit</code>, <code>cloglog</code>,
<code>identity</code>, <code>inverse</code>, <code>log</code>, <code>1/mu^2</code>, <code>sqrt</code></td></tr>
</table>
</blockquote>

<p>The combination of a response distribution, a link function and various
other pieces of information that are needed to carry out the modeling
exercise is called the <em>family</em> of the generalized linear model.
</p>
<hr>
<a name="The-glm_0028_0029-function"></a>
<div class="header">
<p>
Previous: <a href="#Families" accesskey="p" rel="prev">Families</a>, Up: <a href="#Generalized-linear-models" accesskey="u" rel="up">Generalized linear models</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-glm_0028_0029-function-1"></a>
<h4 class="subsection">11.6.2 The <code>glm()</code> function</h4>
<a name="index-glm"></a>

<p>Since the distribution of the response depends on the stimulus variables
through a single linear function <em>only</em>, the same mechanism as was
used for linear models can still be used to specify the linear part of a
generalized model.  The family has to be specified in a different way.
</p>
<p>The R function to fit a generalized linear model is <code>glm()</code>
which uses the form
</p>
<div class="example">
<pre class="example">&gt; <var>fitted.model</var> &lt;- glm(<var>formula</var>, family=<var>family.generator</var>, data=<var>data.frame</var>)
</pre></div>

<p>The only new feature is the <var>family.generator</var>, which is the
instrument by which the family is described.  It is the name of a
function that generates a list of functions and expressions that
together define and control the model and estimation process.  Although
this may seem a little complicated at first sight, its use is quite
simple.
</p>
<p>The names of the standard, supplied family generators are given under
&ldquo;Family Name&rdquo; in the table in <a href="#Families">Families</a>.  Where there is a choice
of links, the name of the link may also be supplied with the family
name, in parentheses as a parameter.  In the case of the <code>quasi</code>
family, the variance function may also be specified in this way.
</p>
<p>Some examples make the process clear.
</p>
<a name="The-gaussian-family"></a>
<h4 class="subsubheading">The <code>gaussian</code> family</h4>

<p>A call such as
</p>
<div class="example">
<pre class="example">&gt; fm &lt;- glm(y ~ x1 + x2, family = gaussian, data = sales)
</pre></div>

<p>achieves the same result as
</p>
<div class="example">
<pre class="example">&gt; fm &lt;- lm(y ~ x1+x2, data=sales)
</pre></div>

<p>but much less efficiently.  Note how the gaussian family is not
automatically provided with a choice of links, so no parameter is
allowed.  If a problem requires a gaussian family with a nonstandard
link, this can usually be achieved through the <code>quasi</code> family, as
we shall see later.
</p>
<a name="The-binomial-family"></a>
<h4 class="subsubheading">The <code>binomial</code> family</h4>

<p>Consider a small, artificial example, from Silvey (1970).
</p>
<p>On the Aegean island of Kalythos the male inhabitants suffer from a
congenital eye disease, the effects of which become more marked with
increasing age.  Samples of islander males of various ages were tested
for blindness and the results recorded.  The data is shown below:
</p>
<table summary="">
<tr><td>Age:</td><td>20</td><td>35</td><td>45</td><td>55</td><td>70</td></tr>
<tr><td>No. tested:</td><td>50</td><td>50</td><td>50</td><td>50</td><td>50</td></tr>
<tr><td>No. blind:</td><td>&nbsp;6<!-- /@w --></td><td>17</td><td>26</td><td>37</td><td>44</td></tr>
</table>

<p>The problem we consider is to fit both logistic and probit models to
this data, and to estimate for each model the LD50, that is the age at
which the chance of blindness for a male inhabitant is 50%.
</p>
<p>If <em>y</em> is the number of blind at age <em>x</em> and <em>n</em> the
number tested, both models have the form
y ~ B(n, F(beta_0 + beta_1 x))
where for the probit case,
F(z) = Phi(z)
is the standard normal distribution function, and in the logit case
(the default),
F(z) = e^z/(1+e^z).
In both cases the LD50 is
LD50 = - beta_0/beta_1
that is, the point at which the argument of the distribution function is
zero.
</p>
<p>The first step is to set the data up as a data frame
</p>
<div class="example">
<pre class="example">&gt; kalythos &lt;- data.frame(x = c(20,35,45,55,70), n = rep(50,5),
                         y = c(6,17,26,37,44))
</pre></div>

<p>To fit a binomial model using <code>glm()</code> there are three possibilities
for the response:
</p>
<ul>
<li> If the response is a <em>vector</em> it is assumed to hold <em>binary</em>
data, and so must be a <em>0/1</em> vector.

</li><li> If the response is a <em>two-column matrix</em> it is assumed that the
first column holds the number of successes for the trial and the second
holds the number of failures.

</li><li> If the response is a <em>factor</em>, its first level is taken as failure
(0) and all other levels as &lsquo;success&rsquo; (1).
</li></ul>

<p>Here we need the second of these conventions, so we add a matrix to our
data frame:
</p>
<div class="example">
<pre class="example">&gt; kalythos$Ymat &lt;- cbind(kalythos$y, kalythos$n - kalythos$y)
</pre></div>

<p>To fit the models we use
</p>
<div class="example">
<pre class="example">&gt; fmp &lt;- glm(Ymat ~ x, family = binomial(link=probit), data = kalythos)
&gt; fml &lt;- glm(Ymat ~ x, family = binomial, data = kalythos)
</pre></div>

<p>Since the logit link is the default the parameter may be omitted on the
second call.  To see the results of each fit we could use
</p>
<div class="example">
<pre class="example">&gt; summary(fmp)
&gt; summary(fml)
</pre></div>

<p>Both models fit (all too) well.  To find the LD50 estimate we can use a
simple function:
</p>
<div class="example">
<pre class="example">&gt; ld50 &lt;- function(b) -b[1]/b[2]
&gt; ldp &lt;- ld50(coef(fmp)); ldl &lt;- ld50(coef(fml)); c(ldp, ldl)
</pre></div>

<p>The actual estimates from this data are 43.663 years and 43.601 years
respectively.
</p>
<a name="Poisson-models"></a>
<h4 class="subsubheading">Poisson models</h4>

<p>With the Poisson family the default link is the <code>log</code>, and in
practice the major use of this family is to fit surrogate Poisson
log-linear models to frequency data, whose actual distribution is often
multinomial.  This is a large and important subject we will not discuss
further here.  It even forms a major part of the use of non-gaussian
generalized models overall.
</p>
<p>Occasionally genuinely Poisson data arises in practice and in the past
it was often analyzed as gaussian data after either a log or a
square-root transformation.  As a graceful alternative to the latter, a
Poisson generalized linear model may be fitted as in the following
example:
</p>
<div class="example">
<pre class="example">&gt; fmod &lt;- glm(y ~ A + B + x, family = poisson(link=sqrt),
              data = worm.counts)
</pre></div>

<a name="Quasi_002dlikelihood-models"></a>
<h4 class="subsubheading">Quasi-likelihood models</h4>

<p>For all families the variance of the response will depend on the mean
and will have the scale parameter as a multiplier.  The form of
dependence of the variance on the mean is a characteristic of the
response distribution; for example for the poisson distribution
Var(y) = mu.
</p>
<p>For quasi-likelihood estimation and inference the precise response
distribution is not specified, but rather only a link function and the
form of the variance function as it depends on the mean.  Since
quasi-likelihood estimation uses formally identical techniques to those
for the gaussian distribution, this family provides a way of fitting
gaussian models with non-standard link functions or variance functions,
incidentally.
</p>
<p>For example, consider fitting the non-linear regression
y = theta_1 z_1 / (z_2 - theta_2) + e
which may be written alternatively as
y = 1 / (beta_1 x_1 + beta_2 x_2) + e
where
x_1 = z_2/z_1, x_2 = -1/z_1, beta_1 = 1/theta_1, and beta_2 =
theta_2/theta_1.
Supposing a suitable data frame to be set up we could fit this
non-linear regression as
</p>
<div class="example">
<pre class="example">&gt; nlfit &lt;- glm(y ~ x1 + x2 - 1,
               family = quasi(link=inverse, variance=constant),
               data = biochem)
</pre></div>

<p>The reader is referred to the manual and the help document for further
information, as needed.
</p>
<hr>
<a name="Nonlinear-least-squares-and-maximum-likelihood-models"></a>
<div class="header">
<p>
Next: <a href="#Some-non_002dstandard-models" accesskey="n" rel="next">Some non-standard models</a>, Previous: <a href="#Generalized-linear-models" accesskey="p" rel="prev">Generalized linear models</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Nonlinear-least-squares-and-maximum-likelihood-models-1"></a>
<h3 class="section">11.7 Nonlinear least squares and maximum likelihood models</h3>
<a name="index-Nonlinear-least-squares"></a>

<p>Certain forms of nonlinear model can be fitted by Generalized Linear
Models (<code>glm()</code>).  But in the majority of cases we have to approach
the nonlinear curve fitting problem as one of nonlinear optimization.
R&rsquo;s nonlinear optimization routines are <code>optim()</code>, <code>nlm()</code>
and <code>nlminb()</code>,
<a name="index-nlm"></a>
<a name="index-optim"></a>
<a name="index-nlminb"></a>
which provide the functionality (and more) of <small>S-PLUS</small>&rsquo;s <code>ms()</code> and
<code>nlminb()</code>.  We seek the parameter values that minimize some index
of lack-of-fit, and they do this by trying out various parameter values
iteratively.  Unlike linear regression for example, there is no
guarantee that the procedure will converge on satisfactory estimates.
All the methods require initial guesses about what parameter values to
try, and convergence may depend critically upon the quality of the
starting values.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Least-squares" accesskey="1">Least squares</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Maximum-likelihood" accesskey="2">Maximum likelihood</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Least-squares"></a>
<div class="header">
<p>
Next: <a href="#Maximum-likelihood" accesskey="n" rel="next">Maximum likelihood</a>, Previous: <a href="#Nonlinear-least-squares-and-maximum-likelihood-models" accesskey="p" rel="prev">Nonlinear least squares and maximum likelihood models</a>, Up: <a href="#Nonlinear-least-squares-and-maximum-likelihood-models" accesskey="u" rel="up">Nonlinear least squares and maximum likelihood models</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Least-squares-1"></a>
<h4 class="subsection">11.7.1 Least squares</h4>

<p>One way to fit a nonlinear model is by minimizing the sum of the squared
errors (SSE) or residuals.  This method makes sense if the observed
errors could have plausibly arisen from a normal distribution.
</p>
<p>Here is an example from Bates &amp; Watts (1988), page 51.  The data are:
</p>
<div class="example">
<pre class="example">&gt; x &lt;- c(0.02, 0.02, 0.06, 0.06, 0.11, 0.11, 0.22, 0.22, 0.56, 0.56,
         1.10, 1.10)
&gt; y &lt;- c(76, 47, 97, 107, 123, 139, 159, 152, 191, 201, 207, 200)
</pre></div>

<p>The fit criterion to be minimized is:
</p>
<div class="example">
<pre class="example">&gt; fn &lt;- function(p) sum((y - (p[1] * x)/(p[2] + x))^2)
</pre></div>

<p>In order to do the fit we need initial estimates of the parameters.  One
way to find sensible starting values is to plot the data, guess some
parameter values, and superimpose the model curve using those values.
</p>
<div class="example">
<pre class="example">&gt; plot(x, y)
&gt; xfit &lt;- seq(.02, 1.1, .05)
&gt; yfit &lt;- 200 * xfit/(0.1 + xfit)
&gt; lines(spline(xfit, yfit))
</pre></div>

<p>We could do better, but these starting values of 200 and 0.1 seem
adequate.  Now do the fit:
</p>
<div class="example">
<pre class="example">&gt; out &lt;- nlm(fn, p = c(200, 0.1), hessian = TRUE)
</pre></div>
<a name="index-nlm-1"></a>

<p>After the fitting, <code>out$minimum</code> is the SSE, and
<code>out$estimate</code> are the least squares estimates of the parameters.
To obtain the approximate standard errors (SE) of the estimates we do:
</p>
<div class="example">
<pre class="example">&gt; sqrt(diag(2*out$minimum/(length(y) - 2) * solve(out$hessian)))
</pre></div>

<p>The <code>2</code> which is subtracted in the line above represents the number
of parameters.  A 95% confidence interval would be the parameter
estimate +/- 1.96 SE.  We can superimpose the least squares
fit on a new plot:
</p>
<div class="example">
<pre class="example">&gt; plot(x, y)
&gt; xfit &lt;- seq(.02, 1.1, .05)
&gt; yfit &lt;- 212.68384222 * xfit/(0.06412146 + xfit)
&gt; lines(spline(xfit, yfit))
</pre></div>

<p>The standard package <strong>stats</strong> provides much more extensive facilities
for fitting non-linear models by least squares.  The model we have just
fitted is the Michaelis-Menten model, so we can use
</p>
<div class="example">
<pre class="example">&gt; df &lt;- data.frame(x=x, y=y)
&gt; fit &lt;- nls(y ~ SSmicmen(x, Vm, K), df)
&gt; fit
Nonlinear regression model
  model:  y ~ SSmicmen(x, Vm, K)
   data:  df
          Vm            K
212.68370711   0.06412123
 residual sum-of-squares:  1195.449
&gt; summary(fit)

Formula: y ~ SSmicmen(x, Vm, K)

Parameters:
    Estimate Std. Error t value Pr(&gt;|t|)
Vm 2.127e+02  6.947e+00  30.615 3.24e-11
K  6.412e-02  8.281e-03   7.743 1.57e-05

Residual standard error: 10.93 on 10 degrees of freedom

Correlation of Parameter Estimates:
      Vm
K 0.7651
</pre></div>

<hr>
<a name="Maximum-likelihood"></a>
<div class="header">
<p>
Previous: <a href="#Least-squares" accesskey="p" rel="prev">Least squares</a>, Up: <a href="#Nonlinear-least-squares-and-maximum-likelihood-models" accesskey="u" rel="up">Nonlinear least squares and maximum likelihood models</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Maximum-likelihood-1"></a>
<h4 class="subsection">11.7.2 Maximum likelihood</h4>
<a name="index-Maximum-likelihood"></a>

<p>Maximum likelihood is a method of nonlinear model fitting that applies
even if the errors are not normal.  The method finds the parameter values
which maximize the log likelihood, or equivalently which minimize the
negative log-likelihood.  Here is an example from Dobson (1990), pp.
108&ndash;111.  This example fits a logistic model to dose-response data,
which clearly could also be fit by <code>glm()</code>.  The data are:
</p>
<div class="example">
<pre class="example">&gt; x &lt;- c(1.6907, 1.7242, 1.7552, 1.7842, 1.8113,
         1.8369, 1.8610, 1.8839)
&gt; y &lt;- c( 6, 13, 18, 28, 52, 53, 61, 60)
&gt; n &lt;- c(59, 60, 62, 56, 63, 59, 62, 60)
</pre></div>

<p>The negative log-likelihood to minimize is:
</p>
<div class="example">
<pre class="example">&gt; fn &lt;- function(p)
   sum( - (y*(p[1]+p[2]*x) - n*log(1+exp(p[1]+p[2]*x))
           + log(choose(n, y)) ))
</pre></div>

<p>We pick sensible starting values and do the fit:
</p>
<div class="example">
<pre class="example">&gt; out &lt;- nlm(fn, p = c(-50,20), hessian = TRUE)
</pre></div>
<a name="index-nlm-2"></a>

<p>After the fitting, <code>out$minimum</code> is the negative log-likelihood,
and <code>out$estimate</code> are the maximum likelihood estimates of the
parameters.  To obtain the approximate SEs of the estimates we do:
</p>
<div class="example">
<pre class="example">&gt; sqrt(diag(solve(out$hessian)))
</pre></div>

<p>A 95% confidence interval would be the parameter estimate +/-
1.96 SE.
</p>
<hr>
<a name="Some-non_002dstandard-models"></a>
<div class="header">
<p>
Previous: <a href="#Nonlinear-least-squares-and-maximum-likelihood-models" accesskey="p" rel="prev">Nonlinear least squares and maximum likelihood models</a>, Up: <a href="#Statistical-models-in-R" accesskey="u" rel="up">Statistical models in R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Some-non_002dstandard-models-1"></a>
<h3 class="section">11.8 Some non-standard models</h3>

<p>We conclude this chapter with just a brief mention of some of the other
facilities available in R for special regression and data analysis
problems.
</p>
<ul>
<li> <a name="index-Mixed-models"></a>
<strong>Mixed models.</strong>  The recommended <a href="https://CRAN.R-project.org/package=nlme"><strong>nlme</strong></a> package provides
functions <code>lme()</code> and <code>nlme()</code>
<a name="index-lme"></a>
<a name="index-nlme"></a>
for linear and non-linear mixed-effects models, that is linear and
non-linear regressions in which some of the coefficients correspond to
random effects.  These functions make heavy use of formulae to specify
the models.

</li><li> <a name="index-Local-approximating-regressions"></a>
<strong>Local approximating regressions.</strong>  The <code>loess()</code>
<a name="index-loess"></a>
function fits a nonparametric regression by using a locally weighted
regression.  Such regressions are useful for highlighting a trend in
messy data or for data reduction to give some insight into a large data
set.

<p>Function <code>loess</code> is in the standard package <strong>stats</strong>, together
with code for projection pursuit regression.
<a name="index-loess-1"></a>
</p>
</li><li> <a name="index-Robust-regression"></a>
<strong>Robust regression.</strong> There are several functions available for
fitting regression models in a way resistant to the influence of extreme
outliers in the data.  Function <code>lqs</code>
<a name="index-lqs"></a>
in the recommended package <a href="https://CRAN.R-project.org/package=MASS"><strong>MASS</strong></a> provides state-of-art algorithms
for highly-resistant fits.  Less resistant but statistically more
efficient methods are available in packages, for example function
<code>rlm</code>
<a name="index-rlm"></a>
in package <a href="https://CRAN.R-project.org/package=MASS"><strong>MASS</strong></a>.

</li><li> <a name="index-Additive-models"></a>
<strong>Additive models.</strong> This technique aims to construct a regression
function from smooth additive functions of the determining variables,
usually one for each determining variable.  Functions <code>avas</code> and
<code>ace</code>
<a name="index-avas"></a>
<a name="index-ace"></a>
in package <a href="https://CRAN.R-project.org/package=acepack"><strong>acepack</strong></a> and functions <code>bruto</code> and <code>mars</code>
<a name="index-bruto"></a>
<a name="index-mars"></a>
in package <a href="https://CRAN.R-project.org/package=mda"><strong>mda</strong></a> provide some examples of these techniques in
user-contributed packages to R.  An extension is <strong>Generalized
Additive Models</strong>, implemented in user-contributed packages <a href="https://CRAN.R-project.org/package=gam"><strong>gam</strong></a> and
<a href="https://CRAN.R-project.org/package=mgcv"><strong>mgcv</strong></a>.

</li><li> <a name="index-Tree_002dbased-models"></a>
<strong>Tree-based models.</strong> Rather than seek an explicit global linear
model for prediction or interpretation, tree-based models seek to
bifurcate the data, recursively, at critical points of the determining
variables in order to partition the data ultimately into groups that are
as homogeneous as possible within, and as heterogeneous as possible
between.  The results often lead to insights that other data analysis
methods tend not to yield.

<p>Models are again specified in the ordinary linear model form.  The model
fitting function is <code>tree()</code>,
<a name="index-tree"></a>
but many other generic functions such as <code>plot()</code> and <code>text()</code>
are well adapted to displaying the results of a tree-based model fit in
a graphical way.
</p>
<p>Tree models are available in R <em>via</em> the user-contributed
packages <a href="https://CRAN.R-project.org/package=rpart"><strong>rpart</strong></a> and <a href="https://CRAN.R-project.org/package=tree"><strong>tree</strong></a>.
</p>
</li></ul>

<hr>
<a name="Graphics"></a>
<div class="header">
<p>
Next: <a href="#Packages" accesskey="n" rel="next">Packages</a>, Previous: <a href="#Statistical-models-in-R" accesskey="p" rel="prev">Statistical models in R</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Graphical-procedures"></a>
<h2 class="chapter">12 Graphical procedures</h2>

<p>Graphical facilities are an important and extremely versatile component
of the R environment.  It is possible to use the facilities to
display a wide variety of statistical graphs and also to build entirely
new types of graph.
</p>
<p>The graphics facilities can be used in both interactive and batch modes,
but in most cases, interactive use is more productive.  Interactive use
is also easy because at startup time R initiates a graphics
<em>device driver</em> which opens a special <em>graphics window</em> for
the display of interactive graphics.  Although this is done
automatically, it may useful to know that the command used is
<code>X11()</code> under UNIX, <code>windows()</code> under Windows and
<code>quartz()</code> under macOS.  A new device can always be opened by
<code>dev.new()</code>.
</p>
<p>Once the device driver is running, R plotting commands can be used to
produce a variety of graphical displays and to create entirely new kinds
of display.
</p>
<p>Plotting commands are divided into three basic groups:
</p>
<ul>
<li> <strong>High-level</strong> plotting functions create a new plot on the graphics
device, possibly with axes, labels, titles and so on.
</li><li> <strong>Low-level</strong> plotting functions add more information to an
existing plot, such as extra points, lines and labels.
</li><li> <strong>Interactive</strong> graphics functions allow you interactively add
information to, or extract information from, an existing plot, using a
pointing device such as a mouse.
</li></ul>

<p>In addition, R maintains a list of <em>graphical parameters</em> which
can be manipulated to customize your plots.
</p>
<p>This manual only describes what are known as &lsquo;base&rsquo; graphics.  A
separate graphics sub-system in package <strong>grid</strong> coexists with base &ndash;
it is more powerful but harder to use.  There is a recommended package
<a href="https://CRAN.R-project.org/package=lattice"><strong>lattice</strong></a> which builds on <strong>grid</strong> and provides ways to produce
multi-panel plots akin to those in the <em>Trellis</em> system in S.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#High_002dlevel-plotting-commands" accesskey="1">High-level plotting commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Low_002dlevel-plotting-commands" accesskey="2">Low-level plotting commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Interacting-with-graphics" accesskey="3">Interacting with graphics</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Using-graphics-parameters" accesskey="4">Using graphics parameters</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Graphics-parameters" accesskey="5">Graphics parameters</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Device-drivers" accesskey="6">Device drivers</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Dynamic-graphics" accesskey="7">Dynamic graphics</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="High_002dlevel-plotting-commands"></a>
<div class="header">
<p>
Next: <a href="#Low_002dlevel-plotting-commands" accesskey="n" rel="next">Low-level plotting commands</a>, Previous: <a href="#Graphics" accesskey="p" rel="prev">Graphics</a>, Up: <a href="#Graphics" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="High_002dlevel-plotting-commands-1"></a>
<h3 class="section">12.1 High-level plotting commands</h3>

<p>High-level plotting functions are designed to generate a complete plot
of the data passed as arguments to the function.  Where appropriate,
axes, labels and titles are automatically generated (unless you request
otherwise.) High-level plotting commands always start a new plot,
erasing the current plot if necessary.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-plot_0028_0029-function" accesskey="1">The plot() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Displaying-multivariate-data" accesskey="2">Displaying multivariate data</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Display-graphics" accesskey="3">Display graphics</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Arguments-to-high_002dlevel-plotting-functions" accesskey="4">Arguments to high-level plotting functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-plot_0028_0029-function"></a>
<div class="header">
<p>
Next: <a href="#Displaying-multivariate-data" accesskey="n" rel="next">Displaying multivariate data</a>, Previous: <a href="#High_002dlevel-plotting-commands" accesskey="p" rel="prev">High-level plotting commands</a>, Up: <a href="#High_002dlevel-plotting-commands" accesskey="u" rel="up">High-level plotting commands</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-plot_0028_0029-function-1"></a>
<h4 class="subsection">12.1.1 The <code>plot()</code> function</h4>
<a name="index-plot-1"></a>

<p>One of the most frequently used plotting functions in R is the
<code>plot()</code> function.  This is a <em>generic</em> function: the type of
plot produced is dependent on the type or <em>class</em> of the first
argument.
</p>
<dl compact="compact">
<dt><code>plot(<var>x</var>, <var>y</var>)</code></dt>
<dt><code>plot(<var>xy</var>)</code></dt>
<dd><p>If <var>x</var> and <var>y</var> are vectors, <code>plot(<var>x</var>, <var>y</var>)</code>
produces a scatterplot of <var>y</var> against <var>x</var>.  The same effect can
be produced by supplying one argument (second form) as either a list
containing two elements <var>x</var> and <var>y</var> or a two-column matrix.
</p>
</dd>
<dt><code>plot(<var>x</var>)</code></dt>
<dd><p>If <var>x</var> is a time series, this produces a time-series plot. If
<var>x</var> is a numeric vector, it produces a plot of the values in the
vector against their index in the vector.  If <var>x</var> is a complex
vector, it produces a plot of imaginary versus real parts of the vector
elements.
</p>
</dd>
<dt><code>plot(<var>f</var>)</code></dt>
<dt><code>plot(<var>f</var>, <var>y</var>)</code></dt>
<dd><p><var>f</var> is a factor object, <var>y</var> is a numeric vector.  The first form
generates a bar plot of <var>f</var>; the second form produces boxplots of
<var>y</var> for each level of <var>f</var>.
</p>
</dd>
<dt><code>plot(<var>df</var>)</code></dt>
<dt><code>plot(~ <var>expr</var>)</code></dt>
<dt><code>plot(<var>y</var> ~ <var>expr</var>)</code></dt>
<dd><p><var>df</var> is a data frame, <var>y</var> is any object, <var>expr</var> is a list
of object names separated by &lsquo;<code>+</code>&rsquo; (e.g., <code>a + b + c</code>).  The
first two forms produce distributional plots of the variables in a data
frame (first form) or of a number of named objects (second form).  The
third form plots <var>y</var> against every object named in <var>expr</var>.
</p></dd>
</dl>

<hr>
<a name="Displaying-multivariate-data"></a>
<div class="header">
<p>
Next: <a href="#Display-graphics" accesskey="n" rel="next">Display graphics</a>, Previous: <a href="#The-plot_0028_0029-function" accesskey="p" rel="prev">The plot() function</a>, Up: <a href="#High_002dlevel-plotting-commands" accesskey="u" rel="up">High-level plotting commands</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Displaying-multivariate-data-1"></a>
<h4 class="subsection">12.1.2 Displaying multivariate data</h4>

<p>R provides two very useful functions for representing multivariate
data.  If <code>X</code> is a numeric matrix or data frame, the command
</p>
<div class="example">
<pre class="example">&gt; pairs(X)
</pre></div>
<a name="index-pairs"></a>

<p>produces a pairwise scatterplot matrix of the variables defined by the
columns of <code>X</code>, that is, every column of <code>X</code> is plotted
against every other column of <code>X</code> and the resulting <em>n(n-1)</em>
plots are arranged in a matrix with plot scales constant over the rows
and columns of the matrix.
</p>
<p>When three or four variables are involved a <em>coplot</em> may be more
enlightening.  If <code>a</code> and <code>b</code> are numeric vectors and <code>c</code>
is a numeric vector or factor object (all of the same length), then
the command
</p>
<div class="example">
<pre class="example">&gt; coplot(a ~ b | c)
</pre></div>
<a name="index-coplot"></a>

<p>produces a number of scatterplots of <code>a</code> against <code>b</code> for given
values of <code>c</code>.  If <code>c</code> is a factor, this simply means that
<code>a</code> is plotted against <code>b</code> for every level of <code>c</code>.  When
<code>c</code> is numeric, it is divided into a number of <em>conditioning
intervals</em> and for each interval <code>a</code> is plotted against <code>b</code>
for values of <code>c</code> within the interval.  The number and position of
intervals can be controlled with <code>given.values=</code> argument to
<code>coplot()</code>&mdash;the function <code>co.intervals()</code> is useful for
selecting intervals.  You can also use two <em>given</em> variables with a
command like
</p>
<div class="example">
<pre class="example">&gt; coplot(a ~ b | c + d)
</pre></div>

<p>which produces scatterplots of <code>a</code> against <code>b</code> for every joint
conditioning interval of <code>c</code> and <code>d</code>.
</p>
<p>The <code>coplot()</code> and <code>pairs()</code> function both take an argument
<code>panel=</code> which can be used to customize the type of plot which
appears in each panel.  The default is <code>points()</code> to produce a
scatterplot but by supplying some other low-level graphics function of
two vectors <code>x</code> and <code>y</code> as the value of <code>panel=</code> you can
produce any type of plot you wish.  An example panel function useful for
coplots is <code>panel.smooth()</code>.
</p>
<hr>
<a name="Display-graphics"></a>
<div class="header">
<p>
Next: <a href="#Arguments-to-high_002dlevel-plotting-functions" accesskey="n" rel="next">Arguments to high-level plotting functions</a>, Previous: <a href="#Displaying-multivariate-data" accesskey="p" rel="prev">Displaying multivariate data</a>, Up: <a href="#High_002dlevel-plotting-commands" accesskey="u" rel="up">High-level plotting commands</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Display-graphics-1"></a>
<h4 class="subsection">12.1.3 Display graphics</h4>

<p>Other high-level graphics functions produce different types of plots.
Some examples are:
</p>
<dl compact="compact">
<dt><code>qqnorm(x)</code></dt>
<dt><code>qqline(x)</code></dt>
<dt><code>qqplot(x, y)</code></dt>
<dd><a name="index-qqnorm-1"></a>
<a name="index-qqline-1"></a>
<a name="index-qqplot"></a>
<p>Distribution-comparison plots.  The first form plots the numeric vector
<code>x</code> against the expected Normal order scores (a normal scores plot)
and the second adds a straight line to such a plot by drawing a line
through the distribution and data quartiles.  The third form plots the
quantiles of <code>x</code> against those of <code>y</code> to compare their
respective distributions.
</p>
</dd>
<dt><code>hist(x)</code></dt>
<dt><code>hist(x, nclass=<var>n</var>)</code></dt>
<dt><code>hist(x, breaks=<var>b</var>, &hellip;)</code></dt>
<dd><a name="index-hist-1"></a>
<p>Produces a histogram of the numeric vector <code>x</code>.  A sensible number
of classes is usually chosen, but a recommendation can be given with the
<code>nclass=</code> argument.  Alternatively, the breakpoints can be
specified exactly with the <code>breaks=</code> argument.  If the
<code>probability=TRUE</code> argument is given, the bars represent relative
frequencies divided by bin width instead of counts.
</p>
</dd>
<dt><code>dotchart(x, &hellip;)</code></dt>
<dd><a name="index-dotchart"></a>
<p>Constructs a dotchart of the data in <code>x</code>.  In a dotchart the
<em>y</em>-axis gives a labelling of the data in <code>x</code> and the
<em>x</em>-axis gives its value.  For example it allows easy visual
selection of all data entries with values lying in specified ranges.
</p>
</dd>
<dt><code>image(x, y, z, &hellip;)</code></dt>
<dt><code>contour(x, y, z, &hellip;)</code></dt>
<dt><code>persp(x, y, z, &hellip;)</code></dt>
<dd><a name="index-image"></a>
<a name="index-contour"></a>
<a name="index-persp"></a>
<p>Plots of three variables.  The <code>image</code> plot draws a grid of rectangles
using different colours to represent the value of <code>z</code>, the <code>contour</code>
plot draws contour lines to represent the value of <code>z</code>, and the
<code>persp</code> plot draws a 3D surface.
</p></dd>
</dl>

<hr>
<a name="Arguments-to-high_002dlevel-plotting-functions"></a>
<div class="header">
<p>
Previous: <a href="#Display-graphics" accesskey="p" rel="prev">Display graphics</a>, Up: <a href="#High_002dlevel-plotting-commands" accesskey="u" rel="up">High-level plotting commands</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Arguments-to-high_002dlevel-plotting-functions-1"></a>
<h4 class="subsection">12.1.4 Arguments to high-level plotting functions</h4>

<p>There are a number of arguments which may be passed to high-level
graphics functions, as follows:
</p>
<dl compact="compact">
<dt><code>add=TRUE</code></dt>
<dd><p>Forces the function to act as a low-level graphics function,
superimposing the plot on the current plot (some functions only).
</p>
</dd>
<dt><code>axes=FALSE</code></dt>
<dd><p>Suppresses generation of axes&mdash;useful for adding your own custom axes
with the <code>axis()</code> function.  The default, <code>axes=TRUE</code>, means
include axes.
</p>
</dd>
<dt><code>log=&quot;x&quot;</code></dt>
<dt><code>log=&quot;y&quot;</code></dt>
<dt><code>log=&quot;xy&quot;</code></dt>
<dd><p>Causes the <em>x</em>, <em>y</em> or both axes to be logarithmic.  This will
work for many, but not all, types of plot.
</p>
</dd>
<dt><code>type=</code></dt>
<dd><p>The <code>type=</code> argument controls the type of plot produced, as
follows:
</p>
<dl compact="compact">
<dt><code>type=&quot;p&quot;</code></dt>
<dd><p>Plot individual points (the default)
</p></dd>
<dt><code>type=&quot;l&quot;</code></dt>
<dd><p>Plot lines
</p></dd>
<dt><code>type=&quot;b&quot;</code></dt>
<dd><p>Plot points connected by lines (<em>both</em>)
</p></dd>
<dt><code>type=&quot;o&quot;</code></dt>
<dd><p>Plot points overlaid by lines
</p></dd>
<dt><code>type=&quot;h&quot;</code></dt>
<dd><p>Plot vertical lines from points to the zero axis (<em>high-density</em>)
</p></dd>
<dt><code>type=&quot;s&quot;</code></dt>
<dt><code>type=&quot;S&quot;</code></dt>
<dd><p>Step-function plots.  In the first form, the top of the vertical defines
the point; in the second, the bottom.
</p></dd>
<dt><code>type=&quot;n&quot;</code></dt>
<dd><p>No plotting at all.  However axes are still drawn (by default) and the
coordinate system is set up according to the data.  Ideal for creating
plots with subsequent low-level graphics functions.
</p></dd>
</dl>

</dd>
<dt><code>xlab=<var>string</var></code></dt>
<dt><code>ylab=<var>string</var></code></dt>
<dd><p>Axis labels for the <em>x</em> and <em>y</em> axes.  Use these arguments to
change the default labels, usually the names of the objects used in the
call to the high-level plotting function.
</p>
</dd>
<dt><code>main=<var>string</var></code></dt>
<dd><p>Figure title, placed at the top of the plot in a large font.
</p>
</dd>
<dt><code>sub=<var>string</var></code></dt>
<dd><p>Sub-title, placed just below the <em>x</em>-axis in a smaller font.
</p></dd>
</dl>

<hr>
<a name="Low_002dlevel-plotting-commands"></a>
<div class="header">
<p>
Next: <a href="#Interacting-with-graphics" accesskey="n" rel="next">Interacting with graphics</a>, Previous: <a href="#High_002dlevel-plotting-commands" accesskey="p" rel="prev">High-level plotting commands</a>, Up: <a href="#Graphics" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Low_002dlevel-plotting-commands-1"></a>
<h3 class="section">12.2 Low-level plotting commands</h3>

<p>Sometimes the high-level plotting functions don&rsquo;t produce exactly the
kind of plot you desire.  In this case, low-level plotting commands can
be used to add extra information (such as points, lines or text) to the
current plot.
</p>
<p>Some of the more useful low-level plotting functions are:
</p>
<dl compact="compact">
<dt><code>points(x, y)</code></dt>
<dt><code>lines(x, y)</code></dt>
<dd><a name="index-points"></a>
<a name="index-lines"></a>
<p>Adds points or connected lines to the current plot.  <code>plot()</code>&rsquo;s
<code>type=</code> argument can also be passed to these functions (and
defaults to <code>&quot;p&quot;</code> for <code>points()</code> and <code>&quot;l&quot;</code> for
<code>lines()</code>.)
</p>
</dd>
<dt><code>text(x, y, labels, &hellip;)</code></dt>
<dd><a name="index-text"></a>
<p>Add text to a plot at points given by <code>x, y</code>.  Normally
<code>labels</code> is an integer or character vector in which case
<code>labels[i]</code> is plotted at point <code>(x[i], y[i])</code>.  The default
is <code>1:length(x)</code>.
</p>
<p><strong>Note</strong>: This function is often used in the sequence
</p>
<div class="example">
<pre class="example">&gt; plot(x, y, type=&quot;n&quot;); text(x, y, names)
</pre></div>

<p>The graphics parameter <code>type=&quot;n&quot;</code> suppresses the points but sets up
the axes, and the <code>text()</code> function supplies special characters, as
specified by the character vector <code>names</code> for the points.
</p>
</dd>
<dt><code>abline(a, b)</code></dt>
<dt><code>abline(h=<var>y</var>)</code></dt>
<dt><code>abline(v=<var>x</var>)</code></dt>
<dt><code>abline(<var>lm.obj</var>)</code></dt>
<dd><a name="index-abline"></a>
<p>Adds a line of slope <code>b</code> and intercept <code>a</code> to the current
plot.  <code>h=<var>y</var></code> may be used to specify <em>y</em>-coordinates for
the heights of horizontal lines to go across a plot, and
<code>v=<var>x</var></code> similarly for the <em>x</em>-coordinates for vertical
lines.  Also <var>lm.obj</var> may be list with a <code>coefficients</code>
component of length 2 (such as the result of model-fitting functions,)
which are taken as an intercept and slope, in that order.
</p>
</dd>
<dt><code>polygon(x, y, &hellip;)</code></dt>
<dd><a name="index-polygon"></a>
<p>Draws a polygon defined by the ordered vertices in (<code>x</code>, <code>y</code>)
and (optionally) shade it in with hatch lines, or fill it if the
graphics device allows the filling of figures.
</p>
</dd>
<dt><code>legend(x, y, legend, &hellip;)</code></dt>
<dd><a name="index-legend"></a>
<p>Adds a legend to the current plot at the specified position.  Plotting
characters, line styles, colors etc., are identified with the labels in
the character vector <code>legend</code>.  At least one other argument <var>v</var>
(a vector the same length as <code>legend</code>) with the corresponding
values of the plotting unit must also be given, as follows:
</p>
<dl compact="compact">
<dt><code>legend( , fill=<var>v</var>)</code></dt>
<dd><p>Colors for filled boxes
</p></dd>
<dt><code>legend( , col=<var>v</var>)</code></dt>
<dd><p>Colors in which points or lines will be drawn
</p></dd>
<dt><code>legend( , lty=<var>v</var>)</code></dt>
<dd><p>Line styles
</p></dd>
<dt><code>legend( , lwd=<var>v</var>)</code></dt>
<dd><p>Line widths
</p></dd>
<dt><code>legend( , pch=<var>v</var>)</code></dt>
<dd><p>Plotting characters (character vector)
</p></dd>
</dl>

</dd>
<dt><code>title(main, sub)</code></dt>
<dd><a name="index-title"></a>
<p>Adds a title <code>main</code> to the top of the current plot in a large font
and (optionally) a sub-title <code>sub</code> at the bottom in a smaller font.
</p>
</dd>
<dt><code>axis(side, &hellip;)</code></dt>
<dd><a name="index-axis"></a>
<p>Adds an axis to the current plot on the side given by the first argument
(1 to 4, counting clockwise from the bottom.)  Other arguments control
the positioning of the axis within or beside the plot, and tick
positions and labels.  Useful for adding custom axes after calling
<code>plot()</code> with the <code>axes=FALSE</code> argument.
</p></dd>
</dl>

<p>Low-level plotting functions usually require some positioning
information (e.g., <em>x</em> and <em>y</em> coordinates) to determine where
to place the new plot elements.  Coordinates are given in terms of
<em>user coordinates</em> which are defined by the previous high-level
graphics command and are chosen based on the supplied data.
</p>
<p>Where <code>x</code> and <code>y</code> arguments are required, it is also
sufficient to supply a single argument being a list with elements named
<code>x</code> and <code>y</code>.  Similarly a matrix with two columns is also
valid input.  In this way functions such as <code>locator()</code> (see below)
may be used to specify positions on a plot interactively.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Mathematical-annotation" accesskey="1">Mathematical annotation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Hershey-vector-fonts" accesskey="2">Hershey vector fonts</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Mathematical-annotation"></a>
<div class="header">
<p>
Next: <a href="#Hershey-vector-fonts" accesskey="n" rel="next">Hershey vector fonts</a>, Previous: <a href="#Low_002dlevel-plotting-commands" accesskey="p" rel="prev">Low-level plotting commands</a>, Up: <a href="#Low_002dlevel-plotting-commands" accesskey="u" rel="up">Low-level plotting commands</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Mathematical-annotation-1"></a>
<h4 class="subsection">12.2.1 Mathematical annotation</h4>

<p>In some cases, it is useful to add mathematical symbols and formulae to a
plot.  This can be achieved in R by specifying an <em>expression</em> rather
than a character string in any one of <code>text</code>, <code>mtext</code>, <code>axis</code>,
or <code>title</code>.   For example, the following code draws the formula for
the Binomial probability function:
</p>
<div class="example">
<pre class="example">&gt; text(x, y, expression(paste(bgroup(&quot;(&quot;, atop(n, x), &quot;)&quot;), p^x, q^{n-x})))
</pre></div>

<p>More information, including a full listing of the features available can
obtained from within R using the commands:
</p>
<div class="example">
<pre class="example">&gt; help(plotmath)
&gt; example(plotmath)
&gt; demo(plotmath)
</pre></div>

<hr>
<a name="Hershey-vector-fonts"></a>
<div class="header">
<p>
Previous: <a href="#Mathematical-annotation" accesskey="p" rel="prev">Mathematical annotation</a>, Up: <a href="#Low_002dlevel-plotting-commands" accesskey="u" rel="up">Low-level plotting commands</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Hershey-vector-fonts-1"></a>
<h4 class="subsection">12.2.2 Hershey vector fonts</h4>

<p>It is possible to specify Hershey vector fonts for rendering text when using
the <code>text</code> and <code>contour</code> functions.  There are three reasons for
using the Hershey fonts:
</p><ul>
<li> Hershey fonts can produce better
output, especially on a computer screen, for rotated and/or small text.
</li><li> Hershey fonts
provide certain symbols that may not be available
in the standard fonts.  In particular, there are zodiac signs, cartographic
symbols and astronomical symbols.
</li><li> Hershey fonts provide cyrillic and japanese (Kana and Kanji) characters.
</li></ul>

<p>More information, including tables of Hershey characters can be obtained from
within R using the commands:
</p>
<div class="example">
<pre class="example">&gt; help(Hershey)
&gt; demo(Hershey)
&gt; help(Japanese)
&gt; demo(Japanese)
</pre></div>

<hr>
<a name="Interacting-with-graphics"></a>
<div class="header">
<p>
Next: <a href="#Using-graphics-parameters" accesskey="n" rel="next">Using graphics parameters</a>, Previous: <a href="#Low_002dlevel-plotting-commands" accesskey="p" rel="prev">Low-level plotting commands</a>, Up: <a href="#Graphics" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Interacting-with-graphics-1"></a>
<h3 class="section">12.3 Interacting with graphics</h3>

<p>R also provides functions which allow users to extract or add
information to a plot using a mouse.  The simplest of these is the
<code>locator()</code> function:
</p>
<dl compact="compact">
<dt><code>locator(n, type)</code></dt>
<dd><a name="index-locator"></a>
<p>Waits for the user to select locations on the current plot using the
left mouse button.  This continues until <code>n</code> (default 512) points
have been selected, or another mouse button is pressed.  The
<code>type</code> argument allows for plotting at the selected points and has
the same effect as for high-level graphics commands; the default is no
plotting.  <code>locator()</code> returns the locations of the points selected
as a list with two components <code>x</code> and <code>y</code>.
</p></dd>
</dl>

<p><code>locator()</code> is usually called with no arguments.  It is
particularly useful for interactively selecting positions for graphic
elements such as legends or labels when it is difficult to calculate in
advance where the graphic should be placed.  For example, to place some
informative text near an outlying point, the command
</p>
<div class="example">
<pre class="example">&gt; text(locator(1), &quot;Outlier&quot;, adj=0)
</pre></div>

<p>may be useful.  (<code>locator()</code> will be ignored if the current device,
such as <code>postscript</code> does not support interactive pointing.)
</p>
<dl compact="compact">
<dt><code>identify(x, y, labels)</code></dt>
<dd><a name="index-identify"></a>
<p>Allow the user to highlight any of the points defined by <code>x</code> and
<code>y</code> (using the left mouse button) by plotting the corresponding
component of <code>labels</code> nearby (or the index number of the point if
<code>labels</code> is absent).  Returns the indices of the selected points
when another button is pressed.
</p></dd>
</dl>

<p>Sometimes we want to identify particular <em>points</em> on a plot, rather
than their positions.  For example, we may wish the user to select some
observation of interest from a graphical display and then manipulate
that observation in some way.  Given a number of <em>(x, y)</em>
coordinates in two numeric vectors <code>x</code> and <code>y</code>, we could use
the <code>identify()</code> function as follows:
</p>
<div class="example">
<pre class="example">&gt; plot(x, y)
&gt; identify(x, y)
</pre></div>

<p>The <code>identify()</code> functions performs no plotting itself, but simply
allows the user to move the mouse pointer and click the left mouse
button near a point.  If there is a point near the mouse pointer it will
be marked with its index number (that is, its position in the
<code>x</code>/<code>y</code> vectors) plotted nearby.  Alternatively, you could use
some informative string (such as a case name) as a highlight by using
the <code>labels</code> argument to <code>identify()</code>, or disable marking
altogether with the <code>plot = FALSE</code> argument.  When the process is
terminated (see above), <code>identify()</code> returns the indices of the
selected points; you can use these indices to extract the selected
points from the original vectors <code>x</code> and <code>y</code>.
</p>
<hr>
<a name="Using-graphics-parameters"></a>
<div class="header">
<p>
Next: <a href="#Graphics-parameters" accesskey="n" rel="next">Graphics parameters</a>, Previous: <a href="#Interacting-with-graphics" accesskey="p" rel="prev">Interacting with graphics</a>, Up: <a href="#Graphics" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Using-graphics-parameters-1"></a>
<h3 class="section">12.4 Using graphics parameters</h3>

<p>When creating graphics, particularly for presentation or publication
purposes, R&rsquo;s defaults do not always produce exactly that which is
required.  You can, however, customize almost every aspect of the
display using <em>graphics parameters</em>.  R maintains a list of a
large number of graphics parameters which control things such as line
style, colors, figure arrangement and text justification among many
others.  Every graphics parameter has a name (such as &lsquo;<code>col</code>&rsquo;,
which controls colors,) and a value (a color number, for example.)
</p>
<p>A separate list of graphics parameters is maintained for each active
device, and each device has a default set of parameters when
initialized.  Graphics parameters can be set in two ways: either
permanently, affecting all graphics functions which access the current
device; or temporarily, affecting only a single graphics function call.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-par_0028_0029-function" accesskey="1">The par() function</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Arguments-to-graphics-functions" accesskey="2">Arguments to graphics functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="The-par_0028_0029-function"></a>
<div class="header">
<p>
Next: <a href="#Arguments-to-graphics-functions" accesskey="n" rel="next">Arguments to graphics functions</a>, Previous: <a href="#Using-graphics-parameters" accesskey="p" rel="prev">Using graphics parameters</a>, Up: <a href="#Using-graphics-parameters" accesskey="u" rel="up">Using graphics parameters</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Permanent-changes_003a-The-par_0028_0029-function"></a>
<h4 class="subsection">12.4.1 Permanent changes: The <code>par()</code> function</h4>
<a name="index-par"></a>
<a name="index-Graphics-parameters"></a>

<p>The <code>par()</code> function is used to access and modify the list of
graphics parameters for the current graphics device.
</p>
<dl compact="compact">
<dt><code>par()</code></dt>
<dd><p>Without arguments, returns a list of all graphics parameters and their
values for the current device.
</p></dd>
<dt><code>par(c(&quot;col&quot;, &quot;lty&quot;))</code></dt>
<dd><p>With a character vector argument, returns only the named graphics
parameters (again, as a list.)
</p></dd>
<dt><code>par(col=4, lty=2)</code></dt>
<dd><p>With named arguments (or a single list argument), sets the values of
the named graphics parameters, and returns the original values of the
parameters as a list.
</p></dd>
</dl>

<p>Setting graphics parameters with the <code>par()</code> function changes the
value of the parameters <em>permanently</em>, in the sense that all future
calls to graphics functions (on the current device) will be affected by
the new value.  You can think of setting graphics parameters in this way
as setting &ldquo;default&rdquo; values for the parameters, which will be used by
all graphics functions unless an alternative value is given.
</p>
<p>Note that calls to <code>par()</code> <em>always</em> affect the global values
of graphics parameters, even when <code>par()</code> is called from within a
function.  This is often undesirable behavior&mdash;usually we want to set
some graphics parameters, do some plotting, and then restore the
original values so as not to affect the user&rsquo;s R session.  You can
restore the initial values by saving the result of <code>par()</code> when
making changes, and restoring the initial values when plotting is
complete.
</p>
<div class="example">
<pre class="example">&gt; oldpar &lt;- par(col=4, lty=2)
  <span class="roman">&hellip; plotting commands &hellip;</span>
&gt; par(oldpar)
</pre></div>

<p>To save and restore <em>all</em> settable<a name="DOCF25" href="#FOOT25"><sup>25</sup></a> graphical parameters use
</p>
<div class="example">
<pre class="example">&gt; oldpar &lt;- par(no.readonly=TRUE)
  <span class="roman">&hellip; plotting commands &hellip;</span>
&gt; par(oldpar)
</pre></div>


<hr>
<a name="Arguments-to-graphics-functions"></a>
<div class="header">
<p>
Previous: <a href="#The-par_0028_0029-function" accesskey="p" rel="prev">The par() function</a>, Up: <a href="#Using-graphics-parameters" accesskey="u" rel="up">Using graphics parameters</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Temporary-changes_003a-Arguments-to-graphics-functions"></a>
<h4 class="subsection">12.4.2 Temporary changes: Arguments to graphics functions</h4>

<p>Graphics parameters may also be passed to (almost) any graphics function
as named arguments.  This has the same effect as passing the arguments
to the <code>par()</code> function, except that the changes only last for the
duration of the function call.  For example:
</p>
<div class="example">
<pre class="example">&gt; plot(x, y, pch=&quot;+&quot;)
</pre></div>

<p>produces a scatterplot using a plus sign as the plotting character,
without changing the default plotting character for future plots.
</p>
<p>Unfortunately, this is not implemented entirely consistently and it is
sometimes necessary to set and reset graphics parameters using
<code>par()</code>.
</p>

<hr>
<a name="Graphics-parameters"></a>
<div class="header">
<p>
Next: <a href="#Device-drivers" accesskey="n" rel="next">Device drivers</a>, Previous: <a href="#Using-graphics-parameters" accesskey="p" rel="prev">Using graphics parameters</a>, Up: <a href="#Graphics" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Graphics-parameters-list"></a>
<h3 class="section">12.5 Graphics parameters list</h3>

<p>The following sections detail many of the commonly-used graphical
parameters.  The R help documentation for the <code>par()</code> function
provides a more concise summary; this is provided as a somewhat more
detailed alternative.
</p>
<p>Graphics parameters will be presented in the following form:
</p>
<dl compact="compact">
<dt><code><var>name</var>=<var>value</var></code></dt>
<dd><p>A description of the parameter&rsquo;s effect.  <var>name</var> is the name of the
parameter, that is, the argument name to use in calls to <code>par()</code> or
a graphics function.  <var>value</var> is a typical value you might use when
setting the parameter.
</p></dd>
</dl>

<p>Note that <code>axes</code> is <strong>not</strong> a graphics parameter but an
argument to a few <code>plot</code> methods: see <code>xaxt</code> and <code>yaxt</code>.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Graphical-elements" accesskey="1">Graphical elements</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Axes-and-tick-marks" accesskey="2">Axes and tick marks</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Figure-margins" accesskey="3">Figure margins</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Multiple-figure-environment" accesskey="4">Multiple figure environment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Graphical-elements"></a>
<div class="header">
<p>
Next: <a href="#Axes-and-tick-marks" accesskey="n" rel="next">Axes and tick marks</a>, Previous: <a href="#Graphics-parameters" accesskey="p" rel="prev">Graphics parameters</a>, Up: <a href="#Graphics-parameters" accesskey="u" rel="up">Graphics parameters</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Graphical-elements-1"></a>
<h4 class="subsection">12.5.1 Graphical elements</h4>

<p>R plots are made up of points, lines, text and polygons (filled
regions.) Graphical parameters exist which control how these
<em>graphical elements</em> are drawn, as follows:
</p>
<dl compact="compact">
<dt><code>pch=&quot;+&quot;</code></dt>
<dd><p>Character to be used for plotting points.  The default varies with
graphics drivers, but it is usually
a circle.
Plotted points tend to appear slightly above or below the appropriate
position unless you use <code>&quot;.&quot;</code> as the plotting character, which
produces centered points.
</p>
</dd>
<dt><code>pch=4</code></dt>
<dd><p>When <code>pch</code> is given as an integer between 0 and 25 inclusive, a
specialized plotting symbol is produced.  To see what the symbols are,
use the command
</p>
<div class="example">
<pre class="example">&gt; legend(locator(1), as.character(0:25), pch = 0:25)
</pre></div>

<p>Those from 21 to 25 may appear to duplicate earlier symbols, but can be
coloured in different ways: see the help on <code>points</code> and its
examples.
</p>
<p>In addition, <code>pch</code> can be a character or a number in the range
<code>32:255</code> representing a character in the current font.
</p>
</dd>
<dt><code>lty=2</code></dt>
<dd><p>Line types.  Alternative line styles are not supported on all graphics
devices (and vary on those that do) but line type 1 is always a solid
line, line type 0 is always invisible, and line types 2 and onwards are
dotted or dashed lines, or some combination of both.
</p>
</dd>
<dt><code>lwd=2</code></dt>
<dd><p>Line widths.  Desired width of lines, in multiples of the &ldquo;standard&rdquo;
line width.  Affects axis lines as well as lines drawn with
<code>lines()</code>, etc.  Not all devices support this, and some have
restrictions on the widths that can be used.
</p>
</dd>
<dt><code>col=2</code></dt>
<dd><p>Colors to be used for points, lines, text, filled regions and images.
A number from the current palette (see <code>?palette</code>) or a named colour.
</p>
</dd>
<dt><code>col.axis</code></dt>
<dt><code>col.lab</code></dt>
<dt><code>col.main</code></dt>
<dt><code>col.sub</code></dt>
<dd><p>The color to be used for axis annotation, <em>x</em> and <em>y</em> labels,
main and sub-titles, respectively.
</p>
</dd>
<dt><code>font=2</code></dt>
<dd><p>An integer which specifies which font to use for text.  If possible,
device drivers arrange so that <code>1</code> corresponds to plain text,
<code>2</code> to bold face, <code>3</code> to italic, <code>4</code> to bold italic
and <code>5</code> to a symbol font (which include Greek letters).
</p>
</dd>
<dt><code>font.axis</code></dt>
<dt><code>font.lab</code></dt>
<dt><code>font.main</code></dt>
<dt><code>font.sub</code></dt>
<dd><p>The font to be used for axis annotation, <em>x</em> and <em>y</em> labels,
main and sub-titles, respectively.
</p>
</dd>
<dt><code>adj=-0.1</code></dt>
<dd><p>Justification of text relative to the plotting position.  <code>0</code> means
left justify, <code>1</code> means right justify and <code>0.5</code> means to
center horizontally about the plotting position.  The actual value is
the proportion of text that appears to the left of the plotting
position, so a value of <code>-0.1</code> leaves a gap of 10% of the text width
between the text and the plotting position.
</p>
</dd>
<dt><code>cex=1.5</code></dt>
<dd><p>Character expansion.  The value is the desired size of text characters
(including plotting characters) relative to the default text size.
</p>
</dd>
<dt><code>cex.axis</code></dt>
<dt><code>cex.lab</code></dt>
<dt><code>cex.main</code></dt>
<dt><code>cex.sub</code></dt>
<dd><p>The character expansion to be used for axis annotation, <em>x</em> and
<em>y</em> labels, main and sub-titles, respectively.
</p></dd>
</dl>

<hr>
<a name="Axes-and-tick-marks"></a>
<div class="header">
<p>
Next: <a href="#Figure-margins" accesskey="n" rel="next">Figure margins</a>, Previous: <a href="#Graphical-elements" accesskey="p" rel="prev">Graphical elements</a>, Up: <a href="#Graphics-parameters" accesskey="u" rel="up">Graphics parameters</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Axes-and-tick-marks-1"></a>
<h4 class="subsection">12.5.2 Axes and tick marks</h4>

<p>Many of R&rsquo;s high-level plots have axes, and you can construct axes
yourself with the low-level <code>axis()</code> graphics function.  Axes have
three main components: the <em>axis line</em> (line style controlled by the
<code>lty</code> graphics parameter), the <em>tick marks</em> (which mark off unit
divisions along the axis line) and the <em>tick labels</em> (which mark the
units.) These components can be customized with the following graphics
parameters.
</p>
<dl compact="compact">
<dt><code>lab=c(5, 7, 12)</code></dt>
<dd><p>The first two numbers are the desired number of tick intervals on the
<em>x</em> and <em>y</em> axes respectively.  The third number is the
desired length of axis labels, in characters (including the decimal
point.)  Choosing a too-small value for this parameter may result in all
tick labels being rounded to the same number!
</p>
</dd>
<dt><code>las=1</code></dt>
<dd><p>Orientation of axis labels.  <code>0</code> means always parallel to axis,
<code>1</code> means always horizontal, and <code>2</code> means always
perpendicular to the axis.
</p>
</dd>
<dt><code>mgp=c(3, 1, 0)</code></dt>
<dd><p>Positions of axis components.  The first component is the distance from
the axis label to the axis position, in text lines.  The second
component is the distance to the tick labels, and the final component is
the distance from the axis position to the axis line (usually zero).
Positive numbers measure outside the plot region, negative numbers
inside.
</p>
</dd>
<dt><code>tck=0.01</code></dt>
<dd><p>Length of tick marks, as a fraction of the size of the plotting region.
When <code>tck</code> is small (less than 0.5) the tick marks on the <em>x</em>
and <em>y</em> axes are forced to be the same size.  A value of 1 gives
grid lines.  Negative values give tick marks outside the plotting
region.  Use <code>tck=0.01</code> and <code>mgp=c(1,-1.5,0)</code> for internal
tick marks.
</p>
</dd>
<dt><code>xaxs=&quot;r&quot;</code></dt>
<dt><code>yaxs=&quot;i&quot;</code></dt>
<dd><p>Axis styles for the <em>x</em> and <em>y</em> axes, respectively.   With
styles <code>&quot;i&quot;</code> (internal) and <code>&quot;r&quot;</code> (the default) tick marks
always fall within the range of the data, however style <code>&quot;r&quot;</code>
leaves a small amount of space at the edges.  (S has other styles
not implemented in R.)
</p>
</dd>
</dl>

<hr>
<a name="Figure-margins"></a>
<div class="header">
<p>
Next: <a href="#Multiple-figure-environment" accesskey="n" rel="next">Multiple figure environment</a>, Previous: <a href="#Axes-and-tick-marks" accesskey="p" rel="prev">Axes and tick marks</a>, Up: <a href="#Graphics-parameters" accesskey="u" rel="up">Graphics parameters</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Figure-margins-1"></a>
<h4 class="subsection">12.5.3 Figure margins</h4>


<p>A single plot in R is known as a <code>figure</code> and comprises a
<em>plot region</em> surrounded by margins (possibly containing axis
labels, titles, etc.) and (usually) bounded by the axes themselves.
</p>
<p>A typical figure is
</p>
<img src="images/fig11.png" alt="images/fig11">

<p>Graphics parameters controlling figure layout include:
</p>
<dl compact="compact">
<dt><code>mai=c(1, 0.5, 0.5, 0)</code></dt>
<dd><p>Widths of the bottom, left, top and right margins, respectively,
measured in inches.
</p>
</dd>
<dt><code>mar=c(4, 2, 2, 1)</code></dt>
<dd><p>Similar to <code>mai</code>, except the measurement unit is text lines.
</p></dd>
</dl>

<p><code>mar</code> and <code>mai</code> are equivalent in the sense that setting one
changes the value of the other.  The default values chosen for this
parameter are often too large; the right-hand margin is rarely needed,
and neither is the top margin if no title is being used.  The bottom and
left margins must be large enough to accommodate the axis and tick
labels.  Furthermore, the default is chosen without regard to the size
of the device surface: for example, using the <code>postscript()</code> driver
with the <code>height=4</code> argument will result in a plot which is about
50% margin unless <code>mar</code> or <code>mai</code> are set explicitly.  When
multiple figures are in use (see below) the margins are reduced, however
this may not be enough when many figures share the same page.
</p>
<hr>
<a name="Multiple-figure-environment"></a>
<div class="header">
<p>
Previous: <a href="#Figure-margins" accesskey="p" rel="prev">Figure margins</a>, Up: <a href="#Graphics-parameters" accesskey="u" rel="up">Graphics parameters</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Multiple-figure-environment-1"></a>
<h4 class="subsection">12.5.4 Multiple figure environment</h4>

<p>R allows you to create an <em>n</em> by <em>m</em> array of figures on a
single page.  Each figure has its own margins, and the array of figures
is optionally surrounded by an <em>outer margin</em>, as shown in the
following figure.
</p>
<img src="images/fig12.png" alt="images/fig12">

<p>The graphical parameters relating to multiple figures are as follows:
</p>
<dl compact="compact">
<dt><code>mfcol=c(3, 2)</code></dt>
<dt><code>mfrow=c(2, 4)</code></dt>
<dd><p>Set the size of a multiple figure array.  The first value is the number of
rows; the second is the number of columns.  The only difference between
these two parameters is that setting <code>mfcol</code> causes figures to be
filled by column; <code>mfrow</code> fills by rows.
</p>
<p>The layout in the Figure could have been created by setting
<code>mfrow=c(3,2)</code>; the figure shows the page after four plots have
been drawn.
</p>
<p>Setting either of these can reduce the base size of symbols and text
(controlled by <code>par(&quot;cex&quot;)</code> and the pointsize of the device).  In a
layout with exactly two rows and columns the base size is reduced by a
factor of 0.83: if there are three or more of either rows or columns,
the reduction factor is 0.66.
</p>
</dd>
<dt><code>mfg=c(2, 2, 3, 2)</code></dt>
<dd><p>Position of the current figure in a multiple figure environment.  The first
two numbers are the row and column of the current figure; the last two
are the number of rows and columns in the multiple figure array.  Set
this parameter to jump between figures in the array.  You can even use
different values for the last two numbers than the <em>true</em> values
for unequally-sized figures on the same page.
</p>
</dd>
<dt><code>fig=c(4, 9, 1, 4)/10</code></dt>
<dd><p>Position of the current figure on the page.  Values are the positions of
the left, right, bottom and top edges respectively, as a percentage of
the page measured from the bottom left corner.  The example value would
be for a figure in the bottom right of the page.  Set this parameter for
arbitrary positioning of figures within a page.  If you want to add a
figure to a current page, use <code>new=TRUE</code> as well (unlike S).
</p>
</dd>
<dt><code>oma=c(2, 0, 3, 0)</code></dt>
<dt><code>omi=c(0, 0, 0.8, 0)</code></dt>
<dd><p>Size of outer margins.  Like <code>mar</code> and <code>mai</code>, the first
measures in text lines and the second in inches, starting with the
bottom margin and working clockwise.
</p>
</dd>
</dl>

<p>Outer margins are particularly useful for page-wise titles, etc.  Text
can be added to the outer margins with the <code>mtext()</code> function with
argument <code>outer=TRUE</code>.  There are no outer margins by default,
however, so you must create them explicitly using <code>oma</code> or
<code>omi</code>.
</p>
<p>More complicated arrangements of multiple figures can be produced by the
<code>split.screen()</code> and <code>layout()</code> functions, as well as by the
<strong>grid</strong> and <a href="https://CRAN.R-project.org/package=lattice"><strong>lattice</strong></a> packages.
</p>
<hr>
<a name="Device-drivers"></a>
<div class="header">
<p>
Next: <a href="#Dynamic-graphics" accesskey="n" rel="next">Dynamic graphics</a>, Previous: <a href="#Graphics-parameters" accesskey="p" rel="prev">Graphics parameters</a>, Up: <a href="#Graphics" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Device-drivers-1"></a>
<h3 class="section">12.6 Device drivers</h3>
<a name="index-Graphics-device-drivers"></a>

<p>R can generate graphics (of varying levels of quality) on almost any
type of display or printing device.  Before this can begin, however,
R needs to be informed what type of device it is dealing with.  This
is done by starting a <em>device driver</em>.  The purpose of a device
driver is to convert graphical instructions from R (&ldquo;draw a line,&rdquo;
for example) into a form that the particular device can understand.
</p>
<p>Device drivers are started by calling a device driver function.  There
is one such function for every device driver: type <code>help(Devices)</code>
for a list of them all.  For example, issuing the command
</p>
<div class="example">
<pre class="example">&gt; postscript()
</pre></div>

<p>causes all future graphics output to be sent to the printer in
PostScript format.  Some commonly-used device drivers are:
</p>
<dl compact="compact">
<dt><code>X11()</code></dt>
<dd><a name="index-X11"></a>
<p>For use with the X11 window system on Unix-alikes
</p></dd>
<dt><code>windows()</code></dt>
<dd><a name="index-windows"></a>
<p>For use on Windows
</p></dd>
<dt><code>quartz()</code></dt>
<dd><a name="index-quartz"></a>
<p>For use on macOS
</p></dd>
<dt><code>postscript()</code></dt>
<dd><a name="index-postscript"></a>
<p>For printing on PostScript printers, or creating PostScript graphics
files.
</p></dd>
<dt><code>pdf()</code></dt>
<dd><a name="index-pdf"></a>
<p>Produces a PDF file, which can also be included into PDF files.
</p></dd>
<dt><code>png()</code></dt>
<dd><a name="index-png"></a>
<p>Produces a bitmap PNG file. (Not always available: see its help page.)
</p></dd>
<dt><code>jpeg()</code></dt>
<dd><a name="index-jpeg"></a>
<p>Produces a bitmap JPEG file, best used for <code>image</code> plots.
(Not always available: see its help page.)
</p></dd>
</dl>

<p>When you have finished with a device, be sure to terminate the device
driver by issuing the command
</p>
<div class="example">
<pre class="example">&gt; dev.off()
</pre></div>

<p>This ensures that the device finishes cleanly; for example in the case
of hardcopy devices this ensures that every page is completed and has
been sent to the printer.  (This will happen automatically at the normal
end of a session.)
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#PostScript-diagrams-for-typeset-documents" accesskey="1">PostScript diagrams for typeset documents</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Multiple-graphics-devices" accesskey="2">Multiple graphics devices</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="PostScript-diagrams-for-typeset-documents"></a>
<div class="header">
<p>
Next: <a href="#Multiple-graphics-devices" accesskey="n" rel="next">Multiple graphics devices</a>, Previous: <a href="#Device-drivers" accesskey="p" rel="prev">Device drivers</a>, Up: <a href="#Device-drivers" accesskey="u" rel="up">Device drivers</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="PostScript-diagrams-for-typeset-documents-1"></a>
<h4 class="subsection">12.6.1 PostScript diagrams for typeset documents</h4>

<p>By passing the <code>file</code> argument to the <code>postscript()</code> device
driver function, you may store the graphics in PostScript format in a
file of your choice.  The plot will be in landscape orientation unless
the <code>horizontal=FALSE</code> argument is given, and you can control the
size of the graphic with the <code>width</code> and <code>height</code> arguments
(the plot will be scaled as appropriate to fit these dimensions.) For
example, the command
</p>
<div class="example">
<pre class="example">&gt; postscript(&quot;file.ps&quot;, horizontal=FALSE, height=5, pointsize=10)
</pre></div>

<p>will produce a file containing PostScript code for a figure five inches
high, perhaps for inclusion in a document.  It is important to note that
if the file named in the command already exists, it will be overwritten.
This is the case even if the file was only created earlier in the same
R session.
</p>
<p>Many usages of PostScript output will be to incorporate the figure in
another document.  This works best when <em>encapsulated</em> PostScript
is produced: R always produces conformant output, but only marks the
output as such when the <code>onefile=FALSE</code> argument is supplied.  This
unusual notation stems from S-compatibility: it really means that
the output will be a single page (which is part of the EPSF
specification).  Thus to produce a plot for inclusion use something like
</p>
<div class="example">
<pre class="example">&gt; postscript(&quot;plot1.eps&quot;, horizontal=FALSE, onefile=FALSE,
             height=8, width=6, pointsize=10)
</pre></div>


<hr>
<a name="Multiple-graphics-devices"></a>
<div class="header">
<p>
Previous: <a href="#PostScript-diagrams-for-typeset-documents" accesskey="p" rel="prev">PostScript diagrams for typeset documents</a>, Up: <a href="#Device-drivers" accesskey="u" rel="up">Device drivers</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Multiple-graphics-devices-1"></a>
<h4 class="subsection">12.6.2 Multiple graphics devices</h4>

<p>In advanced use of R it is often useful to have several graphics
devices in use at the same time.  Of course only one graphics device can
accept graphics commands at any one time, and this is known as the
<em>current device</em>.  When multiple devices are open, they form a
numbered sequence with names giving the kind of device at any position.
</p>
<p>The main commands used for operating with multiple devices, and their
meanings are as follows:
</p>
<dl compact="compact">
<dt><code>X11()</code></dt>
<dd><p>[UNIX]
</p></dd>
<dt><code>windows()</code></dt>
<dt><code>win.printer()</code></dt>
<dt><code>win.metafile()</code></dt>
<dd><p>[Windows]
</p></dd>
<dt><code>quartz()</code></dt>
<dd><p>[macOS]
</p></dd>
<dt><code>postscript()</code></dt>
<dt><code>pdf()</code></dt>
<dt><code>png()</code></dt>
<dt><code>jpeg()</code></dt>
<dt><code>tiff()</code></dt>
<dt><code>bitmap()</code></dt>
<dt><code>&hellip;</code></dt>
<dd><p>Each new call to a device driver function opens a new graphics device,
thus extending by one the device list.  This device becomes the current
device, to which graphics output will be sent.
</p>
</dd>
<dt><code>dev.list()</code></dt>
<dd><a name="index-dev_002elist"></a>
<p>Returns the number and name of all active devices.  The device at
position 1 on the list is always the <em>null device</em> which does not
accept graphics commands at all.
</p>
</dd>
<dt><code>dev.next()</code></dt>
<dt><code>dev.prev()</code></dt>
<dd><a name="index-dev_002enext"></a>
<a name="index-dev_002eprev"></a>
<p>Returns the number and name of the graphics device next to, or previous
to the current device, respectively.
</p>
</dd>
<dt><code>dev.set(which=<var>k</var>)</code></dt>
<dd><a name="index-dev_002eset"></a>
<p>Can be used to change the current graphics device to the one at position
<var>k</var> of the device list.  Returns the number and label of the device.
</p>
</dd>
<dt><code>dev.off(<var>k</var>)</code></dt>
<dd><a name="index-dev_002eoff"></a>
<p>Terminate the graphics device at point <var>k</var> of the device list.  For
some devices, such as <code>postscript</code> devices, this will either print
the file immediately or correctly complete the file for later printing,
depending on how the device was initiated.
</p>
</dd>
<dt><code>dev.copy(device, &hellip;, which=<var>k</var>)</code></dt>
<dt><code>dev.print(device, &hellip;, which=<var>k</var>)</code></dt>
<dd><p>Make a copy of the device <var>k</var>.  Here <code>device</code> is a device
function, such as <code>postscript</code>, with extra arguments, if needed,
specified by &lsquo;<samp>&hellip;</samp>&rsquo;.  <code>dev.print</code> is similar, but the
copied device is immediately closed, so that end actions, such as
printing hardcopies, are immediately performed.
</p>
</dd>
<dt><code>graphics.off()</code></dt>
<dd><p>Terminate all graphics devices on the list, except the null device.
</p></dd>
</dl>

<hr>
<a name="Dynamic-graphics"></a>
<div class="header">
<p>
Previous: <a href="#Device-drivers" accesskey="p" rel="prev">Device drivers</a>, Up: <a href="#Graphics" accesskey="u" rel="up">Graphics</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Dynamic-graphics-1"></a>
<h3 class="section">12.7 Dynamic graphics</h3>
<a name="index-Dynamic-graphics"></a>

<p>R does not have builtin capabilities for dynamic or
interactive graphics, e.g.&nbsp;rotating point clouds or to &ldquo;brushing&rdquo;
(interactively highlighting) points. However, extensive dynamic graphics
facilities are available in the system GGobi by Swayne, Cook and Buja
available from
</p>
<blockquote>
<p><a href="http://www.ggobi.org/">http://www.ggobi.org/</a>
</p></blockquote>

<p>and these can be accessed from R via the package <a href="https://CRAN.R-project.org/package=rggobi"><strong>rggobi</strong></a>, described at
<a href="http://www.ggobi.org/rggobi">http://www.ggobi.org/rggobi</a>.
</p>
<p>Also, package <a href="https://CRAN.R-project.org/package=rgl"><strong>rgl</strong></a> provides ways to interact with 3D plots, for example
of surfaces.
</p>
<hr>
<a name="Packages"></a>
<div class="header">
<p>
Next: <a href="#OS-facilities" accesskey="n" rel="next">OS facilities</a>, Previous: <a href="#Graphics" accesskey="p" rel="prev">Graphics</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Packages-1"></a>
<h2 class="chapter">13 Packages</h2>
<a name="index-Packages-1"></a>

<p>All R functions and datasets are stored in <em>packages</em>.  Only
when a package is loaded are its contents available.  This is done both
for efficiency (the full list would take more memory and would take
longer to search than a subset), and to aid package developers, who are
protected from name clashes with other code.  The process of developing
packages is described in <a href="http://cran.r-project.org/doc/manuals/R-exts.html#Creating-R-packages">Creating R
packages</a> in <cite>Writing R Extensions</cite>.  Here, we will describe them
from a user&rsquo;s point of view.
</p>
<p>To see which packages are installed at your site, issue the command
</p>
<div class="example">
<pre class="example">&gt; library()
</pre></div>

<p>with no arguments.  To load a particular package (e.g., the <a href="https://CRAN.R-project.org/package=boot"><strong>boot</strong></a>
package containing functions from Davison &amp; Hinkley (1997)), use a
command like
</p>
<div class="example">
<pre class="example">&gt; library(boot)
</pre></div>

<p>Users connected to the Internet can use the <code>install.packages()</code>
and <code>update.packages()</code> functions (available through the
<code>Packages</code> menu in the Windows and macOS GUIs, see <a href="http://cran.r-project.org/doc/manuals/R-admin.html#Installing-packages">Installing
packages</a> in <cite>R Installation and Administration</cite>) to install
and update packages.
</p>
<p>To see which packages are currently loaded, use
</p>
<div class="example">
<pre class="example">&gt; search()
</pre></div>

<p>to display the search list.  Some packages may be loaded but not
available on the search list (see <a href="#Namespaces">Namespaces</a>): these will be
included in the list given by
</p>
<div class="example">
<pre class="example">&gt; loadedNamespaces()
</pre></div>


<p>To see a list of all available help topics in an installed package,
use
</p>
<div class="example">
<pre class="example">&gt; help.start()
</pre></div>

<p>to start the <acronym>HTML</acronym> help system, and then navigate to the package
listing in the <code>Reference</code> section.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Standard-packages" accesskey="1">Standard packages</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Contributed-packages-and-CRAN" accesskey="2">Contributed packages and CRAN</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Namespaces" accesskey="3">Namespaces</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Standard-packages"></a>
<div class="header">
<p>
Next: <a href="#Contributed-packages-and-CRAN" accesskey="n" rel="next">Contributed packages and CRAN</a>, Previous: <a href="#Packages" accesskey="p" rel="prev">Packages</a>, Up: <a href="#Packages" accesskey="u" rel="up">Packages</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Standard-packages-1"></a>
<h3 class="section">13.1 Standard packages</h3>

<p>The standard (or <em>base</em>) packages are considered part of the R
source code.  They contain the basic functions that allow R to work,
and the datasets and standard statistical and graphical functions that
are described in this manual.  They should be automatically available in
any R installation.  See <a href="R-FAQ.html#Which-add_002don-packages-exist-for-R_003f">R
packages</a> in <cite>R FAQ</cite>, for a complete list.
</p>
<hr>
<a name="Contributed-packages-and-CRAN"></a>
<div class="header">
<p>
Next: <a href="#Namespaces" accesskey="n" rel="next">Namespaces</a>, Previous: <a href="#Standard-packages" accesskey="p" rel="prev">Standard packages</a>, Up: <a href="#Packages" accesskey="u" rel="up">Packages</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Contributed-packages-and-CRAN-1"></a>
<h3 class="section">13.2 Contributed packages and <acronym>CRAN</acronym></h3>
<a name="index-CRAN"></a>

<p>There are thousands of contributed packages for R, written by many
different authors.  Some of these packages implement specialized
statistical methods, others give access to data or hardware, and others
are designed to complement textbooks.  Some (the <em>recommended</em>
packages) are distributed with every binary distribution of R.  Most
are available for download from <acronym>CRAN</acronym>
(<a href="https://CRAN.R-project.org/">https://CRAN.R-project.org/</a> and its mirrors) and other
repositories such as Bioconductor (<a href="https://www.bioconductor.org/">https://www.bioconductor.org/</a>).
and Omegahat (<a href="http://www.omegahat.net/">http://www.omegahat.net/</a>).  The <em>R FAQ</em>
contains a list of CRAN packages current at the time of release, but the
collection of available packages changes very frequently.
</p>
<hr>
<a name="Namespaces"></a>
<div class="header">
<p>
Previous: <a href="#Contributed-packages-and-CRAN" accesskey="p" rel="prev">Contributed packages and CRAN</a>, Up: <a href="#Packages" accesskey="u" rel="up">Packages</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Namespaces-1"></a>
<h3 class="section">13.3 Namespaces</h3>
<a name="index-Namespace"></a>
<a name="index-_003a_003a"></a>
<a name="index-_003a_003a_003a"></a>

<p>Packages have <em>namespaces</em>, which do three things: they allow the
package writer to hide functions and data that are meant only for
internal use, they prevent functions from breaking when a user (or other
package writer) picks a name that clashes with one in the package, and
they provide a way to refer to an object within a particular package.
</p>
<p>For example, <code>t()</code> is the transpose function in R, but users
might define their own function named <code>t</code>.  Namespaces prevent
the user&rsquo;s definition from taking precedence, and breaking every
function that tries to transpose a matrix.
</p>
<p>There are two operators that work with namespaces.  The double-colon
operator <code>::</code> selects definitions from a particular namespace.
In the example above, the transpose function will always be available
as <code>base::t</code>, because it is defined in the <code>base</code> package.
Only functions that are exported from the package can be retrieved in
this way.
</p>
<p>The triple-colon operator <code>:::</code> may be seen in a few places in R
code: it acts like the double-colon operator but also allows access to
hidden objects.  Users are more likely to use the <code>getAnywhere()</code>
function, which searches multiple packages.
</p>
<p>Packages are often inter-dependent, and loading one may cause others to
be automatically loaded.  The colon operators described above will also
cause automatic loading of the associated package.  When packages with
namespaces are loaded automatically they are not added to the search
list.
</p>
<hr>
<a name="OS-facilities"></a>
<div class="header">
<p>
Next: <a href="#A-sample-session" accesskey="n" rel="next">A sample session</a>, Previous: <a href="#Packages" accesskey="p" rel="prev">Packages</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="OS-facilities-1"></a>
<h2 class="chapter">14 OS facilities</h2>

<p>R has quite extensive facilities to access the OS under which it is
running: this allows it to be used as a scripting language and that
ability is much used by R itself, for example to install packages.
</p>
<p>Because R&rsquo;s own scripts need to work across all platforms,
considerable effort has gone into make the scripting facilities as
platform-independent as is feasible.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Files-and-directories" accesskey="1">Files and directories</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Filepaths" accesskey="2">Filepaths</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#System-commands" accesskey="3">System commands</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Compression-and-Archives" accesskey="4">Compression and Archives</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Files-and-directories"></a>
<div class="header">
<p>
Next: <a href="#Filepaths" accesskey="n" rel="next">Filepaths</a>, Previous: <a href="#OS-facilities" accesskey="p" rel="prev">OS facilities</a>, Up: <a href="#OS-facilities" accesskey="u" rel="up">OS facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Files-and-directories-1"></a>
<h3 class="section">14.1 Files and directories</h3>

<p>There are many functions to manipulate files and directories. Here are
pointers to some of the more commonly used ones.
</p>
<p>To create an (empty) file or directory, use <code>file.create</code> or
<code>dir.create</code>.  (These are the analogues of the POSIX utilities
<code>touch</code> and <code>mkdir</code>.)  For temporary files and
directories in the R session directory see <code>tempfile</code>.
</p>
<p>Files can be removed by either <code>file.remove</code> or <code>unlink</code>: the
latter can remove directory trees.
</p>
<p>For directory listings use <code>list.files</code> (also available as
<code>dir</code>) or <code>list.dirs</code>. These can select files using a regular
expression: to select by wildcards use <code>Sys.glob</code>.
</p>
<p>Many types of information on a filepath (including for example if it is
a file or directory) can be found by <code>file.info</code>.
</p>
<p>There are several ways to find out if a file &lsquo;exists&rsquo; (a file can
exist on the filesystem and not be visible to the current user).
There are functions <code>file.exists</code>, <code>file.access</code> and
<code>file_test</code> with various versions of this test: <code>file_test</code> is
a version of the POSIX <code>test</code> command for those familiar with
shell scripting.
</p>
<p>Function <code>file.copy</code> is the R analogue of the POSIX command
<code>cp</code>.
</p>
<p>Choosing files can be done interactively by <code>file.choose</code>: the
Windows port has the more versatile functions <code>choose.files</code> and
<code>choose.dir</code> and there are similar functions in the <strong>tcltk</strong>
package: <code>tk_choose.files</code> and <code>tk_choose.dir</code>.
</p>
<p>Functions <code>file.show</code> and <code>file.edit</code> will display and edit
one or more files in a way appropriate to the R port, using the
facilities of a console (such as RGui on Windows or R.app on macOS) if
one is in use.
</p>
<p>There is some support for <em>links</em> in the filesystem: see functions
<code>file.link</code> and <code>Sys.readlink</code>.
</p>

<hr>
<a name="Filepaths"></a>
<div class="header">
<p>
Next: <a href="#System-commands" accesskey="n" rel="next">System commands</a>, Previous: <a href="#Files-and-directories" accesskey="p" rel="prev">Files and directories</a>, Up: <a href="#OS-facilities" accesskey="u" rel="up">OS facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Filepaths-1"></a>
<h3 class="section">14.2 Filepaths</h3>

<p>With a few exceptions, R relies on the underlying OS functions to
manipulate filepaths.  Some aspects of this are allowed to depend on the
OS, and do, even down to the version of the OS.  There are POSIX
standards for how OSes should interpret filepaths and many R users
assume POSIX compliance: but Windows does not claim to be compliant and
other OSes may be less than completely compliant.
</p>
<p>The following are some issues which have been encountered with filepaths.
</p>
<ul>
<li> POSIX filesystems are case-sensitive, so <samp>foo.png</samp> and
<samp>Foo.PNG</samp> are different files.  However, the defaults on Windows
and macOS are to be case-insensitive, and FAT filesystems (commonly used
on removable storage) are not normally case-sensitive (and all filepaths
may be mapped to lower case).

</li><li> Almost all the Windows&rsquo; OS services support the use of slash or
backslash as the filepath separator, and R converts the known
exceptions to the form required by Windows.

</li><li> The behaviour of filepaths with a trailing slash is OS-dependent.  Such
paths are not valid on Windows and should not be expected to work.
POSIX-2008 requires such paths to match only directories, but earlier
versions allowed them to also match files.  So they are best avoided.

</li><li> Multiple slashes in filepaths such as <samp>/abc//def</samp> are valid on
POSIX filesystems and treated as if there was only one slash.  They are
<em>usually</em> accepted by Windows&rsquo; OS functions.  However, leading
double slashes may have a different meaning.

</li><li> Windows&rsquo; UNC filepaths (such as <samp>\\server\dir1\dir2\file</samp> and
<samp>\\?\UNC\server\dir1\dir2\file</samp>) are not supported, but they may
work in some R functions.  POSIX filesystems are allowed to treat a
leading double slash specially.

</li><li> Windows allows filepaths containing drives and relative to the current
directory on a drive, e.g. <samp>d:foo/bar</samp> refers to
<samp>d:/a/b/c/foo/bar</samp> if the current directory <em>on drive
<samp>d:</samp></em> is <samp>/a/b/c</samp>.  It is intended that these work, but the
use of absolute paths is safer.
</li></ul>

<p>Functions <code>basename</code> and <code>dirname</code> select parts of a file
path: the recommended way to assemble a file path from components is
<code>file.path</code>.  Function <code>pathexpand</code> does &lsquo;tilde expansion&rsquo;,
substituting values for home directories (the current user&rsquo;s, and
perhaps those of other users).
</p>
<p>On filesystems with links, a single file can be referred to by many
filepaths.  Function <code>normalizePath</code> will find a canonical
filepath.
</p>
<p>Windows has the concepts of short (&lsquo;8.3&rsquo;) and long file names:
<code>normalizePath</code> will return an absolute path using long file names
and <code>shortPathName</code> will return a version using short names.  The
latter does not contain spaces and uses backslash as the separator, so
is sometimes useful for exporting names from R.
</p>
<p>File <em>permissions</em> are a related topic.  R has support for the
POSIX concepts of read/write/execute permission for owner/group/all but
this may be only partially supported on the filesystem, so for example
on Windows only read-only files (for the account running the R
session) are recognized.  Access Control Lists (ACLs) are employed on
several filesystems, but do not have an agreed standard and R has no
facilities to control them.  Use <code>Sys.chmod</code> to change permissions.
</p>
<hr>
<a name="System-commands"></a>
<div class="header">
<p>
Next: <a href="#Compression-and-Archives" accesskey="n" rel="next">Compression and Archives</a>, Previous: <a href="#Filepaths" accesskey="p" rel="prev">Filepaths</a>, Up: <a href="#OS-facilities" accesskey="u" rel="up">OS facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="System-commands-1"></a>
<h3 class="section">14.3 System commands</h3>

<p>Functions <code>system</code> and <code>system2</code> are used to invoke a system
command and optionally collect its output.  <code>system2</code> is a little
more general but its main advantage is that it is easier to write
cross-platform code using it.
</p>
<p><code>system</code> behaves differently on Windows from other OSes (because
the API C call of that name does).  Elsewhere it invokes a shell to run
the command: the Windows port of R has a function <code>shell</code> to do
that.
</p>
<p>To find out if the OS includes a command, use <code>Sys.which</code>, which
attempts to do this in a cross-platform way (unfortunately it is not a
standard OS service).
</p>
<p>Function <code>shQuote</code> will quote filepaths as needed for commands in
the current OS.
</p>
<hr>
<a name="Compression-and-Archives"></a>
<div class="header">
<p>
Previous: <a href="#System-commands" accesskey="p" rel="prev">System commands</a>, Up: <a href="#OS-facilities" accesskey="u" rel="up">OS facilities</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Compression-and-Archives-1"></a>
<h3 class="section">14.4 Compression and Archives</h3>

<p>Recent versions of R have extensive facilities to read and write
compressed files, often transparently.  Reading of files in R is to a
vey large extent done by <em>connections</em>, and the <code>file</code>
function which is used to open a connection to a file (or a URL) and is
able to identify the compression used from the &lsquo;magic&rsquo; header of the
file.
</p>
<p>The type of compression which has been supported for longest is
<code>gzip</code> compression, and that remains a good general compromise.
Files compressed by the earlier Unix <code>compress</code> utility can also
be read, but these are becoming rare.  Two other forms of compression,
those of the <code>bzip2</code> and <code>xz</code> utilities are also
available.  These generally achieve higher rates of compression
(depending on the file, much higher) at the expense of slower
decompression and much slower compression.
</p>
<p>There is some confusion between <code>xz</code> and <code>lzma</code>
compression (see <a href="https://en.wikipedia.org/wiki/Xz">https://en.wikipedia.org/wiki/Xz</a> and
<a href="https://en.wikipedia.org/wiki/LZMA">https://en.wikipedia.org/wiki/LZMA</a>): R can read files
compressed by most versions of either.
</p>
<p>File archives are single files which contain a collection of files, the
most common ones being &lsquo;tarballs&rsquo; and zip files as used to distribute
R packages.  R can list and unpack both (see functions <code>untar</code>
and <code>unzip</code>) and create both (for <code>zip</code> with the help of an
external program).
</p>
<hr>
<a name="A-sample-session"></a>
<div class="header">
<p>
Next: <a href="#Invoking-R" accesskey="n" rel="next">Invoking R</a>, Previous: <a href="#OS-facilities" accesskey="p" rel="prev">OS facilities</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="A-sample-session-1"></a>
<h2 class="appendix">Appendix A A sample session</h2>

<p>The following session is intended to introduce to you some features of
the R environment by using them.  Many features of the system will be
unfamiliar and puzzling at first, but this puzzlement will soon
disappear.
</p>
<dl compact="compact">
<dt><code>Start R appropriately for your platform (see <a href="#Invoking-R">Invoking R</a>).</code></dt>
<dd>
<p>The R program begins, with a banner.
</p>
<p>(Within R code, the prompt on the left hand side will not be shown to
avoid confusion.)
</p>
</dd>
<dt><code>help.start()</code></dt>
<dd><p>Start the <acronym>HTML</acronym> interface to on-line help (using a web browser
available at your machine).  You should briefly explore the features of
this facility with the mouse.
</p>
<p>Iconify the help window and move on to the next part.
</p>
</dd>
<dt><code>x &lt;- rnorm(50)</code></dt>
<dt><code>y &lt;- rnorm(x)</code></dt>
<dd><p>Generate two pseudo-random normal vectors of <em>x</em>- and
<em>y</em>-coordinates.
</p>
</dd>
<dt><code>plot(x, y)</code></dt>
<dd><p>Plot the points in the plane.  A graphics window will appear automatically.
</p>
</dd>
<dt><code>ls()</code></dt>
<dd><p>See which R objects are now in the R workspace.
</p>
</dd>
<dt><code>rm(x, y)</code></dt>
<dd><p>Remove objects no longer needed. (Clean up).
</p>
</dd>
<dt><code>x &lt;- 1:20</code></dt>
<dd><p>Make <em>x = (1, 2, &hellip;, 20)</em>.
</p>
</dd>
<dt><code>w &lt;- 1 + sqrt(x)/2</code></dt>
<dd><p>A &lsquo;weight&rsquo; vector of standard deviations.
</p>
</dd>
<dt><code>dummy &lt;- data.frame(x=x, y= x + rnorm(x)*w)</code></dt>
<dt><code>dummy</code></dt>
<dd><p>Make a <em>data frame</em> of two columns, <em>x</em> and <em>y</em>, and look
at it.
</p>
</dd>
<dt><code>fm &lt;- lm(y ~ x, data=dummy)</code></dt>
<dt><code>summary(fm)</code></dt>
<dd><p>Fit a simple linear regression and look at the
analysis.  With <code>y</code> to the left of the tilde,
we are modelling <em>y</em> dependent on <em>x</em>.
</p>
</dd>
<dt><code>fm1 &lt;- lm(y ~ x, data=dummy, weight=1/w^2)</code></dt>
<dt><code>summary(fm1)</code></dt>
<dd><p>Since we know the standard deviations, we can do a weighted regression.
</p>
</dd>
<dt><code>attach(dummy)</code></dt>
<dd><p>Make the columns in the data frame visible as variables.
</p>
</dd>
<dt><code>lrf &lt;- lowess(x, y)</code></dt>
<dd><p>Make a nonparametric local regression function.
</p>
</dd>
<dt><code>plot(x, y)</code></dt>
<dd><p>Standard point plot.
</p>
</dd>
<dt><code>lines(x, lrf$y)</code></dt>
<dd><p>Add in the local regression.
</p>
</dd>
<dt><code>abline(0, 1, lty=3)</code></dt>
<dd><p>The true regression line: (intercept 0, slope 1).
</p>
</dd>
<dt><code>abline(coef(fm))</code></dt>
<dd><p>Unweighted regression line.
</p>
</dd>
<dt><code>abline(coef(fm1), col = &quot;red&quot;)</code></dt>
<dd><p>Weighted regression line.
</p>
</dd>
<dt><code>detach()</code></dt>
<dd><p>Remove data frame from the search path.
</p>
</dd>
<dt><code>plot(fitted(fm), resid(fm),</code></dt>
<dt><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xlab=&quot;Fitted&nbsp;values&quot;<!-- /@w -->,</code></dt>
<dt><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ylab=&quot;Residuals&quot;<!-- /@w -->,</code></dt>
<dt><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;main=&quot;Residuals&nbsp;vs&nbsp;Fitted&quot;)<!-- /@w --></code></dt>
<dd><p>A standard regression diagnostic plot to check for heteroscedasticity.
Can you see it?
</p>
</dd>
<dt><code>qqnorm(resid(fm), main=&quot;Residuals Rankit Plot&quot;)</code></dt>
<dd><p>A normal scores plot to check for skewness, kurtosis and outliers.  (Not
very useful here.)
</p>
</dd>
<dt><code>rm(fm, fm1, lrf, x, dummy)</code></dt>
<dd><p>Clean up again.
</p></dd>
</dl>

<p>The next section will look at data from the classical experiment of
Michelson to measure the speed of light.  This dataset is available in
the <code>morley</code> object, but we will read it to illustrate the
<code>read.table</code> function.
</p>
<dl compact="compact">
<dt><code>filepath &lt;- system.file(&quot;data&quot;, &quot;morley.tab&quot; , package=&quot;datasets&quot;)</code></dt>
<dt><code>filepath</code></dt>
<dd><p>Get the path to the data file.
</p>
</dd>
<dt><code>file.show(filepath)</code></dt>
<dd><p>Optional.  Look at the file.
</p>
</dd>
<dt><code>mm &lt;- read.table(filepath)</code></dt>
<dt><code>mm</code></dt>
<dd><p>Read in the Michelson data as a data frame, and look at it.
There are five experiments (column <code>Expt</code>) and each has 20 runs
(column <code>Run</code>) and <code>sl</code> is the recorded speed of light,
suitably coded.
</p>
</dd>
<dt><code>mm$Expt &lt;- factor(mm$Expt)</code></dt>
<dt><code>mm$Run &lt;- factor(mm$Run)</code></dt>
<dd><p>Change <code>Expt</code> and <code>Run</code> into factors.
</p>
</dd>
<dt><code>attach(mm)</code></dt>
<dd><p>Make the data frame visible at position 3 (the default).
</p>
</dd>
<dt><code>plot(Expt, Speed, main=&quot;Speed of Light Data&quot;, xlab=&quot;Experiment No.&quot;)</code></dt>
<dd><p>Compare the five experiments with simple boxplots.
</p>
</dd>
<dt><code>fm &lt;- aov(Speed ~ Run + Expt, data=mm)</code></dt>
<dt><code>summary(fm)</code></dt>
<dd><p>Analyze as a randomized block, with &lsquo;runs&rsquo; and &lsquo;experiments&rsquo; as factors.
</p>
</dd>
<dt><code>fm0 &lt;- update(fm, . ~ . - Run)</code></dt>
<dt><code>anova(fm0, fm)</code></dt>
<dd><p>Fit the sub-model omitting &lsquo;runs&rsquo;, and compare using a formal analysis
of variance.
</p>
</dd>
<dt><code>detach()</code></dt>
<dt><code>rm(fm, fm0)</code></dt>
<dd><p>Clean up before moving on.
</p>
</dd>
</dl>

<p>We now look at some more graphical features: contour and image plots.
</p>
<dl compact="compact">
<dt><code>x &lt;- seq(-pi, pi, len=50)</code></dt>
<dt><code>y &lt;- x</code></dt>
<dd><p><em>x</em> is a vector of 50 equally spaced values in
the interval [-pi\, pi].
<em>y</em> is the same.
</p>
</dd>
<dt><code>f &lt;- outer(x, y, function(x, y) cos(y)/(1 + x^2))</code></dt>
<dd><p><em>f</em> is a square matrix, with rows and columns indexed by <em>x</em>
and <em>y</em> respectively, of values of the function
cos(y)/(1 + x^2).
</p>
</dd>
<dt><code>oldpar &lt;- par(no.readonly = TRUE)</code></dt>
<dt><code>par(pty=&quot;s&quot;)</code></dt>
<dd><p>Save the plotting parameters and set the plotting region to &ldquo;square&rdquo;.
</p>
</dd>
<dt><code>contour(x, y, f)</code></dt>
<dt><code>contour(x, y, f, nlevels=15, add=TRUE)</code></dt>
<dd><p>Make a contour map of <em>f</em>; add in more lines for more detail.
</p>
</dd>
<dt><code>fa &lt;- (f-t(f))/2</code></dt>
<dd><p><code>fa</code> is the &ldquo;asymmetric part&rdquo; of <em>f</em>.  (<code>t()</code> is
transpose).
</p>
</dd>
<dt><code>contour(x, y, fa, nlevels=15)</code></dt>
<dd><p>Make a contour plot, &hellip;
</p>
</dd>
<dt><code>par(oldpar)</code></dt>
<dd><p>&hellip; and restore the old graphics parameters.
</p>
</dd>
<dt><code>image(x, y, f)</code></dt>
<dt><code>image(x, y, fa)</code></dt>
<dd><p>Make some high density image plots, (of which you can get
hardcopies if you wish), &hellip;
</p>
</dd>
<dt><code>objects(); rm(x, y, f, fa)</code></dt>
<dd><p>&hellip; and clean up before moving on.
</p></dd>
</dl>

<p>R can do complex arithmetic, also.
</p>
<dl compact="compact">
<dt><code>th &lt;- seq(-pi, pi, len=100)</code></dt>
<dt><code>z &lt;- exp(1i*th)</code></dt>
<dd><p><code>1i</code> is used for the complex number <em>i</em>.
</p>
</dd>
<dt><code>par(pty=&quot;s&quot;)</code></dt>
<dt><code>plot(z, type=&quot;l&quot;)</code></dt>
<dd><p>Plotting complex arguments means plot imaginary versus real parts.  This
should be a circle.
</p>
</dd>
<dt><code>w &lt;- rnorm(100) + rnorm(100)*1i</code></dt>
<dd><p>Suppose we want to sample points within the unit circle.  One method
would be to take complex numbers with standard normal real and imaginary
parts &hellip;
</p>
</dd>
<dt><code>w &lt;- ifelse(Mod(w) &gt; 1, 1/w, w)</code></dt>
<dd><p>&hellip; and to map any outside the circle onto their reciprocal.
</p>
</dd>
<dt><code>plot(w, xlim=c(-1,1), ylim=c(-1,1), pch=&quot;+&quot;,xlab=&quot;x&quot;, ylab=&quot;y&quot;)</code></dt>
<dt><code>lines(z)</code></dt>
<dd><p>All points are inside the unit circle, but the distribution is not
uniform.
</p>
</dd>
<dt><code>w &lt;- sqrt(runif(100))*exp(2*pi*runif(100)*1i)</code></dt>
<dt><code>plot(w, xlim=c(-1,1), ylim=c(-1,1), pch=&quot;+&quot;, xlab=&quot;x&quot;, ylab=&quot;y&quot;)</code></dt>
<dt><code>lines(z)</code></dt>
<dd><p>The second method uses the uniform distribution.  The points should now
look more evenly spaced over the disc.
</p>
</dd>
<dt><code>rm(th, w, z)</code></dt>
<dd><p>Clean up again.
</p>
</dd>
<dt><code>q()</code></dt>
<dd><p>Quit the R program.  You will be asked if you want to save the R
workspace, and for an exploratory session like this, you probably do not
want to save it.
</p></dd>
</dl>

<hr>
<a name="Invoking-R"></a>
<div class="header">
<p>
Next: <a href="#The-command_002dline-editor" accesskey="n" rel="next">The command-line editor</a>, Previous: <a href="#A-sample-session" accesskey="p" rel="prev">A sample session</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Invoking-R-1"></a>
<h2 class="appendix">Appendix B Invoking R</h2>

<p>Users of R on Windows or macOS should read the OS-specific section
first, but command-line use is also supported.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Invoking-R-from-the-command-line" accesskey="1">Invoking R from the command line</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Invoking-R-under-Windows" accesskey="2">Invoking R under Windows</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Invoking-R-under-macOS" accesskey="3">Invoking R under macOS</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Scripting-with-R" accesskey="4">Scripting with R</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>

<hr>
<a name="Invoking-R-from-the-command-line"></a>
<div class="header">
<p>
Next: <a href="#Invoking-R-under-Windows" accesskey="n" rel="next">Invoking R under Windows</a>, Previous: <a href="#Invoking-R" accesskey="p" rel="prev">Invoking R</a>, Up: <a href="#Invoking-R" accesskey="u" rel="up">Invoking R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Invoking-R-from-the-command-line-1"></a>
<h3 class="appendixsec">B.1 Invoking R from the command line</h3>

<p>When working at a command line on UNIX or Windows, the command &lsquo;<samp>R</samp>&rsquo;
can be used both for starting the main R program in the form
</p>
<div class="display">
<pre class="display"><code>R</code> [<var>options</var>] [<code>&lt;</code><var>infile</var>] [<code>&gt;</code><var>outfile</var>],
</pre></div>

<p>or, via the <code>R CMD</code> interface, as a wrapper to various R tools
(e.g., for processing files in R documentation format or manipulating
add-on packages) which are not intended to be called &ldquo;directly&rdquo;.
</p>
<p>At the Windows command-line, <code>Rterm.exe</code> is preferred to
<code>R</code>.
</p>
<p>You need to ensure that either the environment variable <code>TMPDIR</code> is
unset or it points to a valid place to create temporary files and
directories.
</p>
<p>Most options control what happens at the beginning and at the end of an
R session.  The startup mechanism is as follows (see also the on-line
help for topic &lsquo;<samp>Startup</samp>&rsquo; for more information, and the section below
for some Windows-specific details).
</p>
<ul>
<li> Unless <samp>--no-environ</samp> was given, R searches for user and site
files to process for setting environment variables.  The name of the
site file is the one pointed to by the environment variable
<code>R_ENVIRON</code>; if this is unset, <samp><var>R_HOME</var>/etc/Renviron.site</samp>
is used (if it exists).  The user file is the one pointed to by the
environment variable <code>R_ENVIRON_USER</code> if this is set; otherwise,
files <samp>.Renviron</samp> in the current or in the user&rsquo;s home directory
(in that order) are searched for.  These files should contain lines of
the form &lsquo;<samp><var>name</var>=<var>value</var></samp>&rsquo;.  (See <code>help(&quot;Startup&quot;)</code> for
a precise description.)  Variables you might want to set include
<code>R_PAPERSIZE</code> (the default paper size), <code>R_PRINTCMD</code> (the
default print command) and <code>R_LIBS</code> (specifies the list of R
library trees searched for add-on packages).

</li><li> Then R searches for the site-wide startup profile unless the command
line option <samp>--no-site-file</samp> was given.  The name of this file is
taken from the value of the <code>R_PROFILE</code> environment variable.  If
that variable is unset, the default
<samp><var>R_HOME</var>/etc/Rprofile.site</samp> is used if this exists.

</li><li> Then, unless <samp>--no-init-file</samp> was given, R searches for a user
profile and sources it.  The name of this file is taken from the
environment variable <code>R_PROFILE_USER</code>; if unset, a file called
<samp>.Rprofile</samp> in the current directory or in the user&rsquo;s home
directory (in that order) is searched for.

</li><li> It also loads a saved workspace from file <samp>.RData</samp> in the current
directory if there is one (unless <samp>--no-restore</samp> or
<samp>--no-restore-data</samp> was specified).

</li><li> Finally, if a function <code>.First()</code> exists, it is executed.  This
function (as well as <code>.Last()</code> which is executed at the end of the
R session) can be defined in the appropriate startup profiles, or
reside in <samp>.RData</samp>.
</li></ul>

<p>In addition, there are options for controlling the memory available to
the R process (see the on-line help for topic &lsquo;<samp>Memory</samp>&rsquo; for more
information).  Users will not normally need to use these unless they
are trying to limit the amount of memory used by R.
</p>
<p>R accepts the following command-line options.
</p>
<dl compact="compact">
<dt><samp>--help</samp></dt>
<dt><samp>-h</samp></dt>
<dd><p>Print short help message to standard output and exit successfully.
</p>
</dd>
<dt><samp>--version</samp></dt>
<dd><p>Print version information to standard output and exit successfully.
</p>
</dd>
<dt><samp>--encoding=<var>enc</var></samp></dt>
<dd><p>Specify the encoding to be assumed for input from the console or
<code>stdin</code>.  This needs to be an encoding known to <code>iconv</code>: see
its help page.  (<code>--encoding <var>enc</var></code> is also accepted.)  The
input is re-encoded to the locale R is running in and needs to be
representable in the latter&rsquo;s encoding (so e.g.&nbsp;you cannot re-encode
Greek text in a French locale unless that locale uses the UTF-8
encoding).
</p>
</dd>
<dt><samp>RHOME</samp></dt>
<dd><p>Print the path to the R &ldquo;home directory&rdquo; to standard output and
exit successfully.  Apart from the front-end shell script and the man
page, R installation puts everything (executables, packages, etc.)
into this directory.
</p>
</dd>
<dt><samp>--save</samp></dt>
<dt><samp>--no-save</samp></dt>
<dd><p>Control whether data sets should be saved or not at the end of the R
session.  If neither is given in an interactive session, the user is
asked for the desired behavior when ending the session with <kbd>q()</kbd>;
in non-interactive use one of these must be specified or implied by some
other option (see below).
</p>
</dd>
<dt><samp>--no-environ</samp></dt>
<dd><p>Do not read any user file to set environment variables.
</p>
</dd>
<dt><samp>--no-site-file</samp></dt>
<dd><p>Do not read the site-wide profile at startup.
</p>
</dd>
<dt><samp>--no-init-file</samp></dt>
<dd><p>Do not read the user&rsquo;s profile at startup.
</p>
</dd>
<dt><samp>--restore</samp></dt>
<dt><samp>--no-restore</samp></dt>
<dt><samp>--no-restore-data</samp></dt>
<dd><p>Control whether saved images (file <samp>.RData</samp> in the directory where
R was started) should be restored at startup or not.  The default is
to restore. (<samp>--no-restore</samp> implies all the specific
<samp>--no-restore-*</samp> options.)
</p>
</dd>
<dt><samp>--no-restore-history</samp></dt>
<dd><p>Control whether the history file (normally file <samp>.Rhistory</samp> in the
directory where R was started, but can be set by the environment
variable <code>R_HISTFILE</code>) should be restored at startup or not.  The
default is to restore.
</p>
</dd>
<dt><samp>--no-Rconsole</samp></dt>
<dd><p>(Windows only) Prevent loading the <samp>Rconsole</samp> file at startup.
</p>
</dd>
<dt><samp>--vanilla</samp></dt>
<dd><p>Combine <samp>--no-save</samp>, <samp>--no-environ</samp>,
<samp>--no-site-file</samp>, <samp>--no-init-file</samp> and
<samp>--no-restore</samp>.  Under Windows, this also includes
<samp>--no-Rconsole</samp>.
</p>
</dd>
<dt><samp>-f <var>file</var></samp></dt>
<dt><samp>--file=<var>file</var></samp></dt>
<dd><p>(not <code>Rgui.exe</code>) Take input from <var>file</var>: &lsquo;<samp>-</samp>&rsquo; means
<code>stdin</code>.  Implies <samp>--no-save</samp> unless <samp>--save</samp> has
been set.  On a Unix-alike, shell metacharacters should be avoided in
<var>file</var> (but spaces are allowed).
</p>
</dd>
<dt><samp>-e <var>expression</var></samp></dt>
<dd><p>(not <code>Rgui.exe</code>) Use <var>expression</var> as an input line.  One or
more <samp>-e</samp> options can be used, but not together with <samp>-f</samp>
or <samp>--file</samp>.  Implies <samp>--no-save</samp> unless <samp>--save</samp>
has been set.  (There is a limit of 10,000 bytes on the total length of
expressions used in this way.  Expressions containing spaces or shell
metacharacters will need to be quoted.)
</p>
</dd>
<dt><samp>--no-readline</samp></dt>
<dd><p>(UNIX only) Turn off command-line editing via <strong>readline</strong>.  This
is useful when running R from within Emacs using the <acronym>ESS</acronym>
(&ldquo;Emacs Speaks Statistics&rdquo;) package.  See <a href="#The-command_002dline-editor">The command-line editor</a>,
for more information.  Command-line editing is enabled for default
interactive use (see <samp>--interactive</samp>).  This option also affects
tilde-expansion: see the help for <code>path.expand</code>.
</p>
</dd>
<dt><samp>--min-vsize=<var>N</var></samp></dt>
<dt><samp>--min-nsize=<var>N</var></samp></dt>
<dd><p>For expert use only: set the initial trigger sizes for garbage
collection of vector heap (in bytes) and <em>cons cells</em> (number)
respectively.  Suffix &lsquo;<samp>M</samp>&rsquo; specifies megabytes or millions of cells
respectively.  The defaults are 6Mb and 350k respectively and can also
be set by environment variables <code>R_NSIZE</code> and <code>R_VSIZE</code>.
</p>
</dd>
<dt><samp>--max-ppsize=<var>N</var></samp></dt>
<dd><p>Specify the maximum size of the pointer protection stack as <var>N</var>
locations.  This defaults to 10000, but can be increased to allow
large and complicated calculations to be done.  Currently the maximum
value accepted is 100000.
</p>
</dd>
<dt><samp>--max-mem-size=<var>N</var></samp></dt>
<dd><p>(Windows only) Specify a limit for the amount of memory to be used both
for R objects and working areas.  This is set by default to the
smaller of the amount of physical RAM in the machine and for 32-bit
R, 1.5Gb<a name="DOCF26" href="#FOOT26"><sup>26</sup></a>, and must be between 32Mb and the
maximum allowed on that version of Windows.
</p>
</dd>
<dt><samp>--quiet</samp></dt>
<dt><samp>--silent</samp></dt>
<dt><samp>-q</samp></dt>
<dd><p>Do not print out the initial copyright and welcome messages.
</p>
</dd>
<dt><samp>--slave</samp></dt>
<dd><p>Make R run as quietly as possible.  This option is intended to
support programs which use R to compute results for them.  It implies
<samp>--quiet</samp> and <samp>--no-save</samp>.
</p>
</dd>
<dt><samp>--interactive</samp></dt>
<dd><p>(UNIX only) Assert that R really is being run interactively even if
input has been redirected: use if input is from a FIFO or pipe and fed
from an interactive program.  (The default is to deduce that R is
being run interactively if and only if <samp>stdin</samp> is connected to a
terminal or <code>pty</code>.)  Using <samp>-e</samp>, <samp>-f</samp> or
<samp>--file</samp> asserts non-interactive use even if
<samp>--interactive</samp> is given.
</p>
<p>Note that this does not turn on command-line editing.
</p>
</dd>
<dt><samp>--ess</samp></dt>
<dd><p>(Windows only) Set <code>Rterm</code> up for use by <code>R-inferior-mode</code> in
<acronym>ESS</acronym>, including asserting interactive use (without the
command-line editor) and no buffering of <samp>stdout</samp>.
</p>
</dd>
<dt><samp>--verbose</samp></dt>
<dd><p>Print more information about progress, and in particular set R&rsquo;s
option <code>verbose</code> to <code>TRUE</code>.  R code uses this option to
control the printing of diagnostic messages.
</p>
</dd>
<dt><samp>--debugger=<var>name</var></samp></dt>
<dt><samp>-d <var>name</var></samp></dt>
<dd><p>(UNIX only) Run R through debugger <var>name</var>.  For most debuggers
(the exceptions are <code>valgrind</code> and recent versions of
<code>gdb</code>), further command line options are disregarded, and should
instead be given when starting the R executable from inside the
debugger.
</p>
</dd>
<dt><samp>--gui=<var>type</var></samp></dt>
<dt><samp>-g <var>type</var></samp></dt>
<dd><p>(UNIX only) Use <var>type</var> as graphical user interface (note that this
also includes interactive graphics).  Currently, possible values for
<var>type</var> are &lsquo;<samp>X11</samp>&rsquo; (the default) and, provided that &lsquo;<samp>Tcl/Tk</samp>&rsquo;
support is available, &lsquo;<samp>Tk</samp>&rsquo;. (For back-compatibility, &lsquo;<samp>x11</samp>&rsquo; and
&lsquo;<samp>tk</samp>&rsquo; are accepted.)
</p>
</dd>
<dt><samp>--arch=<var>name</var></samp></dt>
<dd><p>(UNIX only) Run the specified sub-architecture.
</p>
</dd>
<dt><samp>--args</samp></dt>
<dd><p>This flag does nothing except cause the rest of the command line to be
skipped: this can be useful to retrieve values from it with
<code>commandArgs(TRUE)</code>.
</p></dd>
</dl>

<p>Note that input and output can be redirected in the usual way (using
&lsquo;<samp>&lt;</samp>&rsquo; and &lsquo;<samp>&gt;</samp>&rsquo;), but the line length limit of 4095 bytes still
applies.  Warning and error messages are sent to the error channel
(<code>stderr</code>).
</p>
<p>The command <code>R CMD</code> allows the invocation of various tools which
are useful in conjunction with R, but not intended to be called
&ldquo;directly&rdquo;.  The general form is
</p>
<div class="example">
<pre class="example">R CMD <var>command</var> <var>args</var>
</pre></div>

<p>where <var>command</var> is the name of the tool and <var>args</var> the arguments
passed on to it.
</p>
<p>Currently, the following tools are available.
</p>
<dl compact="compact">
<dt><code>BATCH</code></dt>
<dd><p>Run R in batch mode.  Runs <code>R --restore --save</code> with possibly
further options (see <code>?BATCH</code>).
</p></dd>
<dt><code>COMPILE</code></dt>
<dd><p>(UNIX only) Compile C, C++, Fortran &hellip; files for use with R.
</p></dd>
<dt><code>SHLIB</code></dt>
<dd><p>Build shared library for dynamic loading.
</p></dd>
<dt><code>INSTALL</code></dt>
<dd><p>Install add-on packages.
</p></dd>
<dt><code>REMOVE</code></dt>
<dd><p>Remove add-on packages.
</p></dd>
<dt><code>build</code></dt>
<dd><p>Build (that is, package) add-on packages.
</p></dd>
<dt><code>check</code></dt>
<dd><p>Check add-on packages.
</p></dd>
<dt><code>LINK</code></dt>
<dd><p>(UNIX only) Front-end for creating executable programs.
</p></dd>
<dt><code>Rprof</code></dt>
<dd><p>Post-process R profiling files.
</p></dd>
<dt><code>Rdconv</code></dt>
<dt><code>Rd2txt</code></dt>
<dd><p>Convert Rd format to various other formats, including <acronym>HTML</acronym>, LaTeX,
plain text, and extracting the examples.  <code>Rd2txt</code> can be used as
shorthand for <code>Rd2conv -t txt</code>.
</p></dd>
<dt><code>Rd2pdf</code></dt>
<dd><p>Convert Rd format to PDF.
</p></dd>
<dt><code>Stangle</code></dt>
<dd><p>Extract S/R code from Sweave or other vignette documentation
</p></dd>
<dt><code>Sweave</code></dt>
<dd><p>Process Sweave or other vignette documentation
</p></dd>
<dt><code>Rdiff</code></dt>
<dd><p>Diff R output ignoring headers etc
</p></dd>
<dt><code>config</code></dt>
<dd><p>Obtain configuration information
</p></dd>
<dt><code>javareconf</code></dt>
<dd><p>(Unix only) Update the Java configuration variables
</p></dd>
<dt><code>rtags</code></dt>
<dd><p>(Unix only) Create Emacs-style tag files from C, R, and Rd files
</p></dd>
<dt><code>open</code></dt>
<dd><p>(Windows only) Open a file via Windows&rsquo; file associations
</p></dd>
<dt><code>texify</code></dt>
<dd><p>(Windows only) Process (La)TeX files with R&rsquo;s style files
</p></dd>
</dl>

<p>Use
</p>
<div class="example">
<pre class="example">R CMD <var>command</var> --help
</pre></div>

<p>to obtain usage information for each of the tools accessible via the
<code>R CMD</code> interface.
</p>
<p>In addition, you can use options <samp>--arch=</samp>,
<samp>--no-environ</samp>, <samp>--no-init-file</samp>, <samp>--no-site-file</samp>
and <samp>--vanilla</samp> between <code>R</code> and <code>CMD</code>: these
affect any R processes run by the tools.  (Here <samp>--vanilla</samp> is
equivalent to <samp>--no-environ --no-site-file --no-init-file</samp>.)
However, note that <code>R CMD</code> does not of itself use any R
startup files (in particular, neither user nor site <samp>Renviron</samp>
files), and all of the R processes run by these tools (except
<code>BATCH</code>) use <samp>--no-restore</samp>.  Most use <samp>--vanilla</samp>
and so invoke no R startup files: the current exceptions are
<code>INSTALL</code>, <code>REMOVE</code>, <code>Sweave</code> and
<code>SHLIB</code> (which uses <samp>--no-site-file --no-init-file</samp>).
</p>
<div class="example">
<pre class="example">R CMD <var>cmd</var> <var>args</var>
</pre></div>

<p>for any other executable <code><var>cmd</var></code> on the path or given by an
absolute filepath: this is useful to have the same environment as R
or the specific commands run under, for example to run <code>ldd</code> or
<code>pdflatex</code>.  Under Windows <var>cmd</var> can be an executable or a
batch file, or if it has extension <code>.sh</code> or <code>.pl</code> the
appropriate interpreter (if available) is called to run it.
</p>

<hr>
<a name="Invoking-R-under-Windows"></a>
<div class="header">
<p>
Next: <a href="#Invoking-R-under-macOS" accesskey="n" rel="next">Invoking R under macOS</a>, Previous: <a href="#Invoking-R-from-the-command-line" accesskey="p" rel="prev">Invoking R from the command line</a>, Up: <a href="#Invoking-R" accesskey="u" rel="up">Invoking R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Invoking-R-under-Windows-1"></a>
<h3 class="appendixsec">B.2 Invoking R under Windows</h3>

<p>There are two ways to run R under Windows.  Within a terminal window
(e.g.&nbsp;<code>cmd.exe</code> or a more capable shell), the methods described in
the previous section may be used, invoking by <code>R.exe</code> or more
directly by <code>Rterm.exe</code>.  For interactive use, there is a
console-based GUI (<code>Rgui.exe</code>).
</p>
<p>The startup procedure under Windows is very similar to that under
UNIX, but references to the &lsquo;home directory&rsquo; need to be clarified, as
this is not always defined on Windows.  If the environment variable
<code>R_USER</code> is defined, that gives the home directory.  Next, if the
environment variable <code>HOME</code> is defined, that gives the home
directory.  After those two user-controllable settings, R tries to
find system defined home directories.  It first tries to use the
Windows &quot;personal&quot; directory (typically <code>My Documents</code> in 
recent versions of Windows).  If that fails, and
environment variables <code>HOMEDRIVE</code> and <code>HOMEPATH</code> are defined
(and they normally are) these define the home directory.  Failing all
those, the home directory is taken to be the starting directory.
</p>
<p>You need to ensure that either the environment variables <code>TMPDIR</code>,
<code>TMP</code> and <code>TEMP</code> are either unset or one of them points to a
valid place to create temporary files and directories.
</p>
<p>Environment variables can be supplied as &lsquo;<samp><var>name</var>=<var>value</var></samp>&rsquo;
pairs on the command line.
</p>
<p>If there is an argument ending <samp>.RData</samp> (in any case) it is
interpreted as the path to the workspace to be restored: it implies
<samp>--restore</samp> and sets the working directory to the parent of the
named file.  (This mechanism is used for drag-and-drop and file
association with <code>RGui.exe</code>, but also works for <code>Rterm.exe</code>.
If the named file does not exist it sets the working directory
if the parent directory exists.)
</p>
<p>The following additional command-line options are available when
invoking <code>RGui.exe</code>.
</p>
<dl compact="compact">
<dt><samp>--mdi</samp></dt>
<dt><samp>--sdi</samp></dt>
<dt><samp>--no-mdi</samp></dt>
<dd><p>Control whether <code>Rgui</code> will operate as an MDI program 
(with multiple child windows within one main window) or an SDI application
(with multiple top-level windows for the console, graphics and pager).  The 
command-line setting overrides the setting in the user&rsquo;s <samp>Rconsole</samp> file.
</p>
</dd>
<dt><samp>--debug</samp></dt>
<dd><p>Enable the &ldquo;Break to debugger&rdquo; menu item in <code>Rgui</code>, and trigger
a break to the debugger during command line processing.
</p></dd>
</dl>

<p>Under Windows with <code>R CMD</code> you may also specify your own
<samp>.bat</samp>, <samp>.exe</samp>, <samp>.sh</samp> or <samp>.pl</samp> file.  It will be run
under the appropriate interpreter (Perl for <samp>.pl</samp>) with several
environment variables set appropriately, including <code>R_HOME</code>,
<code>R_OSTYPE</code>, <code>PATH</code>, <code>BSTINPUTS</code> and <code>TEXINPUTS</code>.  For
example, if you already have <samp>latex.exe</samp> on your path, then
</p>
<div class="example">
<pre class="example">R CMD latex.exe mydoc
</pre></div>
<p>will run LaTeX on <samp>mydoc.tex</samp>, with the path to R&rsquo;s
<samp>share/texmf</samp> macros appended to <code>TEXINPUTS</code>.  (Unfortunately,
this does not help with the MiKTeX build of LaTeX, but
<code>R CMD texify mydoc</code> will work in that case.)
</p>
<hr>
<a name="Invoking-R-under-macOS"></a>
<div class="header">
<p>
Next: <a href="#Scripting-with-R" accesskey="n" rel="next">Scripting with R</a>, Previous: <a href="#Invoking-R-under-Windows" accesskey="p" rel="prev">Invoking R under Windows</a>, Up: <a href="#Invoking-R" accesskey="u" rel="up">Invoking R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Invoking-R-under-macOS-1"></a>
<h3 class="appendixsec">B.3 Invoking R under macOS</h3>

<p>There are two ways to run R under macOS.  Within a <code>Terminal.app</code>
window by invoking <code>R</code>, the methods described in the first
subsection apply.  There is also console-based GUI (<code>R.app</code>) that by
default is installed in the <code>Applications</code> folder on your
system.  It is a standard double-clickable macOS application.
</p>
<p>The startup procedure under macOS is very similar to that under UNIX, but
<code>R.app</code> does not make use of command-line arguments.  The &lsquo;home
directory&rsquo; is the one inside the R.framework, but the startup and
current working directory are set as the user&rsquo;s home directory unless a
different startup directory is given in the Preferences window
accessible from within the GUI.
</p>
<hr>
<a name="Scripting-with-R"></a>
<div class="header">
<p>
Previous: <a href="#Invoking-R-under-macOS" accesskey="p" rel="prev">Invoking R under macOS</a>, Up: <a href="#Invoking-R" accesskey="u" rel="up">Invoking R</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Scripting-with-R-1"></a>
<h3 class="appendixsec">B.4 Scripting with R</h3>

<p>If you just want to run a file <samp>foo.R</samp> of R commands, the
recommended way is to use <code>R CMD BATCH foo.R</code>.  If you want to
run this in the background or as a batch job use OS-specific facilities
to do so: for example in most shells on Unix-alike OSes <code>R CMD
BATCH foo.R &amp;</code> runs a background job.
</p>
<p>You can pass parameters to scripts via additional arguments on the
command line: for example (where the exact quoting needed will depend on
the shell in use)
</p>
<div class="example">
<pre class="example">R CMD BATCH &quot;--args arg1 arg2&quot; foo.R &amp;
</pre></div>

<p>will pass arguments to a script which can be retrieved as a character
vector by
</p>
<div class="example">
<pre class="example">args &lt;- commandArgs(TRUE)
</pre></div>

<p>This is made simpler by the alternative front-end <code>Rscript</code>,
which can be invoked by
</p>
<div class="example">
<pre class="example">Rscript foo.R arg1 arg2
</pre></div>

<p>and this can also be used to write executable script files like (at
least on Unix-alikes, and in some Windows shells)
</p>
<div class="example">
<pre class="example">#! /path/to/Rscript
args &lt;- commandArgs(TRUE)
...
q(status=&lt;exit status code&gt;)
</pre></div>

<p>If this is entered into a text file <samp>runfoo</samp> and this is made
executable (by <code>chmod 755 runfoo</code>), it can be invoked for
different arguments by
</p>
<div class="example">
<pre class="example">runfoo arg1 arg2
</pre></div>

<p>For further options see <code>help(&quot;Rscript&quot;)</code>.  This writes R
output to <samp>stdout</samp> and <samp>stderr</samp>, and this can be redirected in
the usual way for the shell running the command.
</p>
<p>If you do not wish to hardcode the path to <code>Rscript</code> but have it
in your path (which is normally the case for an installed R except on
Windows, but e.g. macOS users may need to add <samp>/usr/local/bin</samp>
to their path), use
</p>
<div class="example">
<pre class="example">#! /usr/bin/env Rscript
...
</pre></div>

<p>At least in Bourne and bash shells, the <code>#!</code> mechanism does
<strong>not</strong> allow extra arguments like 
<code>#! /usr/bin/env Rscript --vanilla</code>.
</p>
<p>One thing to consider is what <code>stdin()</code> refers to.  It is
commonplace to write R scripts with segments like
</p>
<div class="example">
<pre class="example">chem &lt;- scan(n=24)
2.90 3.10 3.40 3.40 3.70 3.70 2.80 2.50 2.40 2.40 2.70 2.20
5.28 3.37 3.03 3.03 28.95 3.77 3.40 2.20 3.50 3.60 3.70 3.70
</pre></div>

<p>and <code>stdin()</code> refers to the script file to allow such traditional
usage.  If you want to refer to the process&rsquo;s <samp>stdin</samp>, use
<code>&quot;stdin&quot;</code> as a <code>file</code> connection, e.g. <code>scan(&quot;stdin&quot;, ...)</code>.
</p>
<p>Another way to write executable script files (suggested by Fran&ccedil;ois
Pinard) is to use a <em>here document</em> like
</p>
<div class="example">
<pre class="example">#!/bin/sh
[environment variables can be set here]
R --slave [other options] &lt;&lt;EOF

   R program goes here...

EOF
</pre></div>

<p>but here <code>stdin()</code> refers to the program source and
<code>&quot;stdin&quot;</code> will not be usable.
</p>
<p>Short scripts can be passed to <code>Rscript</code> on the command-line
<em>via</em> the <samp>-e</samp> flag.  (Empty scripts are not accepted.)
</p>
<p>Note that on a Unix-alike the input filename (such as <samp>foo.R</samp>)
should not contain spaces nor shell metacharacters.
</p>

<hr>
<a name="The-command_002dline-editor"></a>
<div class="header">
<p>
Next: <a href="#Function-and-variable-index" accesskey="n" rel="next">Function and variable index</a>, Previous: <a href="#Invoking-R" accesskey="p" rel="prev">Invoking R</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-command_002dline-editor-1"></a>
<h2 class="appendix">Appendix C The command-line editor</h2>

<a name="Preliminaries"></a>
<h3 class="appendixsection">C.1 Preliminaries</h3>

<p>When the <acronym>GNU</acronym> <strong>readline</strong> library is available at the
time R is configured for compilation under UNIX, an inbuilt command
line editor allowing recall, editing and re-submission of prior commands
is used.  Note that other versions of <strong>readline</strong> exist and may be
used by the inbuilt command line editor: this used to happen on macOS.
</p>
<p>It can be disabled (useful for usage with <acronym>ESS</acronym> <a name="DOCF27" href="#FOOT27"><sup>27</sup></a>) using the startup option
<samp>--no-readline</samp>.
</p>
<p>Windows versions of R have somewhat simpler command-line editing: see
&lsquo;<samp>Console</samp>&rsquo; under the &lsquo;<samp>Help</samp>&rsquo; menu of the <acronym>GUI</acronym>, and the
file <samp>README.Rterm</samp> for command-line editing under
<code>Rterm.exe</code>.
</p>
<p>When using R with GNU<a name="DOCF28" href="#FOOT28"><sup>28</sup></a> <strong>readline</strong> capabilities, the functions described
below are available, as well as others (probably) documented in
<code>man readline</code> or <code>info readline</code> on your system.
</p>
<p>Many of these use either Control or Meta characters.  Control
characters, such as <kbd>Control-m</kbd>, are obtained by holding the
<tt class="key">CTRL</tt> down while you press the <tt class="key">m</tt> key, and are written as
<kbd>C-m</kbd> below.  Meta characters, such as <kbd>Meta-b</kbd>, are typed by
holding down <tt class="key">META</tt><a name="DOCF29" href="#FOOT29"><sup>29</sup></a> and pressing <tt class="key">b</tt>, and written as <kbd>M-b</kbd>
in the following.  If your terminal does not have a <tt class="key">META</tt> key
enabled, you can still type Meta characters using two-character
sequences starting with <kbd>ESC</kbd>.  Thus, to enter <kbd>M-b</kbd>, you could
type <tt class="key">ESC</tt><tt class="key">b</tt>.  The <kbd>ESC</kbd> character sequences are also
allowed on terminals with real Meta keys.  Note that case is significant
for Meta characters.
</p>
<p>Some but not all versions<a name="DOCF30" href="#FOOT30"><sup>30</sup></a> of <strong>readline</strong>
will recognize resizing of the terminal window so this is best avoided.
</p>
<a name="Editing-actions"></a>
<h3 class="appendixsection">C.2 Editing actions</h3>

<p>The R program keeps a history of the command lines you type,
including the erroneous lines, and commands in your history may be
recalled, changed if necessary, and re-submitted as new commands.  In
Emacs-style command-line editing any straight typing you do while in
this editing phase causes the characters to be inserted in the command
you are editing, displacing any characters to the right of the cursor.
In <em>vi</em> mode character insertion mode is started by <kbd>M-i</kbd> or
<kbd>M-a</kbd>, characters are typed and insertion mode is finished by typing
a further <tt class="key">ESC</tt>.  (The default is Emacs-style, and only that is
described here: for <em>vi</em> mode see the <strong>readline</strong>
documentation.)
</p>
<p>Pressing the <tt class="key">RET</tt> command at any time causes the command to be
re-submitted.
</p>
<p>Other editing actions are summarized in the following table.
</p>
<a name="Command_002dline-editor-summary"></a>
<h3 class="appendixsection">C.3 Command-line editor summary</h3>

<a name="Command-recall-and-vertical-motion"></a>
<h4 class="subheading">Command recall and vertical motion</h4>

<dl compact="compact">
<dt><kbd>C-p</kbd></dt>
<dd><p>Go to the previous command (backwards in the history).
</p></dd>
<dt><kbd>C-n</kbd></dt>
<dd><p>Go to the next command (forwards in the history).
</p></dd>
<dt><kbd>C-r <var>text</var></kbd></dt>
<dd><p>Find the last command with the <var>text</var> string in it.  This can be
cancelled by <code>C-g</code> (and on some versions of R by <code>C-c</code>).
</p></dd>
</dl>

<p>On most terminals, you can also use the up and down arrow keys instead
of <kbd>C-p</kbd> and <kbd>C-n</kbd>, respectively.
</p>
<a name="Horizontal-motion-of-the-cursor"></a>
<h4 class="subheading">Horizontal motion of the cursor</h4>

<dl compact="compact">
<dt><kbd>C-a</kbd></dt>
<dd><p>Go to the beginning of the command.
</p></dd>
<dt><kbd>C-e</kbd></dt>
<dd><p>Go to the end of the line.
</p></dd>
<dt><kbd>M-b</kbd></dt>
<dd><p>Go back one word.
</p></dd>
<dt><kbd>M-f</kbd></dt>
<dd><p>Go forward one word.
</p></dd>
<dt><kbd>C-b</kbd></dt>
<dd><p>Go back one character.
</p></dd>
<dt><kbd>C-f</kbd></dt>
<dd><p>Go forward one character.
</p></dd>
</dl>

<p>On most terminals, you can also use the left and right arrow keys
instead of <kbd>C-b</kbd> and <kbd>C-f</kbd>, respectively.
</p>
<a name="Editing-and-re_002dsubmission"></a>
<h4 class="subheading">Editing and re-submission</h4>

<dl compact="compact">
<dt><kbd><var>text</var></kbd></dt>
<dd><p>Insert <var>text</var> at the cursor.
</p></dd>
<dt><kbd>C-f <var>text</var></kbd></dt>
<dd><p>Append <var>text</var> after the cursor.
</p></dd>
<dt><kbd><span class="key">DEL</span></kbd></dt>
<dd><p>Delete the previous character (left of the cursor).
</p></dd>
<dt><kbd>C-d</kbd></dt>
<dd><p>Delete the character under the cursor.
</p></dd>
<dt><kbd>M-d</kbd></dt>
<dd><p>Delete the rest of the word under the cursor, and &ldquo;save&rdquo; it.
</p></dd>
<dt><kbd>C-k</kbd></dt>
<dd><p>Delete from cursor to end of command, and &ldquo;save&rdquo; it.
</p></dd>
<dt><kbd>C-y</kbd></dt>
<dd><p>Insert (yank) the last &ldquo;saved&rdquo; text here.
</p></dd>
<dt><kbd>C-t</kbd></dt>
<dd><p>Transpose the character under the cursor with the next.
</p></dd>
<dt><kbd>M-l</kbd></dt>
<dd><p>Change the rest of the word to lower case.
</p></dd>
<dt><kbd>M-c</kbd></dt>
<dd><p>Change the rest of the word to upper case.
</p></dd>
<dt><kbd><span class="key">RET</span></kbd></dt>
<dd><p>Re-submit the command to R.
</p></dd>
</dl>

<p>The final <tt class="key">RET</tt> terminates the command line editing sequence.
</p>
<p>The <strong>readline</strong> key bindings can be customized in the usual way
<em>via</em> a <samp>~/.inputrc</samp> file.  These customizations can be
conditioned on application <code>R</code>, that is by including a section like
</p>
<div class="example">
<pre class="example">$if R
  &quot;\C-xd&quot;: &quot;q('no')\n&quot;
$endif
</pre></div>

<hr>
<a name="Function-and-variable-index"></a>
<div class="header">
<p>
Next: <a href="#Concept-index" accesskey="n" rel="next">Concept index</a>, Previous: <a href="#The-command_002dline-editor" accesskey="p" rel="prev">The command-line editor</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Function-and-variable-index-1"></a>
<h2 class="appendix">Appendix D Function and variable index</h2>

<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Function-and-variable-index_vr_symbol-1"><b>!</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-2"><b>%</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-3"><b>&amp;</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-4"><b>*</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-5"><b>+</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-6"><b>-</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-7"><b>.</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-8"><b>/</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-9"><b>:</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-10"><b>&lt;</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-11"><b>=</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-12"><b>&gt;</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-13"><b>?</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-14"><b>^</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-15"><b>|</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-16"><b>~</b></a>
 &nbsp; 
<br>
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-H"><b>H</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-J"><b>J</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-K"><b>K</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-X"><b>X</b></a>
 &nbsp; 
</td></tr></table>
<table summary="" class="index-vr" border="0">
<tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-1">!</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0021"><code>!</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0021_003d"><code>!=</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-2">%</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0025_002a_0025"><code>%*%</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiplication">Multiplication</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0025o_0025"><code>%o%</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-outer-product-of-two-arrays">The outer product of two arrays</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-3">&amp;</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0026"><code>&amp;</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0026_0026"><code>&amp;&amp;</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Conditional-execution">Conditional execution</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-4">*</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002a"><code>*</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-5">+</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002b"><code>+</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-6">-</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002d"><code>-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-7">.</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002e"><code>.</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Updating-fitted-models">Updating fitted models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eFirst"><code>.First</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Customizing-the-environment">Customizing the environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eLast"><code>.Last</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Customizing-the-environment">Customizing the environment</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-8">/</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002f"><code>/</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-9">:</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003a"><code>:</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generating-regular-sequences">Generating regular sequences</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003a_003a"><code>::</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Namespaces">Namespaces</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003a_003a_003a"><code>:::</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Namespaces">Namespaces</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-10">&lt;</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003c"><code>&lt;</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003c_003c_002d"><code>&lt;&lt;-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003c_003d"><code>&lt;=</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-11">=</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003d_003d"><code>==</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-12">&gt;</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003e"><code>&gt;</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003e_003d"><code>&gt;=</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-13">?</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003f"><code>?</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-help">Getting help</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_003f_003f"><code>??</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-help">Getting help</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-14">^</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_005e"><code>^</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-15">|</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_007c"><code>|</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_007c_007c"><code>||</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Conditional-execution">Conditional execution</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_symbol-16">~</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_007e"><code>~</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Formulae-for-statistical-models">Formulae for statistical models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-A">A</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-abline"><code>abline</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ace"><code>ace</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-add1"><code>add1</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Updating-fitted-models">Updating fitted models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-anova"><code>anova</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-anova-1"><code>anova</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#ANOVA-tables">ANOVA tables</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-aov"><code>aov</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Analysis-of-variance-and-model-comparison">Analysis of variance and model comparison</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-aperm"><code>aperm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generalized-transpose-of-an-array">Generalized transpose of an array</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-array"><code>array</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-array_0028_0029-function">The array() function</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-as_002edata_002eframe"><code>as.data.frame</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Making-data-frames">Making data frames</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-as_002evector"><code>as.vector</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-concatenation-function-c_0028_0029-with-arrays">The concatenation function c() with arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attach"><code>attach</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#attach_0028_0029-and-detach_0028_0029">attach() and detach()</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attr"><code>attr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attr-1"><code>attr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attributes"><code>attributes</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attributes-1"><code>attributes</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-and-setting-attributes">Getting and setting attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-avas"><code>avas</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-axis"><code>axis</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-B">B</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-boxplot"><code>boxplot</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-break"><code>break</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Repetitive-execution">Repetitive execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-bruto"><code>bruto</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-C">C</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-c"><code>c</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vectors-and-assignment">Vectors and assignment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-c-1"><code>c</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-vectors">Character vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-c-2"><code>c</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-concatenation-function-c_0028_0029-with-arrays">The concatenation function c() with arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-c-3"><code>c</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Concatenating-lists">Concatenating lists</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-C"><code>C</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Contrasts">Contrasts</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-cbind"><code>cbind</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Forming-partitioned-matrices">Forming partitioned matrices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coef"><code>coef</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coefficients"><code>coefficients</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-contour"><code>contour</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-contrasts"><code>contrasts</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Contrasts">Contrasts</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coplot"><code>coplot</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Displaying-multivariate-data">Displaying multivariate data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-cos"><code>cos</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-crossprod"><code>crossprod</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Index-matrices">Index matrices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-crossprod-1"><code>crossprod</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiplication">Multiplication</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-cut"><code>cut</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Frequency-tables-from-factors">Frequency tables from factors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-D">D</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-data"><code>data</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Accessing-builtin-datasets">Accessing builtin datasets</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-data_002eframe"><code>data.frame</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Making-data-frames">Making data frames</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-density"><code>density</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-det"><code>det</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-detach"><code>detach</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#attach_0028_0029-and-detach_0028_0029">attach() and detach()</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-determinant"><code>determinant</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dev_002elist"><code>dev.list</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiple-graphics-devices">Multiple graphics devices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dev_002enext"><code>dev.next</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiple-graphics-devices">Multiple graphics devices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dev_002eoff"><code>dev.off</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiple-graphics-devices">Multiple graphics devices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dev_002eprev"><code>dev.prev</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiple-graphics-devices">Multiple graphics devices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dev_002eset"><code>dev.set</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiple-graphics-devices">Multiple graphics devices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-deviance"><code>deviance</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-diag"><code>diag</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiplication">Multiplication</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dim"><code>dim</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Arrays">Arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dotchart"><code>dotchart</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-drop1"><code>drop1</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Updating-fitted-models">Updating fitted models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-E">E</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-ecdf"><code>ecdf</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-edit"><code>edit</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Editing-data">Editing data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-eigen"><code>eigen</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Eigenvalues-and-eigenvectors">Eigenvalues and eigenvectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-else"><code>else</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Conditional-execution">Conditional execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Error"><code>Error</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Analysis-of-variance-and-model-comparison">Analysis of variance and model comparison</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-example"><code>example</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-help">Getting help</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-exp"><code>exp</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-F">F</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-F"><code>F</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-factor"><code>factor</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Factors">Factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-FALSE"><code>FALSE</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-fivenum"><code>fivenum</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-for"><code>for</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Repetitive-execution">Repetitive execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-formula"><code>formula</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function"><code>function</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Writing-your-own-functions">Writing your own functions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-G">G</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-getAnywhere"><code>getAnywhere</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object-orientation">Object orientation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-getS3method"><code>getS3method</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object-orientation">Object orientation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-glm"><code>glm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-glm_0028_0029-function">The glm() function</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-H">H</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-help"><code>help</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-help">Getting help</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-help-1"><code>help</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-help">Getting help</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-help_002esearch"><code>help.search</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-help">Getting help</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-help_002estart"><code>help.start</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Getting-help">Getting help</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-hist"><code>hist</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-hist-1"><code>hist</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-I">I</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-identify"><code>identify</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Interacting-with-graphics">Interacting with graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-if"><code>if</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Conditional-execution">Conditional execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-if-1"><code>if</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Conditional-execution">Conditional execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ifelse"><code>ifelse</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Conditional-execution">Conditional execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-image"><code>image</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-is_002ena"><code>is.na</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Missing-values">Missing values</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-is_002enan"><code>is.nan</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Missing-values">Missing values</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-J">J</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-jpeg"><code>jpeg</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-K">K</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-ks_002etest"><code>ks.test</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-L">L</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-legend"><code>legend</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-length"><code>length</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-length-1"><code>length</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-intrinsic-attributes-mode-and-length">The intrinsic attributes mode and length</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-levels"><code>levels</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Factors">Factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-lines"><code>lines</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-list"><code>list</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lists">Lists</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-lm"><code>lm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Linear-models">Linear models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-lme"><code>lme</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-locator"><code>locator</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Interacting-with-graphics">Interacting with graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-loess"><code>loess</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-loess-1"><code>loess</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-log"><code>log</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-lqs"><code>lqs</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-lsfit"><code>lsfit</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-M">M</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-mars"><code>mars</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-max"><code>max</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-mean"><code>mean</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-methods"><code>methods</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object-orientation">Object orientation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-min"><code>min</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-mode"><code>mode</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-intrinsic-attributes-mode-and-length">The intrinsic attributes mode and length</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-N">N</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-NA"><code>NA</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Missing-values">Missing values</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-NaN"><code>NaN</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Missing-values">Missing values</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ncol"><code>ncol</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Matrix-facilities">Matrix facilities</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-next"><code>next</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Repetitive-execution">Repetitive execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-nlm"><code>nlm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-nlm-1"><code>nlm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Least-squares">Least squares</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-nlm-2"><code>nlm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Maximum-likelihood">Maximum likelihood</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-nlme"><code>nlme</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-nlminb"><code>nlminb</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-nrow"><code>nrow</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Matrix-facilities">Matrix facilities</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-O">O</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-optim"><code>optim</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-order"><code>order</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ordered"><code>ordered</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Ordered-factors">Ordered factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-ordered-1"><code>ordered</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Ordered-factors">Ordered factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-outer"><code>outer</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-outer-product-of-two-arrays">The outer product of two arrays</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-P">P</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-pairs"><code>pairs</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Displaying-multivariate-data">Displaying multivariate data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-par"><code>par</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-par_0028_0029-function">The par() function</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-paste"><code>paste</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-vectors">Character vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-pdf"><code>pdf</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-persp"><code>persp</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-plot"><code>plot</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-plot-1"><code>plot</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-plot_0028_0029-function">The plot() function</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-pmax"><code>pmax</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-pmin"><code>pmin</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-png"><code>png</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-points"><code>points</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-polygon"><code>polygon</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-postscript"><code>postscript</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-predict"><code>predict</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-print"><code>print</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-prod"><code>prod</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-Q">Q</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-qqline"><code>qqline</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-qqline-1"><code>qqline</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-qqnorm"><code>qqnorm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-qqnorm-1"><code>qqnorm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-qqplot"><code>qqplot</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Display-graphics">Display graphics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-qr"><code>qr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-quartz"><code>quartz</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-R">R</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-range"><code>range</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rbind"><code>rbind</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Forming-partitioned-matrices">Forming partitioned matrices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-read_002etable"><code>read.table</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-read_002etable_0028_0029-function">The read.table() function</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rep"><code>rep</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generating-regular-sequences">Generating regular sequences</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-repeat"><code>repeat</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Repetitive-execution">Repetitive execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-resid"><code>resid</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-residuals"><code>residuals</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rlm"><code>rlm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-rm"><code>rm</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Data-permanency-and-removing-objects">Data permanency and removing objects</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-S">S</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-scan"><code>scan</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-scan_0028_0029-function">The scan() function</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sd"><code>sd</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-function-tapply_0028_0029-and-ragged-arrays">The function tapply() and ragged arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-search"><code>search</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Managing-the-search-path">Managing the search path</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-seq"><code>seq</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generating-regular-sequences">Generating regular sequences</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-shapiro_002etest"><code>shapiro.test</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sin"><code>sin</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sink"><code>sink</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Executing-commands-from-or-diverting-output-to-a-file">Executing commands from or diverting output to a file</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-solve"><code>solve</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Linear-equations-and-inversion">Linear equations and inversion</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sort"><code>sort</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-source"><code>source</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Executing-commands-from-or-diverting-output-to-a-file">Executing commands from or diverting output to a file</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-split"><code>split</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Repetitive-execution">Repetitive execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sqrt"><code>sqrt</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-stem"><code>stem</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-step"><code>step</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-step-1"><code>step</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Updating-fitted-models">Updating fitted models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-sum"><code>sum</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-summary"><code>summary</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-summary-1"><code>summary</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-svd"><code>svd</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-T">T</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-T"><code>T</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-t"><code>t</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generalized-transpose-of-an-array">Generalized transpose of an array</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-t_002etest"><code>t.test</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-table"><code>table</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Index-matrices">Index matrices</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-table-1"><code>table</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Frequency-tables-from-factors">Frequency tables from factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-tan"><code>tan</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-tapply"><code>tapply</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-function-tapply_0028_0029-and-ragged-arrays">The function tapply() and ragged arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-text"><code>text</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-title"><code>title</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Low_002dlevel-plotting-commands">Low-level plotting commands</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-tree"><code>tree</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-TRUE"><code>TRUE</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Logical-vectors">Logical vectors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-U">U</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-unclass"><code>unclass</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-class-of-an-object">The class of an object</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-update"><code>update</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Updating-fitted-models">Updating fitted models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-V">V</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-var"><code>var</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-var-1"><code>var</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-function-tapply_0028_0029-and-ragged-arrays">The function tapply() and ragged arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-var_002etest"><code>var.test</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-vcov"><code>vcov</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generic-functions-for-extracting-model-information">Generic functions for extracting model information</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-vector"><code>vector</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vectors-and-assignment">Vectors and assignment</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-W">W</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-while"><code>while</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Repetitive-execution">Repetitive execution</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-wilcox_002etest"><code>wilcox.test</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-windows"><code>windows</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-variable-index_vr_letter-X">X</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-X11"><code>X11</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
</table>
<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Function-and-variable-index_vr_symbol-1"><b>!</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-2"><b>%</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-3"><b>&amp;</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-4"><b>*</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-5"><b>+</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-6"><b>-</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-7"><b>.</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-8"><b>/</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-9"><b>:</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-10"><b>&lt;</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-11"><b>=</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-12"><b>&gt;</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-13"><b>?</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-14"><b>^</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-15"><b>|</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_symbol-16"><b>~</b></a>
 &nbsp; 
<br>
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-H"><b>H</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-J"><b>J</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-K"><b>K</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-W"><b>W</b></a>
 &nbsp; 
<a class="summary-letter" href="#Function-and-variable-index_vr_letter-X"><b>X</b></a>
 &nbsp; 
</td></tr></table>

<hr>
<a name="Concept-index"></a>
<div class="header">
<p>
Next: <a href="#References" accesskey="n" rel="next">References</a>, Previous: <a href="#Function-and-variable-index" accesskey="p" rel="prev">Function and variable index</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Concept-index-1"></a>
<h2 class="appendix">Appendix E Concept index</h2>

<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Concept-index_cp_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-K"><b>K</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-W"><b>W</b></a>
 &nbsp; 
</td></tr></table>
<table summary="" class="index-cp" border="0">
<tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-A">A</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Accessing-builtin-datasets">Accessing builtin datasets</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Accessing-builtin-datasets">Accessing builtin datasets</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Additive-models">Additive models</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Analysis-of-variance">Analysis of variance</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Analysis-of-variance-and-model-comparison">Analysis of variance and model comparison</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Arithmetic-functions-and-operators">Arithmetic functions and operators</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Arrays">Arrays</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Arrays">Arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Assignment">Assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vectors-and-assignment">Vectors and assignment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Attributes">Attributes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-B">B</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Binary-operators">Binary operators</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Defining-new-binary-operators">Defining new binary operators</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Box-plots">Box plots</a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-C">C</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Character-vectors">Character vectors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Character-vectors">Character vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Classes">Classes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-class-of-an-object">The class of an object</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Classes-1">Classes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object-orientation">Object orientation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Concatenating-lists">Concatenating lists</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Concatenating-lists">Concatenating lists</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Contrasts">Contrasts</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Contrasts">Contrasts</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Control-statements">Control statements</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Control-statements">Control statements</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-CRAN">CRAN</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Contributed-packages-and-CRAN">Contributed packages and CRAN</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Customizing-the-environment">Customizing the environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Customizing-the-environment">Customizing the environment</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-D">D</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Data-frames">Data frames</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Data-frames">Data frames</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Default-values">Default values</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Named-arguments-and-defaults">Named arguments and defaults</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Density-estimation">Density estimation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Determinants">Determinants</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Diverting-input-and-output">Diverting input and output</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Executing-commands-from-or-diverting-output-to-a-file">Executing commands from or diverting output to a file</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Dynamic-graphics">Dynamic graphics</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Dynamic-graphics">Dynamic graphics</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-E">E</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Eigenvalues-and-eigenvectors">Eigenvalues and eigenvectors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Eigenvalues-and-eigenvectors">Eigenvalues and eigenvectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Empirical-CDFs">Empirical CDFs</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-F">F</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Factors">Factors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Factors">Factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Factors-1">Factors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Contrasts">Contrasts</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Families">Families</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Families">Families</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Formulae">Formulae</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Formulae-for-statistical-models">Formulae for statistical models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-G">G</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Generalized-linear-models">Generalized linear models</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generalized-linear-models">Generalized linear models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Generalized-transpose-of-an-array">Generalized transpose of an array</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generalized-transpose-of-an-array">Generalized transpose of an array</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Generic-functions">Generic functions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object-orientation">Object orientation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Graphics-device-drivers">Graphics device drivers</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Device-drivers">Device drivers</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Graphics-parameters">Graphics parameters</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-par_0028_0029-function">The par() function</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Grouped-expressions">Grouped expressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Grouped-expressions">Grouped expressions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-I">I</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Indexing-of-and-by-arrays">Indexing of and by arrays</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Array-indexing">Array indexing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Indexing-vectors">Indexing vectors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Index-vectors">Index vectors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-K">K</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Kolmogorov_002dSmirnov-test">Kolmogorov-Smirnov test</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-L">L</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Least-squares-fitting">Least squares fitting</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Linear-equations">Linear equations</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Linear-equations-and-inversion">Linear equations and inversion</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Linear-models">Linear models</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Linear-models">Linear models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Lists">Lists</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lists">Lists</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Local-approximating-regressions">Local approximating regressions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Loops-and-conditional-execution">Loops and conditional execution</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Loops-and-conditional-execution">Loops and conditional execution</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-M">M</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Matrices">Matrices</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Arrays">Arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Matrix-multiplication">Matrix multiplication</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Multiplication">Multiplication</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Maximum-likelihood">Maximum likelihood</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Maximum-likelihood">Maximum likelihood</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Missing-values">Missing values</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Missing-values">Missing values</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Mixed-models">Mixed models</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-N">N</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Named-arguments">Named arguments</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Named-arguments-and-defaults">Named arguments and defaults</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Namespace">Namespace</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Namespaces">Namespaces</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Nonlinear-least-squares">Nonlinear least squares</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Nonlinear-least-squares-and-maximum-likelihood-models">Nonlinear least squares and maximum likelihood models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-O">O</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Object-orientation">Object orientation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object-orientation">Object orientation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Objects">Objects</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-One_002d-and-two_002dsample-tests">One- and two-sample tests</a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Ordered-factors">Ordered factors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Factors">Factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Ordered-factors-1">Ordered factors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Contrasts">Contrasts</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Outer-products-of-arrays">Outer products of arrays</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-outer-product-of-two-arrays">The outer product of two arrays</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-P">P</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Packages">Packages</a>:</td><td>&nbsp;</td><td valign="top"><a href="#R-and-statistics">R and statistics</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Packages-1">Packages</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Packages">Packages</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Probability-distributions">Probability distributions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Probability-distributions">Probability distributions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-Q">Q</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-QR-decomposition">QR decomposition</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Least-squares-fitting-and-the-QR-decomposition">Least squares fitting and the QR decomposition</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Quantile_002dquantile-plots">Quantile-quantile plots</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-R">R</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Reading-data-from-files">Reading data from files</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Reading-data-from-files">Reading data from files</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Recycling-rule">Recycling rule</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-arithmetic">Vector arithmetic</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Recycling-rule-1">Recycling rule</a>:</td><td>&nbsp;</td><td valign="top"><a href="#The-recycling-rule">The recycling rule</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Regular-sequences">Regular sequences</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Generating-regular-sequences">Generating regular sequences</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Removing-objects">Removing objects</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Data-permanency-and-removing-objects">Data permanency and removing objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Robust-regression">Robust regression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-S">S</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Scope">Scope</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Search-path">Search path</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Managing-the-search-path">Managing the search path</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Shapiro_002dWilk-test">Shapiro-Wilk test</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Examining-the-distribution-of-a-set-of-data">Examining the distribution of a set of data</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Singular-value-decomposition">Singular value decomposition</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Singular-value-decomposition-and-determinants">Singular value decomposition and determinants</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Statistical-models">Statistical models</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Statistical-models-in-R">Statistical models in R</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Student_0027s-t-test">Student&rsquo;s <em>t</em> test</a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-T">T</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Tabulation">Tabulation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Frequency-tables-from-factors">Frequency tables from factors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Tree_002dbased-models">Tree-based models</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Some-non_002dstandard-models">Some non-standard models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-U">U</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Updating-fitted-models">Updating fitted models</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Updating-fitted-models">Updating fitted models</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-V">V</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Vectors">Vectors</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Simple-manipulations-numbers-and-vectors">Simple manipulations numbers and vectors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-index_cp_letter-W">W</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-Wilcoxon-test">Wilcoxon test</a>:</td><td>&nbsp;</td><td valign="top"><a href="#One_002d-and-two_002dsample-tests">One- and two-sample tests</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Workspace">Workspace</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Data-permanency-and-removing-objects">Data permanency and removing objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Writing-functions">Writing functions</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Writing-your-own-functions">Writing your own functions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
</table>
<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Concept-index_cp_letter-A"><b>A</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-B"><b>B</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-C"><b>C</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-D"><b>D</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-E"><b>E</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-F"><b>F</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-G"><b>G</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-I"><b>I</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-K"><b>K</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-L"><b>L</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-M"><b>M</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-N"><b>N</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-O"><b>O</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-P"><b>P</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-Q"><b>Q</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-R"><b>R</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-S"><b>S</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-T"><b>T</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-U"><b>U</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-V"><b>V</b></a>
 &nbsp; 
<a class="summary-letter" href="#Concept-index_cp_letter-W"><b>W</b></a>
 &nbsp; 
</td></tr></table>

<hr>
<a name="References"></a>
<div class="header">
<p>
Previous: <a href="#Concept-index" accesskey="p" rel="prev">Concept index</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-variable-index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="References-1"></a>
<h2 class="appendix">Appendix F References</h2>

<p>D. M. Bates and  D. G. Watts (1988), <em>Nonlinear Regression
Analysis and Its Applications.</em> John Wiley &amp; Sons, New York.
</p>
<p>Richard A. Becker, John M. Chambers and Allan R. Wilks (1988),
<em>The New S Language.</em> Chapman &amp; Hall, New York.
This book is often called the &ldquo;<em>Blue Book</em>&rdquo;.
</p>
<p>John M. Chambers and Trevor J. Hastie eds. (1992),
<em>Statistical Models in S.</em> Chapman &amp; Hall, New York.
This is also called the &ldquo;<em>White Book</em>&rdquo;.
</p>
<p>John M. Chambers (1998)
<em>Programming with Data</em>. Springer, New York.
This is also called the &ldquo;<em>Green Book</em>&rdquo;.
</p>
<p>A. C. Davison and D. V. Hinkley (1997), <em>Bootstrap Methods
and Their Applications</em>, Cambridge University Press.
</p>
<p>Annette J. Dobson (1990), <em>An Introduction to Generalized Linear
Models</em>, Chapman and Hall, London.
</p>
<p>Peter McCullagh and John A. Nelder (1989), <em>Generalized Linear
Models.</em> Second edition, Chapman and Hall, London.
</p>
<p>John A.&nbsp;Rice (1995), <em>Mathematical Statistics and Data Analysis.</em>
Second edition.  Duxbury Press, Belmont, CA.
</p>
<p>S. D. Silvey (1970), <em>Statistical Inference.</em> Penguin, London.
</p>
<div class="footnote">
<hr>
<h4 class="footnotes-heading">Footnotes</h4>

<h3><a name="FOOT1" href="#DOCF1">(1)</a></h3>
<p>ACM Software Systems award, 1998:
<a href="https://awards.acm.org/award_winners/chambers_6640862.cfm">https://awards.acm.org/award_winners/chambers_6640862.cfm</a>.</p>
<h3><a name="FOOT2" href="#DOCF2">(2)</a></h3>
<p>For portable R code (including that to
be used in R packages) only A&ndash;Za&ndash;z0&ndash;9 should be used.</p>
<h3><a name="FOOT3" href="#DOCF3">(3)</a></h3>
<p><strong>not</strong> inside strings,
nor within the argument list of a function definition</p>
<h3><a name="FOOT4" href="#DOCF4">(4)</a></h3>
<p>some of the
consoles will not allow you to enter more, and amongst those which do
some will silently discard the excess and some will use it as the start
of the next line.</p>
<h3><a name="FOOT5" href="#DOCF5">(5)</a></h3>
<p>of unlimited length.</p>
<h3><a name="FOOT6" href="#DOCF6">(6)</a></h3>
<p>The leading &ldquo;dot&rdquo; in
this file name makes it <em>invisible</em> in normal file listings in
UNIX, and in default GUI file listings on macOS and Windows.</p>
<h3><a name="FOOT7" href="#DOCF7">(7)</a></h3>
<p>With other than vector types of argument,
such as <code>list</code> mode arguments, the action of <code>c()</code> is rather
different.  See <a href="#Concatenating-lists">Concatenating lists</a>.</p>
<h3><a name="FOOT8" href="#DOCF8">(8)</a></h3>
<p>Actually, it is still available as
<code>.Last.value</code> before any other statements are executed.</p>
<h3><a name="FOOT9" href="#DOCF9">(9)</a></h3>
<p><code>paste(..., collapse=<var>ss</var>)</code> joins the
arguments into a single character string putting <var>ss</var> in between, e.g.,
<code>ss &lt;- &quot;|&quot;</code>.  There are more tools for character manipulation, see the help
for <code>sub</code> and <code>substring</code>.</p>
<h3><a name="FOOT10" href="#DOCF10">(10)</a></h3>
<p><em>numeric</em> mode is
actually an amalgam of two distinct modes, namely <em>integer</em> and
<em>double</em> precision, as explained in the manual.</p>
<h3><a name="FOOT11" href="#DOCF11">(11)</a></h3>
<p>Note however that <code>length(<var>object</var>)</code> does not always
contain intrinsic useful information, e.g., when <code><var>object</var></code> is a
function.</p>
<h3><a name="FOOT12" href="#DOCF12">(12)</a></h3>
<p>In general, coercion
from numeric to character and back again will not be exactly reversible,
because of roundoff errors in the character representation.</p>
<h3><a name="FOOT13" href="#DOCF13">(13)</a></h3>
<p>A different style using
&lsquo;formal&rsquo; or &lsquo;S4&rsquo; classes is provided in package <code>methods</code>.</p>
<h3><a name="FOOT14" href="#DOCF14">(14)</a></h3>
<p>Readers should note
that there are eight states and territories in Australia, namely the
Australian Capital Territory, New South Wales, the Northern Territory,
Queensland, South Australia, Tasmania, Victoria and Western Australia.</p>
<h3><a name="FOOT15" href="#DOCF15">(15)</a></h3>
<p>Note that <code>tapply()</code> also works in this case
when its second argument is not a factor, e.g.,
&lsquo;<samp><code>tapply(incomes, state)</code></samp>&rsquo;, and this is true for quite a few
other functions, since arguments are <em>coerced</em> to factors when
necessary (using <code>as.factor()</code>).</p>
<h3><a name="FOOT16" href="#DOCF16">(16)</a></h3>
<p>Note that <code>x %*% x</code> is ambiguous, as
it could mean either x&rsquo;x or x x&rsquo;, where x is the
column form.  In such cases the smaller matrix seems implicitly to be
the interpretation adopted, so the scalar x&rsquo;x is in this case the
result.  The matrix x x&rsquo; may be calculated either by <code>cbind(x)
%*% x</code> or <code>x %*% rbind(x)</code> since the result of <code>rbind()</code> or
<code>cbind()</code> is always a matrix.  However, the best way to compute
x&rsquo;x or x x&rsquo; is <code>crossprod(x)</code> or <code>x %o% x</code> respectively.</p>
<h3><a name="FOOT17" href="#DOCF17">(17)</a></h3>
<p>Even better would be to form a matrix square
root B with A = BB&rsquo; and find the squared length
of the solution of By = x , perhaps using the Cholesky or
eigen decomposition of A.  </p>
<h3><a name="FOOT18" href="#DOCF18">(18)</a></h3>
<p>Conversion of character columns to factors is
overridden using the <code>stringsAsFactors</code> argument to the
<code>data.frame()</code> function.</p>
<h3><a name="FOOT19" href="#DOCF19">(19)</a></h3>
<p>See the on-line help
for <code>autoload</code> for the meaning of the second term.</p>
<h3><a name="FOOT20" href="#DOCF20">(20)</a></h3>
<p>Under UNIX, the utilities
<code>sed</code> or<code>awk</code> can be used.</p>
<h3><a name="FOOT21" href="#DOCF21">(21)</a></h3>
<p>to be
discussed later, or use <code>xyplot</code> from package <a href="https://CRAN.R-project.org/package=lattice"><strong>lattice</strong></a>.</p>
<h3><a name="FOOT22" href="#DOCF22">(22)</a></h3>
<p>See also the methods described in <a href="#Statistical-models-in-R">Statistical models in R</a></p>
<h3><a name="FOOT23" href="#DOCF23">(23)</a></h3>
<p>In some sense this
mimics the behavior in <small>S-PLUS</small> since in <small>S-PLUS</small> this operator always
creates or assigns to a global variable.</p>
<h3><a name="FOOT24" href="#DOCF24">(24)</a></h3>
<p>So it is hidden under
UNIX.</p>
<h3><a name="FOOT25" href="#DOCF25">(25)</a></h3>
<p>Some graphics
parameters such as the size of the current device are for information
only.</p>
<h3><a name="FOOT26" href="#DOCF26">(26)</a></h3>
<p>2.5Gb on versions of Windows that support 3Gb per
process and have the support enabled: see the <samp>rw-FAQ</samp> Q2.9; 3.5Gb
on most 64-bit versions of Windows.</p>
<h3><a name="FOOT27" href="#DOCF27">(27)</a></h3>
<p>The
&lsquo;Emacs Speaks Statistics&rsquo; package; see the <acronym>URL</acronym>
<a href="https://ESS.R-project.org/">https://ESS.R-project.org/</a></p>
<h3><a name="FOOT28" href="#DOCF28">(28)</a></h3>
<p>It is possible to build R using an
emulation of GNU <strong>readline</strong>, such as one based on NetBSD&rsquo;s
<strong>editline</strong>, it which case only a subset of the capabilities may
be provided.</p>
<h3><a name="FOOT29" href="#DOCF29">(29)</a></h3>
<p>On a PC keyboard this is usually the
Alt key, occasionally the &lsquo;Windows&rsquo; key.  On a Mac keyboard normally no
meta key is available.</p>
<h3><a name="FOOT30" href="#DOCF30">(30)</a></h3>
<p>In particular, not versions 6.3 or
later: this is worked around as from R 3.4.0.</p>
</div>
<hr>



</body>
</html>