Now that our data is loaded in memory, we can start doing things with it.
If we want to get a single value from the data frame, we can provide an index in square brackets, just as we do in math:
#first value in gapminder
gapminder[1, 1]
## [1] Afghanistan
## 142 Levels: Afghanistan Albania Algeria Angola Argentina ... Zimbabwe
#middle value in gapminder
gapminder[3, 3]
## [1] 10267083
An index like [3, 3]
selects a single element of a data frame, but we can select whole sections as well. For example, we can select the first two rows of values for the first four columns like this:
gapminder[1:2, 1:4]
## country year pop continent
## 1 Afghanistan 1952 8425333 Asia
## 2 Afghanistan 1957 9240934 Asia
The slice 1:4
means, “Start at index 1 and go to index 4.”
The slice does not need to start at 1, e.g. the line below selects rows 5 through 10:
gapminder[5:10, 1:4]
## country year pop continent
## 5 Afghanistan 1972 13079460 Asia
## 6 Afghanistan 1977 14880372 Asia
## 7 Afghanistan 1982 12881816 Asia
## 8 Afghanistan 1987 13867957 Asia
## 9 Afghanistan 1992 16317921 Asia
## 10 Afghanistan 1997 22227415 Asia
We can use the function c
, which stands for combine, to select non-contiguous values:
gapminder[c(3, 8, 37, 56), c(1, 4, 5)]
## country continent lifeExp
## 3 Afghanistan Asia 31.997
## 8 Afghanistan Asia 40.822
## 37 Angola Africa 30.015
## 56 Argentina Americas 70.774
We also don’t have to provide a slice for either the rows or the columns. If we don’t include a slice for the rows, R returns all the rows; if we don’t include a slice for the columns, R returns all the columns. If we don’t provide a slice for either rows or columns, e.g. gapminder[, ]
, R returns the full data frame.
# All columns from row 5
gapminder[5, ]
## country year pop continent lifeExp gdpPercap
## 5 Afghanistan 1972 13079460 Asia 36.088 739.9811
## [1] 779.4453 820.8530 853.1007 836.1971 739.9811
## [6] 786.1134 978.0114 852.3959 649.3414 635.3414
## [11] 726.7341 974.5803 1601.0561 1942.2842 2312.8890
## [16] 2760.1969 3313.4222 3533.0039 3630.8807 3738.9327
## [21] 2497.4379 3193.0546 4604.2117 5937.0295 2449.0082
## [26] 3013.9760 2550.8169 3246.9918 4182.6638 4910.4168
## [31] 5745.1602 5681.3585 5023.2166 4797.2951 5288.0404
## [36] 6223.3675 3520.6103 3827.9405 4269.2767 5522.7764
## [41] 5473.2880 3008.6474 2756.9537 2430.2083 2627.8457
## [46] 2277.1409 2773.2873 4797.2313 5911.3151 6856.8562
## [51] 7133.1660 8052.9530 9443.0385 10079.0267 8997.8974
## [56] 9139.6714 9308.4187 10967.2820 8797.6407 12779.3796
## [61] 10039.5956 10949.6496 12217.2269 14526.1246 16788.6295
## [66] 18334.1975 19477.0093 21888.8890 23424.7668 26997.9366
## [71] 30687.7547 34435.3674 6137.0765 8842.5980 10750.7211
## [76] 12834.6024 16661.6256 19749.4223 21597.0836 23687.8261
## [81] 27042.0187 29095.9207 32417.6077 36126.4927 9867.0848
## [86] 11635.7995 12753.2751 14804.6727 18268.6584 19340.1020
## [91] 19211.1473 18524.0241 19035.5792 20292.0168 23403.5593
## [96] 29796.0483 684.2442 661.6375 686.3416 721.1861
## [101] 630.2336 659.8772 676.9819 751.9794 837.8102
## [106] 972.7700 1136.3904 1391.2538 8343.1051 9714.9606
## [111] 10991.2068 13149.0412 16672.1436 19117.9745 20979.8459
## [116] 22525.5631 25575.5707 27561.1966 30485.8838 33692.6051
## [121] 1062.7522 959.6011 949.4991 1035.8314 1085.7969
## [126] 1029.1613 1277.8976 1225.8560 1191.2077 1232.9753
## [131] 1372.8779 1441.2849 2677.3263 2127.6863 2180.9725
## [136] 2586.8861 2980.3313 3548.0978 3156.5105 2753.6915
## [141] 2961.6997 3326.1432 3413.2627 3822.1371 973.5332
## [146] 1353.9892 1709.6837 2172.3524 2860.1698 3528.4813
## [151] 4126.6132 4314.1148 2546.7814 4766.3559 6018.9752
## [156] 7446.2988 851.2411 918.2325 983.6540 1214.7093
## [161] 2263.6111 3214.8578 4551.1421 6205.8839 7954.1116
## [166] 8647.1423 11003.6051 12569.8518 2108.9444 2487.3660
## [171] 3336.5858 3429.8644 4985.7115 6660.1187 7030.8359
## [176] 7807.0958 6950.2830 7957.9808 8131.2128 9065.8008
## [181] 2444.2866 3008.6707 4254.3378 5577.0028 6597.4944
## [186] 7612.2404 8224.1916 8239.8548 6302.6234 5970.3888
## [191] 7696.7777 10680.7928 543.2552 617.1835 722.5120
## [196] 794.8266 854.7360 743.3870 807.1986 912.0631
## [201] 931.7528 946.2950 1037.6452 1217.0330 339.2965
## [206] 379.5646 355.2032 412.9775 464.0995 556.1033
## [211] 559.6032 621.8188 631.6999 463.1151 446.4035
## [216] 430.0707 368.4693 434.0383 496.9136 523.4323
## [221] 421.6240 524.9722 624.4755 683.8956 682.3032
## [226] 734.2852 896.2260 1713.7787 1172.6677 1313.0481
## [231] 1399.6074 1508.4531 1684.1465 1783.4329 2367.9833
## [236] 2602.6642 1793.1633 1694.3375 1934.0114 2042.0952
## [241] 11367.1611 12489.9501 13462.4855 16076.5880 18970.5709
## [246] 22090.8831 22898.7921 26626.5150 26342.8843 28954.9259
## [251] 33328.9651 36319.2350 1071.3107 1190.8443 1193.0688
## [256] 1136.0566 1070.0133 1109.3743 956.7530 844.8764
## [261] 747.9055 740.5063 738.6906 706.0165 1178.6659
## [266] 1308.4956 1389.8176 1196.8106 1104.1040 1133.9850
## [271] 797.9081 952.3861 1058.0643 1004.9614 1156.1819
## [276] 1704.0637 3939.9788 4315.6227 4519.0943 5106.6543
## [281] 5494.0244 4756.7638 5095.6657 5547.0638 7596.1260
## [286] 10118.0532 10778.7838 13171.6388 400.4486 575.9870
## [291] 487.6740 612.7057 676.9001 741.2375 962.4214
## [296] 1378.9040 1655.7842 2289.2341 3119.2809 4959.1149
## [301] 2144.1151 2323.8056 2492.3511 2678.7298 3264.6600
## [306] 3815.8079 4397.5757 4903.2191 5444.6486 6117.3617
## [311] 5755.2600 7006.5804 1102.9909 1211.1485 1406.6483
## [316] 1876.0296 1937.5777 1172.6030 1267.1001 1315.9808
## [321] 1246.9074 1173.6182 1075.8116 986.1479 780.5423
## [326] 905.8602 896.3146 861.5932 904.8961 795.7573
## [331] 673.7478 672.7748 457.7192 312.1884 241.1659
## [336] 277.5519 2125.6214 2315.0566 2464.7832 2677.9396
## [341] 3213.1527 3259.1790 4879.5075 4201.1949 4016.2395
## [346] 3484.1644 3484.0620 3632.5578 2627.0095 2990.0108
## [351] 3460.9370 4161.7278 5118.1469 5926.8770 5262.7348
## [356] 5629.9153 6160.4163 6677.0453 7723.4472 9645.0614
## [361] 1388.5947 1500.8959 1728.8694 2052.0505 2378.2011
## [366] 2517.7365 2602.7102 2156.9561 1648.0738 1786.2654
## [371] 1648.8008 1544.7501 3119.2365 4338.2316 5477.8900
## [376] 6960.2979 9164.0901 11305.3852 13221.8218 13822.5839
## [381] 8447.7949 9875.6045 11628.3890 14619.2227 5586.5388
## [386] 6092.1744 5180.7559 5690.2680 5305.4453 6380.4950
## [391] 7316.9181 7532.9248 5592.8440 5431.9904 6340.6467
## [396] 8948.1029 6876.1403 8256.3439 10136.8671 11399.4449
## [401] 13108.4536 14800.1606 15377.2285 16310.4434 14297.0212
## [406] 16048.5142 17596.2102 22833.3085 9692.3852 11099.6593
## [411] 13583.3135 15937.2112 18866.2072 20422.9015 21688.0405
## [416] 25116.1758 26406.7399 29804.3457 32166.5001 35278.4187
## [421] 2669.5295 2864.9691 3020.9893 3020.0505 3694.2124
## [426] 3081.7610 2879.4681 2880.1026 2377.1562 1895.0170
## [431] 1908.2609 2082.4816 1397.7171 1544.4030 1662.1374
## [436] 1653.7230 2189.8745 2681.9889 2861.0924 2899.8422
## [441] 3044.2142 3614.1013 4563.8082 6025.3748 3522.1107
## [446] 3780.5467 4086.1141 4579.0742 5280.9947 6679.6233
## [451] 7213.7913 6481.7770 7103.7026 7429.4559 5773.0445
## [456] 6873.2623 1418.8224 1458.9153 1693.3359 1814.8807
## [461] 2024.0081 2785.4936 3503.7296 3885.4607 3794.7552
## [466] 4173.1818 4754.6044 5581.1810 3048.3029 3421.5232
## [471] 3776.8036 4358.5954 4520.2460 5138.9224 4098.3442
## [476] 4140.4421 4444.2317 5154.8255 5351.5687 5728.3535
## [481] 375.6431 426.0964 582.8420 915.5960 672.4123
## [486] 958.5668 927.8253 966.8968 1132.0550 2814.4808
## [491] 7703.4959 12154.0897 328.9406 344.1619 380.9958
## [496] 468.7950 514.3242 505.7538 524.8758 521.1341
## [501] 582.8585 913.4708 765.3500 641.3695 362.1463
## [506] 378.9042 419.4564 516.1186 566.2439 556.8084
## [511] 577.8607 573.7413 421.3535 515.8894 530.0535
## [516] 690.8056 6424.5191 7545.4154 9371.8426 10921.6363
## [521] 14358.8759 15605.4228 18533.1576 21141.0122 20647.1650
## [526] 23723.9502 28204.5906 33207.0844 7029.8093 8662.8349
## [531] 10560.4855 12999.9177 16107.1917 18292.6351 20293.8975
## [536] 22066.4421 24703.7961 25889.7849 28926.0323 30470.0167
## [541] 4293.4765 4976.1981 6631.4592 8358.7620 11401.9484
## [546] 21745.5733 15113.3619 11864.4084 13522.1575 14722.8419
## [551] 12521.7139 13206.4845 485.2307 520.9267 599.6503
## [556] 734.7829 756.0868 884.7553 835.8096 611.6589
## [561] 665.6244 653.7302 660.5856 752.7497 7144.1144
## [566] 10187.8267 12902.4629 14745.6256 18016.1803 20512.9212
## [571] 22031.5327 24639.1857 26505.3032 27788.8842 30035.8020
## [576] 32170.3744 911.2989 1043.5615 1190.0411 1125.6972
## [581] 1178.2237 993.2240 876.0326 847.0061 925.0602
## [586] 1005.2458 1111.9846 1327.6089 3530.6901 4916.2999
## [591] 6017.1907 8513.0970 12724.8296 14195.5243 15268.4209
## [596] 16120.5284 17541.4963 18747.6981 22514.2548 27538.4119
## [601] 2428.2378 2617.1560 2750.3644 3242.5311 4031.4083
## [606] 4879.9927 4820.4948 4246.4860 4439.4508 4684.3138
## [611] 4858.3475 5186.0500 510.1965 576.2670 686.3737
## [616] 708.7595 741.6662 874.6859 857.2504 805.5725
## [621] 794.3484 869.4498 945.5836 942.6542 299.8503
## [626] 431.7905 522.0344 715.5806 820.2246 764.7260
## [631] 838.1240 736.4154 745.5399 796.6645 575.7047
## [636] 579.2317 1840.3669 1726.8879 1796.5890 1452.0577
## [641] 1654.4569 1874.2989 2011.1595 1823.0160 1456.3095
## [646] 1341.7269 1270.3649 1201.6372 2194.9262 2220.4877
## [651] 2291.1568 2538.2694 2529.8423 3203.2081 3121.7608
## [656] 3023.0967 3081.6946 3160.4549 3099.7287 3548.3308
## [661] 3054.4212 3629.0765 4692.6483 6197.9628 8315.9281
## [666] 11186.1413 14560.5305 20038.4727 24757.6030 28377.6322
## [671] 30209.0152 39724.9787 5263.6738 6040.1800 7550.3599
## [676] 9326.6447 10168.6561 11674.8374 12545.9907 12986.4800
## [681] 10535.6285 11712.7768 14843.9356 18008.9444 7267.6884
## [686] 9244.0014 10350.1591 13319.8957 15798.0636 19654.9625
## [691] 23269.6075 26923.2063 25144.3920 28061.0997 31163.2020
## [696] 36180.7892 546.5657 590.0620 658.3472 700.7706
## [701] 724.0325 813.3373 855.7235 976.5127 1164.4068
## [706] 1458.8174 1746.7695 2452.2104 749.6817 858.9003
## [711] 849.2898 762.4318 1111.1079 1382.7021 1516.8730
## [716] 1748.3570 2383.1409 3119.3356 2873.9129 3540.6516
## [721] 3035.3260 3290.2576 4187.3298 5906.7318 9613.8186
## [726] 11888.5951 7608.3346 6642.8814 7235.6532 8263.5903
## [731] 9240.7620 11605.7145 4129.7661 6229.3336 8341.7378
## [736] 8931.4598 9576.0376 14688.2351 14517.9071 11643.5727
## [741] 3745.6407 3076.2398 4390.7173 4471.0619 5210.2803
## [746] 5599.0779 6631.5973 7655.5690 9530.7729 11150.9811
## [751] 12618.3214 13872.8665 17558.8155 24521.9471 34077.0494
## [756] 40675.9964 4086.5221 5385.2785 7105.6307 8393.7414
## [761] 12786.9322 13306.6192 15367.0292 17122.4799 18051.5225
## [766] 20896.6092 21905.5951 25523.2771 4931.4042 6248.6562
## [771] 8243.5823 10022.4013 12269.2738 14255.9847 16537.4835
## [776] 19207.2348 22013.6449 24675.0245 27968.0982 28569.7197
## [781] 2898.5309 4756.5258 5246.1075 6124.7035 7433.8893
## [786] 6650.1956 6068.0513 6351.2375 7404.9237 7121.9247
## [791] 6994.7749 7320.8803 3216.9563 4317.6944 6576.6495
## [796] 9847.7886 14778.7864 16610.3770 19384.1057 22375.9419
## [801] 26824.8951 28816.5850 28604.5919 31656.0681 1546.9078
## [806] 1886.0806 2348.0092 2741.7963 2110.8563 2852.3516
## [811] 4161.4160 4448.6799 3431.5936 3645.3796 3844.9172
## [816] 4519.4612 853.5409 944.4383 896.9664 1056.7365
## [821] 1222.3600 1267.6132 1348.2258 1361.9369 1341.9217
## [826] 1360.4850 1287.5147 1463.2493 1088.2778 1571.1347
## [831] 1621.6936 2143.5406 3701.6215 4106.3012 4106.5253
## [836] 4106.4923 3726.0635 1690.7568 1646.7582 1593.0655
## [841] 1030.5922 1487.5935 1536.3444 2029.2281 3030.8767
## [846] 4657.2210 5622.9425 8533.0888 12104.2787 15993.5280
## [851] 19233.9882 23348.1397 108382.3529 113523.1329 95458.1118
## [856] 80894.8833 109347.8670 59265.4771 31354.0357 28118.4300
## [861] 34932.9196 40300.6200 35110.1057 47306.9898 4834.8041
## [866] 6089.7869 5714.5606 6006.9830 7486.3843 8659.6968
## [871] 7640.5195 5377.0913 6890.8069 8754.9639 9313.9388
## [876] 10461.0587 298.8462 335.9971 411.8006 498.6390
## [881] 496.5816 745.3695 797.2631 773.9932 977.4863
## [886] 1186.1480 1275.1846 1569.3314 575.5730 620.9700
## [891] 634.1952 713.6036 803.0055 640.3224 572.1996
## [896] 506.1139 636.6229 609.1740 531.4824 414.5073
## [901] 2387.5481 3448.2844 6757.0308 18772.7517 21011.4972
## [906] 21951.2118 17364.2754 11770.5898 9640.1385 9467.4461
## [911] 9534.6775 12057.4993 1443.0117 1589.2027 1643.3871
## [916] 1634.0473 1748.5630 1544.2286 1302.8787 1155.4419
## [921] 1040.6762 986.2959 894.6371 1044.7701 369.1651
## [926] 416.3698 427.9011 495.5148 584.6220 663.2237
## [931] 632.8039 635.5174 563.2000 692.2758 665.4231
## [936] 759.3499 1831.1329 1810.0670 2036.8849 2277.7424
## [941] 2849.0948 3827.9216 4920.3560 5249.8027 7277.9128
## [946] 10132.9096 10206.9779 12451.6558 452.3370 490.3822
## [951] 496.1743 545.0099 581.3689 686.3953 618.0141
## [956] 684.1716 739.0144 790.2580 951.4098 1042.5816
## [961] 743.1159 846.1203 1055.8960 1421.1452 1586.8518
## [966] 1497.4922 1481.1502 1421.6036 1361.3698 1483.1361
## [971] 1579.0195 1803.1515 1967.9557 2034.0380 2529.0675
## [976] 2475.3876 2575.4842 3710.9830 3688.0377 4783.5869
## [981] 6058.2538 7425.7053 9021.8159 10956.9911 3478.1255
## [986] 4131.5466 4581.6094 5754.7339 6809.4067 7674.9291
## [991] 9611.1475 8688.1560 9472.3843 9767.2975 10742.4405
## [996] 11977.5750 786.5669 912.6626 1056.3540 1226.0411
## [1001] 1421.7420 1647.5117 2000.6031 2338.0083 1785.4020
## [1006] 1902.2521 2140.7393 3095.7723 2647.5856 3682.2599
## [1011] 4649.5938 5907.8509 7778.4140 9595.9299 11222.5876
## [1016] 11732.5102 7003.3390 6465.6133 6557.1943 9253.8961
## [1021] 1688.2036 1642.0023 1566.3535 1711.0448 1930.1950
## [1026] 2370.6200 2702.6204 2755.0470 2948.0473 2982.1019
## [1031] 3258.4956 3820.1752 468.5260 495.5868 556.6864
## [1036] 566.6692 724.9178 502.3197 462.2114 389.8762
## [1041] 410.8968 472.3461 633.6179 823.6856 331.0000
## [1046] 350.0000 388.0000 349.0000 357.0000 371.0000
## [1051] 424.0000 385.0000 347.0000 415.0000 611.0000
## [1056] 944.0000 2423.7804 2621.4481 3173.2156 3793.6948
## [1061] 3746.0809 3876.4860 4191.1005 3693.7313 3804.5380
## [1066] 3899.5243 4072.3248 4811.0604 545.8657 597.9364
## [1071] 652.3969 676.4422 674.7881 694.1124 718.3731
## [1076] 775.6325 897.7404 1010.8921 1057.2063 1091.3598
## [1081] 8941.5719 11276.1934 12790.8496 15363.2514 18794.7457
## [1086] 21209.0592 21399.4605 23651.3236 26790.9496 30246.1306
## [1091] 33724.7578 36797.9333 10556.5757 12247.3953 13175.6780
## [1096] 14463.9189 16046.0373 16233.7177 17632.4104 19007.1913
## [1101] 18363.3249 21050.4138 23189.8014 25185.0091 3112.3639
## [1106] 3457.4159 3634.3644 4643.3935 4688.5933 5486.3711
## [1111] 3470.3382 2955.9844 2170.1517 2253.0230 2474.5488
## [1116] 2749.3210 761.8794 835.5234 997.7661 1054.3849
## [1121] 954.2092 808.8971 909.7221 668.3000 581.1827
## [1126] 580.3052 601.0745 619.6769 1077.2819 1100.5926
## [1131] 1150.9275 1014.5141 1698.3888 1981.9518 1576.9738
## [1136] 1385.0296 1619.8482 1624.9413 1615.2864 2013.9773
## [1141] 10095.4217 11653.9730 13450.4015 16361.8765 18965.0555
## [1146] 23311.3494 26298.6353 31540.9748 33965.6611 41283.1643
## [1151] 44683.9753 49357.1902 1828.2303 2242.7466 2924.6381
## [1156] 4720.9427 10618.0385 11848.3439 12954.7910 18115.2231
## [1161] 18616.7069 19702.0558 19774.8369 22316.1929 684.5971
## [1166] 747.0835 803.3427 942.4083 1049.9390 1175.9212
## [1171] 1443.4298 1704.6866 1971.8295 2049.3505 2092.7124
## [1176] 2605.9476 2480.3803 2961.8009 3536.5403 4421.0091
## [1181] 5364.2497 5351.9121 7009.6016 7034.7792 6618.7431
## [1186] 7113.6923 7356.0319 9809.1856 1952.3087 2046.1547
## [1191] 2148.0271 2299.3763 2523.3380 3248.3733 4258.5036
## [1196] 3998.8757 4196.4111 4247.4003 3783.6742 4172.8385
## [1201] 3758.5234 4245.2567 4957.0380 5788.0933 5937.8273
## [1206] 6281.2909 6434.5018 6360.9434 4446.3809 5838.3477
## [1211] 5909.0201 7408.9056 1272.8810 1547.9448 1649.5522
## [1216] 1814.1274 1989.3741 2373.2043 2603.2738 2189.6350
## [1221] 2279.3240 2536.5349 2650.9211 3190.4810 4029.3297
## [1226] 4734.2530 5338.7521 6557.1528 8006.5070 9508.1415
## [1231] 8451.5310 9082.3512 7738.8812 10159.5837 12002.2391
## [1236] 15389.9247 3068.3199 3774.5717 4727.9549 6361.5180
## [1241] 9022.2474 10172.4857 11753.8429 13039.3088 16207.2666
## [1246] 17641.0316 19970.9079 20509.6478 3081.9598 3907.1562
## [1251] 5108.3446 6929.2777 9123.0417 9770.5249 10330.9891
## [1256] 12281.3419 14641.5871 16999.4333 18855.6062 19328.7090
## [1261] 2718.8853 2769.4518 3173.7233 4021.1757 5047.6586
## [1266] 4319.8041 5267.2194 5303.3775 6101.2558 6071.9414
## [1271] 6316.1652 7670.1226 3144.6132 3943.3702 4734.9976
## [1276] 6470.8665 8011.4144 9356.3972 9605.3141 9696.2733
## [1281] 6598.4099 7346.5476 7885.3601 10808.4756 493.3239
## [1286] 540.2894 597.4731 510.9637 590.5807 670.0806
## [1291] 881.5706 847.9912 737.0686 589.9445 785.6538
## [1296] 863.0885 879.5836 860.7369 1071.5511 1384.8406
## [1301] 1532.9853 1737.5617 1890.2181 1516.5255 1428.7778
## [1306] 1339.0760 1353.0924 1598.4351 6459.5548 8157.5912
## [1311] 11626.4197 16903.0489 24837.4287 34167.7626 33693.1753
## [1316] 21198.2614 24841.6178 20586.6902 19014.5412 21654.8319
## [1321] 1450.3570 1567.6530 1654.9887 1612.4046 1597.7121
## [1326] 1561.7691 1518.4800 1441.7207 1367.8994 1392.3683
## [1331] 1519.6353 1712.4721 3581.4594 4981.0909 6289.6292
## [1336] 7991.7071 10522.0675 12980.6696 15181.0927 15870.8785
## [1341] 9325.0682 7914.3203 7236.0753 9786.5347 879.7877
## [1346] 1004.4844 1116.6399 1206.0435 1353.7598 1348.2852
## [1351] 1465.0108 1294.4478 1068.6963 574.6482 699.4897
## [1356] 862.5408 2315.1382 2843.1044 3674.7356 4977.4185
## [1361] 8597.7562 11210.0895 15169.1611 18861.5308 24769.8912
## [1366] 33519.4766 36023.1054 47143.1796 5074.6591 6093.2630
## [1371] 7481.1076 8412.9024 9674.1676 10922.6640 11348.5459
## [1376] 12037.2676 9498.4677 12126.2306 13638.7784 18678.3144
## [1381] 4215.0417 5862.2766 7402.3034 9405.4894 12383.4862
## [1386] 15277.0302 17866.7218 18678.5349 14214.7168 17161.1073
## [1391] 20660.0194 25768.2576 1135.7498 1258.1474 1369.4883
## [1396] 1284.7332 1254.5761 1450.9925 1176.8070 1093.2450
## [1401] 926.9603 930.5964 882.0818 926.1411 4725.2955
## [1406] 5487.1042 5768.7297 7114.4780 7765.9626 8028.6514
## [1411] 8568.2662 7825.8234 7225.0693 7479.1882 7710.9464
## [1416] 9269.6578 3834.0347 4564.8024 5693.8439 7993.5123
## [1421] 10638.7513 13236.9212 13926.1700 15764.9831 18603.0645
## [1426] 20445.2990 24835.4717 28821.0637 1083.5320 1072.5466
## [1431] 1074.4720 1135.5143 1213.3955 1348.7757 1648.0798
## [1436] 1876.7668 2153.7392 2664.4773 3015.3788 3970.0954
## [1441] 1615.9911 1770.3371 1959.5938 1687.9976 1659.6528
## [1446] 2202.9884 1895.5441 1507.8192 1492.1970 1632.2108
## [1451] 1993.3983 2602.3950 1148.3766 1244.7084 1856.1821
## [1456] 2613.1017 3364.8366 3781.4106 3895.3840 3984.8398
## [1461] 3553.0224 3876.7685 4128.1169 4513.4806 8527.8447
## [1466] 9911.8782 12329.4419 15258.2970 17832.0246 18855.7252
## [1471] 20667.3812 23586.9293 23880.0168 25266.5950 29341.6309
## [1476] 33859.7484 14734.2327 17909.4897 20431.0927 22966.1443
## [1481] 27195.1130 26982.2905 28397.7151 30281.7046 31871.5303
## [1486] 32135.3230 34480.9577 37506.4191 1643.4854 2117.2349
## [1491] 2193.0371 1881.9236 2571.4230 3195.4846 3761.8377
## [1496] 3116.7743 3340.5428 4014.2390 4090.9253 4184.5481
## [1501] 1206.9479 1507.8613 1822.8790 2643.8587 4062.5239
## [1506] 5596.5198 7426.3548 11054.5618 15215.6579 20206.8210
## [1511] 23235.4233 28718.2768 716.6501 698.5356 722.0038
## [1516] 848.2187 915.9851 962.4923 874.2426 831.8221
## [1521] 825.6825 789.1862 899.0742 1107.4822 757.7974
## [1526] 793.5774 1002.1992 1295.4607 1524.3589 1961.2246
## [1531] 2393.2198 2982.6538 4616.8965 5852.6255 5913.1875
## [1536] 7458.3963 859.8087 925.9083 1067.5348 1477.5968
## [1541] 1649.6602 1532.7770 1344.5780 1202.2014 1034.2989
## [1546] 982.2869 886.2206 882.9699 3023.2719 4100.3934
## [1551] 4997.5240 5621.3685 6619.5514 7899.5542 9119.5286
## [1556] 7388.5978 7370.9909 8792.5731 11460.6002 18008.5092
## [1561] 1468.4756 1395.2325 1660.3032 1932.3602 2753.2860
## [1566] 3120.8768 3560.2332 3810.4193 4332.7202 4876.7986
## [1571] 5722.8957 7092.9230 1969.1010 2218.7543 2322.8699
## [1576] 2826.3564 3450.6964 4269.1223 4241.3563 5089.0437
## [1581] 5678.3483 6601.4299 6508.0857 8458.2764 734.7535
## [1586] 774.3711 767.2717 908.9185 950.7359 843.7331
## [1591] 682.2662 617.7244 644.1708 816.5591 927.7210
## [1596] 1056.3801 9979.5085 11283.1779 12477.1771 14142.8509
## [1601] 15895.1164 17428.7485 18232.4245 21664.7877 22705.0925
## [1606] 26074.5314 29478.9992 33203.2613 13990.4821 14847.1271
## [1611] 16173.1459 19530.3656 21806.0359 24072.6321 25009.5591
## [1616] 29884.3504 32003.9322 35767.4330 39097.0995 42951.6531
## [1621] 5716.7667 6150.7730 5603.3577 5444.6196 5703.4089
## [1626] 6504.3397 6920.2231 7452.3990 8137.0048 9230.2407
## [1631] 7727.0020 10611.4630 7689.7998 9802.4665 8422.9742
## [1636] 9541.4742 10505.2597 13143.9510 11152.4101 9883.5846
## [1641] 10733.9263 10165.4952 8605.0478 11415.8057 605.0665
## [1646] 676.2854 772.0492 637.1233 699.5016 713.5371
## [1651] 707.2358 820.7994 989.0231 1385.8968 1764.4567
## [1656] 2441.5764 1515.5923 1827.0677 2198.9563 2649.7150
## [1661] 3133.4093 3682.8315 4336.0321 5107.1974 6017.6548
## [1666] 7110.6676 4515.4876 3025.3498 781.7176 804.8305
## [1671] 825.6232 862.4421 1265.0470 1829.7652 1977.5570
## [1676] 1971.7415 1879.4967 2117.4845 2234.8208 2280.7699
## [1681] 1147.3888 1311.9568 1452.7258 1777.0773 1773.4983
## [1686] 1588.6883 1408.6786 1213.3151 1210.8846 1071.3538
## [1691] 1071.6139 1271.2116 406.8841 518.7643 527.2722
## [1696] 569.7951 799.3622 685.5877 788.8550 706.1573
## [1701] 693.4208 792.4500 672.0386 469.7093
We can also calculate some information from our data. For example, the maximum and minimum of a list are as follows.
max(gapminder[,6])
## [1] 113523.1
min(gapminder[,6])
## [1] 241.1659
Now that we have our data in R, let’s do something with it.
We have the GDP per capita as well as the population of the country. Let’s calculate the GDP and add that to our data frame. First, we multiply gdpPercap by pop and save that to a variable. Multiplying two columns automatically multiplies each pair of values and makes a vector containing each result.
gdp<-gapminder$gdpPercap * gapminder$pop
gdp is a vector. A vector is the most common and basic data structure in R
. It can only contain one data type. They are the building blocks of every other data structure.
A vector can contain any of five types:
TRUE
, FALSE
)as.integer(3)
)2
, 2.0
, pi
)1 + 0i
, 1 + 4i
)"a"
, "swc"
)Now we can add the vector gdp to the gapminder data frame using cbind
which stands for “column bind”, as in bind the vector gdp to the gapminder data frame.
gapminder <- cbind(gapminder, gdp)
We can check that our results are correct by looking at the first item in gdp and comparing it to the expected result.
gdp[1]
## [1] 6567086330
gapminder$gdp[1]
## [1] 6567086330
gapminder$gdpPercap[1]*gapminder$pop[1]
## [1] 6567086330
We can also view the first few rows of the data again.
head(gapminder)
## country year pop continent lifeExp gdpPercap gdp
## 1 Afghanistan 1952 8425333 Asia 28.801 779.4453 6567086330
## 2 Afghanistan 1957 9240934 Asia 30.332 820.8530 7585448670
## 3 Afghanistan 1962 10267083 Asia 31.997 853.1007 8758855797
## 4 Afghanistan 1967 11537966 Asia 34.020 836.1971 9648014150
## 5 Afghanistan 1972 13079460 Asia 36.088 739.9811 9678553274
## 6 Afghanistan 1977 14880372 Asia 38.438 786.1134 11697659231
Make a new column in the gapminder
data frame that contains population in units of millions of people.
Check the head or tail of the data frame to make sure it worked.
head(gapminder)
## country year pop continent lifeExp gdpPercap gdp
## 1 Afghanistan 1952 8425333 Asia 28.801 779.4453 6567086330
## 2 Afghanistan 1957 9240934 Asia 30.332 820.8530 7585448670
## 3 Afghanistan 1962 10267083 Asia 31.997 853.1007 8758855797
## 4 Afghanistan 1967 11537966 Asia 34.020 836.1971 9648014150
## 5 Afghanistan 1972 13079460 Asia 36.088 739.9811 9678553274
## 6 Afghanistan 1977 14880372 Asia 38.438 786.1134 11697659231
## pop_mill
## 1 8.425333
## 2 9.240934
## 3 10.267083
## 4 11.537966
## 5 13.079460
## 6 14.880372
Now let’s use our data to answer a biological question. For example:
We won’t go into too much detail, but briefly:
lm
estimates linear statistical modelsa ~ b
meaning that a
, the dependent (or response) variable, is a function of b
, the independent variable.lm
to use the gapminder data frame, so it knows where to find the variables lifeExp
and year
.reg <- lm(lifeExp ~ year, data=gapminder)
Let’s look at the output:
reg
##
## Call:
## lm(formula = lifeExp ~ year, data = gapminder)
##
## Coefficients:
## (Intercept) year
## -585.6522 0.3259
Not much there right? But if we look at the structure…
str(reg)
## List of 12
## $ coefficients : Named num [1:2] -585.652 0.326
## ..- attr(*, "names")= chr [1:2] "(Intercept)" "year"
## $ residuals : Named num [1:1704] -21.7 -21.8 -21.8 -21.4 -20.9 ...
## ..- attr(*, "names")= chr [1:1704] "1" "2" "3" "4" ...
## $ effects : Named num [1:1704] -2455.1 232.2 -20.8 -20.5 -20.2 ...
## ..- attr(*, "names")= chr [1:1704] "(Intercept)" "year" "" "" ...
## $ rank : int 2
## $ fitted.values: Named num [1:1704] 50.5 52.1 53.8 55.4 57 ...
## ..- attr(*, "names")= chr [1:1704] "1" "2" "3" "4" ...
## $ assign : int [1:2] 0 1
## $ qr :List of 5
## ..$ qr : num [1:1704, 1:2] -41.2795 0.0242 0.0242 0.0242 0.0242 ...
## .. ..- attr(*, "dimnames")=List of 2
## .. .. ..$ : chr [1:1704] "1" "2" "3" "4" ...
## .. .. ..$ : chr [1:2] "(Intercept)" "year"
## .. ..- attr(*, "assign")= int [1:2] 0 1
## ..$ qraux: num [1:2] 1.02 1.03
## ..$ pivot: int [1:2] 1 2
## ..$ tol : num 1e-07
## ..$ rank : int 2
## ..- attr(*, "class")= chr "qr"
## $ df.residual : int 1702
## $ xlevels : Named list()
## $ call : language lm(formula = lifeExp ~ year, data = gapminder)
## $ terms :Classes 'terms', 'formula' language lifeExp ~ year
## .. ..- attr(*, "variables")= language list(lifeExp, year)
## .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. ..$ : chr [1:2] "lifeExp" "year"
## .. .. .. ..$ : chr "year"
## .. ..- attr(*, "term.labels")= chr "year"
## .. ..- attr(*, "order")= int 1
## .. ..- attr(*, "intercept")= int 1
## .. ..- attr(*, "response")= int 1
## .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. ..- attr(*, "predvars")= language list(lifeExp, year)
## .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. ..- attr(*, "names")= chr [1:2] "lifeExp" "year"
## $ model :'data.frame': 1704 obs. of 2 variables:
## ..$ lifeExp: num [1:1704] 28.8 30.3 32 34 36.1 ...
## ..$ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## ..- attr(*, "terms")=Classes 'terms', 'formula' language lifeExp ~ year
## .. .. ..- attr(*, "variables")= language list(lifeExp, year)
## .. .. ..- attr(*, "factors")= int [1:2, 1] 0 1
## .. .. .. ..- attr(*, "dimnames")=List of 2
## .. .. .. .. ..$ : chr [1:2] "lifeExp" "year"
## .. .. .. .. ..$ : chr "year"
## .. .. ..- attr(*, "term.labels")= chr "year"
## .. .. ..- attr(*, "order")= int 1
## .. .. ..- attr(*, "intercept")= int 1
## .. .. ..- attr(*, "response")= int 1
## .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
## .. .. ..- attr(*, "predvars")= language list(lifeExp, year)
## .. .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "numeric"
## .. .. .. ..- attr(*, "names")= chr [1:2] "lifeExp" "year"
## - attr(*, "class")= chr "lm"
There’s a great deal stored in nested lists! The structure function allows you to see all the data available, in this case, the data that was returned by the lm
function.
For now, we can look at the summary
:
summary(reg)
##
## Call:
## lm(formula = lifeExp ~ year, data = gapminder)
##
## Residuals:
## Min 1Q Median 3Q Max
## -39.949 -9.651 1.697 10.335 22.158
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -585.65219 32.31396 -18.12 <2e-16 ***
## year 0.32590 0.01632 19.96 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 11.63 on 1702 degrees of freedom
## Multiple R-squared: 0.1898, Adjusted R-squared: 0.1893
## F-statistic: 398.6 on 1 and 1702 DF, p-value: < 2.2e-16
As you might expect, life expectancy has slowly been increasing over time, so we see a significant positive association!