Carga de paquetes

#install.packages("gapminder")  # para cargar la base de datos a usar hoy
#install.packages("tidyverse")
library(gapminder)  # datos
library(dplyr)      # Paquete para manipular los datos
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Recuerde: Filas=individuos y Columnas=variables

Nombres de las variables

colnames(gapminder)
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"

Dimensión de la base de datos

dim(gapminder)  # 
## [1] 1704    6

1704 filas y 6 columnas

Información sobre las variables

str(gapminder)  #
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
##  $ country  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year     : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ lifeExp  : num [1:1704] 28.8 30.3 32 34 36.1 ...
##  $ pop      : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##  $ gdpPercap: num [1:1704] 779 821 853 836 740 ...

gapminder es el nombre de la base de datos

LLamar una variable

gapminder$country
##    [1] Afghanistan              Afghanistan             
##    [3] Afghanistan              Afghanistan             
##    [5] Afghanistan              Afghanistan             
##    [7] Afghanistan              Afghanistan             
##    [9] Afghanistan              Afghanistan             
##   [11] Afghanistan              Afghanistan             
##   [13] Albania                  Albania                 
##   [15] Albania                  Albania                 
##   [17] Albania                  Albania                 
##   [19] Albania                  Albania                 
##   [21] Albania                  Albania                 
##   [23] Albania                  Albania                 
##   [25] Algeria                  Algeria                 
##   [27] Algeria                  Algeria                 
##   [29] Algeria                  Algeria                 
##   [31] Algeria                  Algeria                 
##   [33] Algeria                  Algeria                 
##   [35] Algeria                  Algeria                 
##   [37] Angola                   Angola                  
##   [39] Angola                   Angola                  
##   [41] Angola                   Angola                  
##   [43] Angola                   Angola                  
##   [45] Angola                   Angola                  
##   [47] Angola                   Angola                  
##   [49] Argentina                Argentina               
##   [51] Argentina                Argentina               
##   [53] Argentina                Argentina               
##   [55] Argentina                Argentina               
##   [57] Argentina                Argentina               
##   [59] Argentina                Argentina               
##   [61] Australia                Australia               
##   [63] Australia                Australia               
##   [65] Australia                Australia               
##   [67] Australia                Australia               
##   [69] Australia                Australia               
##   [71] Australia                Australia               
##   [73] Austria                  Austria                 
##   [75] Austria                  Austria                 
##   [77] Austria                  Austria                 
##   [79] Austria                  Austria                 
##   [81] Austria                  Austria                 
##   [83] Austria                  Austria                 
##   [85] Bahrain                  Bahrain                 
##   [87] Bahrain                  Bahrain                 
##   [89] Bahrain                  Bahrain                 
##   [91] Bahrain                  Bahrain                 
##   [93] Bahrain                  Bahrain                 
##   [95] Bahrain                  Bahrain                 
##   [97] Bangladesh               Bangladesh              
##   [99] Bangladesh               Bangladesh              
##  [101] Bangladesh               Bangladesh              
##  [103] Bangladesh               Bangladesh              
##  [105] Bangladesh               Bangladesh              
##  [107] Bangladesh               Bangladesh              
##  [109] Belgium                  Belgium                 
##  [111] Belgium                  Belgium                 
##  [113] Belgium                  Belgium                 
##  [115] Belgium                  Belgium                 
##  [117] Belgium                  Belgium                 
##  [119] Belgium                  Belgium                 
##  [121] Benin                    Benin                   
##  [123] Benin                    Benin                   
##  [125] Benin                    Benin                   
##  [127] Benin                    Benin                   
##  [129] Benin                    Benin                   
##  [131] Benin                    Benin                   
##  [133] Bolivia                  Bolivia                 
##  [135] Bolivia                  Bolivia                 
##  [137] Bolivia                  Bolivia                 
##  [139] Bolivia                  Bolivia                 
##  [141] Bolivia                  Bolivia                 
##  [143] Bolivia                  Bolivia                 
##  [145] Bosnia and Herzegovina   Bosnia and Herzegovina  
##  [147] Bosnia and Herzegovina   Bosnia and Herzegovina  
##  [149] Bosnia and Herzegovina   Bosnia and Herzegovina  
##  [151] Bosnia and Herzegovina   Bosnia and Herzegovina  
##  [153] Bosnia and Herzegovina   Bosnia and Herzegovina  
##  [155] Bosnia and Herzegovina   Bosnia and Herzegovina  
##  [157] Botswana                 Botswana                
##  [159] Botswana                 Botswana                
##  [161] Botswana                 Botswana                
##  [163] Botswana                 Botswana                
##  [165] Botswana                 Botswana                
##  [167] Botswana                 Botswana                
##  [169] Brazil                   Brazil                  
##  [171] Brazil                   Brazil                  
##  [173] Brazil                   Brazil                  
##  [175] Brazil                   Brazil                  
##  [177] Brazil                   Brazil                  
##  [179] Brazil                   Brazil                  
##  [181] Bulgaria                 Bulgaria                
##  [183] Bulgaria                 Bulgaria                
##  [185] Bulgaria                 Bulgaria                
##  [187] Bulgaria                 Bulgaria                
##  [189] Bulgaria                 Bulgaria                
##  [191] Bulgaria                 Bulgaria                
##  [193] Burkina Faso             Burkina Faso            
##  [195] Burkina Faso             Burkina Faso            
##  [197] Burkina Faso             Burkina Faso            
##  [199] Burkina Faso             Burkina Faso            
##  [201] Burkina Faso             Burkina Faso            
##  [203] Burkina Faso             Burkina Faso            
##  [205] Burundi                  Burundi                 
##  [207] Burundi                  Burundi                 
##  [209] Burundi                  Burundi                 
##  [211] Burundi                  Burundi                 
##  [213] Burundi                  Burundi                 
##  [215] Burundi                  Burundi                 
##  [217] Cambodia                 Cambodia                
##  [219] Cambodia                 Cambodia                
##  [221] Cambodia                 Cambodia                
##  [223] Cambodia                 Cambodia                
##  [225] Cambodia                 Cambodia                
##  [227] Cambodia                 Cambodia                
##  [229] Cameroon                 Cameroon                
##  [231] Cameroon                 Cameroon                
##  [233] Cameroon                 Cameroon                
##  [235] Cameroon                 Cameroon                
##  [237] Cameroon                 Cameroon                
##  [239] Cameroon                 Cameroon                
##  [241] Canada                   Canada                  
##  [243] Canada                   Canada                  
##  [245] Canada                   Canada                  
##  [247] Canada                   Canada                  
##  [249] Canada                   Canada                  
##  [251] Canada                   Canada                  
##  [253] Central African Republic Central African Republic
##  [255] Central African Republic Central African Republic
##  [257] Central African Republic Central African Republic
##  [259] Central African Republic Central African Republic
##  [261] Central African Republic Central African Republic
##  [263] Central African Republic Central African Republic
##  [265] Chad                     Chad                    
##  [267] Chad                     Chad                    
##  [269] Chad                     Chad                    
##  [271] Chad                     Chad                    
##  [273] Chad                     Chad                    
##  [275] Chad                     Chad                    
##  [277] Chile                    Chile                   
##  [279] Chile                    Chile                   
##  [281] Chile                    Chile                   
##  [283] Chile                    Chile                   
##  [285] Chile                    Chile                   
##  [287] Chile                    Chile                   
##  [289] China                    China                   
##  [291] China                    China                   
##  [293] China                    China                   
##  [295] China                    China                   
##  [297] China                    China                   
##  [299] China                    China                   
##  [301] Colombia                 Colombia                
##  [303] Colombia                 Colombia                
##  [305] Colombia                 Colombia                
##  [307] Colombia                 Colombia                
##  [309] Colombia                 Colombia                
##  [311] Colombia                 Colombia                
##  [313] Comoros                  Comoros                 
##  [315] Comoros                  Comoros                 
##  [317] Comoros                  Comoros                 
##  [319] Comoros                  Comoros                 
##  [321] Comoros                  Comoros                 
##  [323] Comoros                  Comoros                 
##  [325] Congo, Dem. Rep.         Congo, Dem. Rep.        
##  [327] Congo, Dem. Rep.         Congo, Dem. Rep.        
##  [329] Congo, Dem. Rep.         Congo, Dem. Rep.        
##  [331] Congo, Dem. Rep.         Congo, Dem. Rep.        
##  [333] Congo, Dem. Rep.         Congo, Dem. Rep.        
##  [335] Congo, Dem. Rep.         Congo, Dem. Rep.        
##  [337] Congo, Rep.              Congo, Rep.             
##  [339] Congo, Rep.              Congo, Rep.             
##  [341] Congo, Rep.              Congo, Rep.             
##  [343] Congo, Rep.              Congo, Rep.             
##  [345] Congo, Rep.              Congo, Rep.             
##  [347] Congo, Rep.              Congo, Rep.             
##  [349] Costa Rica               Costa Rica              
##  [351] Costa Rica               Costa Rica              
##  [353] Costa Rica               Costa Rica              
##  [355] Costa Rica               Costa Rica              
##  [357] Costa Rica               Costa Rica              
##  [359] Costa Rica               Costa Rica              
##  [361] Cote d'Ivoire            Cote d'Ivoire           
##  [363] Cote d'Ivoire            Cote d'Ivoire           
##  [365] Cote d'Ivoire            Cote d'Ivoire           
##  [367] Cote d'Ivoire            Cote d'Ivoire           
##  [369] Cote d'Ivoire            Cote d'Ivoire           
##  [371] Cote d'Ivoire            Cote d'Ivoire           
##  [373] Croatia                  Croatia                 
##  [375] Croatia                  Croatia                 
##  [377] Croatia                  Croatia                 
##  [379] Croatia                  Croatia                 
##  [381] Croatia                  Croatia                 
##  [383] Croatia                  Croatia                 
##  [385] Cuba                     Cuba                    
##  [387] Cuba                     Cuba                    
##  [389] Cuba                     Cuba                    
##  [391] Cuba                     Cuba                    
##  [393] Cuba                     Cuba                    
##  [395] Cuba                     Cuba                    
##  [397] Czech Republic           Czech Republic          
##  [399] Czech Republic           Czech Republic          
##  [401] Czech Republic           Czech Republic          
##  [403] Czech Republic           Czech Republic          
##  [405] Czech Republic           Czech Republic          
##  [407] Czech Republic           Czech Republic          
##  [409] Denmark                  Denmark                 
##  [411] Denmark                  Denmark                 
##  [413] Denmark                  Denmark                 
##  [415] Denmark                  Denmark                 
##  [417] Denmark                  Denmark                 
##  [419] Denmark                  Denmark                 
##  [421] Djibouti                 Djibouti                
##  [423] Djibouti                 Djibouti                
##  [425] Djibouti                 Djibouti                
##  [427] Djibouti                 Djibouti                
##  [429] Djibouti                 Djibouti                
##  [431] Djibouti                 Djibouti                
##  [433] Dominican Republic       Dominican Republic      
##  [435] Dominican Republic       Dominican Republic      
##  [437] Dominican Republic       Dominican Republic      
##  [439] Dominican Republic       Dominican Republic      
##  [441] Dominican Republic       Dominican Republic      
##  [443] Dominican Republic       Dominican Republic      
##  [445] Ecuador                  Ecuador                 
##  [447] Ecuador                  Ecuador                 
##  [449] Ecuador                  Ecuador                 
##  [451] Ecuador                  Ecuador                 
##  [453] Ecuador                  Ecuador                 
##  [455] Ecuador                  Ecuador                 
##  [457] Egypt                    Egypt                   
##  [459] Egypt                    Egypt                   
##  [461] Egypt                    Egypt                   
##  [463] Egypt                    Egypt                   
##  [465] Egypt                    Egypt                   
##  [467] Egypt                    Egypt                   
##  [469] El Salvador              El Salvador             
##  [471] El Salvador              El Salvador             
##  [473] El Salvador              El Salvador             
##  [475] El Salvador              El Salvador             
##  [477] El Salvador              El Salvador             
##  [479] El Salvador              El Salvador             
##  [481] Equatorial Guinea        Equatorial Guinea       
##  [483] Equatorial Guinea        Equatorial Guinea       
##  [485] Equatorial Guinea        Equatorial Guinea       
##  [487] Equatorial Guinea        Equatorial Guinea       
##  [489] Equatorial Guinea        Equatorial Guinea       
##  [491] Equatorial Guinea        Equatorial Guinea       
##  [493] Eritrea                  Eritrea                 
##  [495] Eritrea                  Eritrea                 
##  [497] Eritrea                  Eritrea                 
##  [499] Eritrea                  Eritrea                 
##  [501] Eritrea                  Eritrea                 
##  [503] Eritrea                  Eritrea                 
##  [505] Ethiopia                 Ethiopia                
##  [507] Ethiopia                 Ethiopia                
##  [509] Ethiopia                 Ethiopia                
##  [511] Ethiopia                 Ethiopia                
##  [513] Ethiopia                 Ethiopia                
##  [515] Ethiopia                 Ethiopia                
##  [517] Finland                  Finland                 
##  [519] Finland                  Finland                 
##  [521] Finland                  Finland                 
##  [523] Finland                  Finland                 
##  [525] Finland                  Finland                 
##  [527] Finland                  Finland                 
##  [529] France                   France                  
##  [531] France                   France                  
##  [533] France                   France                  
##  [535] France                   France                  
##  [537] France                   France                  
##  [539] France                   France                  
##  [541] Gabon                    Gabon                   
##  [543] Gabon                    Gabon                   
##  [545] Gabon                    Gabon                   
##  [547] Gabon                    Gabon                   
##  [549] Gabon                    Gabon                   
##  [551] Gabon                    Gabon                   
##  [553] Gambia                   Gambia                  
##  [555] Gambia                   Gambia                  
##  [557] Gambia                   Gambia                  
##  [559] Gambia                   Gambia                  
##  [561] Gambia                   Gambia                  
##  [563] Gambia                   Gambia                  
##  [565] Germany                  Germany                 
##  [567] Germany                  Germany                 
##  [569] Germany                  Germany                 
##  [571] Germany                  Germany                 
##  [573] Germany                  Germany                 
##  [575] Germany                  Germany                 
##  [577] Ghana                    Ghana                   
##  [579] Ghana                    Ghana                   
##  [581] Ghana                    Ghana                   
##  [583] Ghana                    Ghana                   
##  [585] Ghana                    Ghana                   
##  [587] Ghana                    Ghana                   
##  [589] Greece                   Greece                  
##  [591] Greece                   Greece                  
##  [593] Greece                   Greece                  
##  [595] Greece                   Greece                  
##  [597] Greece                   Greece                  
##  [599] Greece                   Greece                  
##  [601] Guatemala                Guatemala               
##  [603] Guatemala                Guatemala               
##  [605] Guatemala                Guatemala               
##  [607] Guatemala                Guatemala               
##  [609] Guatemala                Guatemala               
##  [611] Guatemala                Guatemala               
##  [613] Guinea                   Guinea                  
##  [615] Guinea                   Guinea                  
##  [617] Guinea                   Guinea                  
##  [619] Guinea                   Guinea                  
##  [621] Guinea                   Guinea                  
##  [623] Guinea                   Guinea                  
##  [625] Guinea-Bissau            Guinea-Bissau           
##  [627] Guinea-Bissau            Guinea-Bissau           
##  [629] Guinea-Bissau            Guinea-Bissau           
##  [631] Guinea-Bissau            Guinea-Bissau           
##  [633] Guinea-Bissau            Guinea-Bissau           
##  [635] Guinea-Bissau            Guinea-Bissau           
##  [637] Haiti                    Haiti                   
##  [639] Haiti                    Haiti                   
##  [641] Haiti                    Haiti                   
##  [643] Haiti                    Haiti                   
##  [645] Haiti                    Haiti                   
##  [647] Haiti                    Haiti                   
##  [649] Honduras                 Honduras                
##  [651] Honduras                 Honduras                
##  [653] Honduras                 Honduras                
##  [655] Honduras                 Honduras                
##  [657] Honduras                 Honduras                
##  [659] Honduras                 Honduras                
##  [661] Hong Kong, China         Hong Kong, China        
##  [663] Hong Kong, China         Hong Kong, China        
##  [665] Hong Kong, China         Hong Kong, China        
##  [667] Hong Kong, China         Hong Kong, China        
##  [669] Hong Kong, China         Hong Kong, China        
##  [671] Hong Kong, China         Hong Kong, China        
##  [673] Hungary                  Hungary                 
##  [675] Hungary                  Hungary                 
##  [677] Hungary                  Hungary                 
##  [679] Hungary                  Hungary                 
##  [681] Hungary                  Hungary                 
##  [683] Hungary                  Hungary                 
##  [685] Iceland                  Iceland                 
##  [687] Iceland                  Iceland                 
##  [689] Iceland                  Iceland                 
##  [691] Iceland                  Iceland                 
##  [693] Iceland                  Iceland                 
##  [695] Iceland                  Iceland                 
##  [697] India                    India                   
##  [699] India                    India                   
##  [701] India                    India                   
##  [703] India                    India                   
##  [705] India                    India                   
##  [707] India                    India                   
##  [709] Indonesia                Indonesia               
##  [711] Indonesia                Indonesia               
##  [713] Indonesia                Indonesia               
##  [715] Indonesia                Indonesia               
##  [717] Indonesia                Indonesia               
##  [719] Indonesia                Indonesia               
##  [721] Iran                     Iran                    
##  [723] Iran                     Iran                    
##  [725] Iran                     Iran                    
##  [727] Iran                     Iran                    
##  [729] Iran                     Iran                    
##  [731] Iran                     Iran                    
##  [733] Iraq                     Iraq                    
##  [735] Iraq                     Iraq                    
##  [737] Iraq                     Iraq                    
##  [739] Iraq                     Iraq                    
##  [741] Iraq                     Iraq                    
##  [743] Iraq                     Iraq                    
##  [745] Ireland                  Ireland                 
##  [747] Ireland                  Ireland                 
##  [749] Ireland                  Ireland                 
##  [751] Ireland                  Ireland                 
##  [753] Ireland                  Ireland                 
##  [755] Ireland                  Ireland                 
##  [757] Israel                   Israel                  
##  [759] Israel                   Israel                  
##  [761] Israel                   Israel                  
##  [763] Israel                   Israel                  
##  [765] Israel                   Israel                  
##  [767] Israel                   Israel                  
##  [769] Italy                    Italy                   
##  [771] Italy                    Italy                   
##  [773] Italy                    Italy                   
##  [775] Italy                    Italy                   
##  [777] Italy                    Italy                   
##  [779] Italy                    Italy                   
##  [781] Jamaica                  Jamaica                 
##  [783] Jamaica                  Jamaica                 
##  [785] Jamaica                  Jamaica                 
##  [787] Jamaica                  Jamaica                 
##  [789] Jamaica                  Jamaica                 
##  [791] Jamaica                  Jamaica                 
##  [793] Japan                    Japan                   
##  [795] Japan                    Japan                   
##  [797] Japan                    Japan                   
##  [799] Japan                    Japan                   
##  [801] Japan                    Japan                   
##  [803] Japan                    Japan                   
##  [805] Jordan                   Jordan                  
##  [807] Jordan                   Jordan                  
##  [809] Jordan                   Jordan                  
##  [811] Jordan                   Jordan                  
##  [813] Jordan                   Jordan                  
##  [815] Jordan                   Jordan                  
##  [817] Kenya                    Kenya                   
##  [819] Kenya                    Kenya                   
##  [821] Kenya                    Kenya                   
##  [823] Kenya                    Kenya                   
##  [825] Kenya                    Kenya                   
##  [827] Kenya                    Kenya                   
##  [829] Korea, Dem. Rep.         Korea, Dem. Rep.        
##  [831] Korea, Dem. Rep.         Korea, Dem. Rep.        
##  [833] Korea, Dem. Rep.         Korea, Dem. Rep.        
##  [835] Korea, Dem. Rep.         Korea, Dem. Rep.        
##  [837] Korea, Dem. Rep.         Korea, Dem. Rep.        
##  [839] Korea, Dem. Rep.         Korea, Dem. Rep.        
##  [841] Korea, Rep.              Korea, Rep.             
##  [843] Korea, Rep.              Korea, Rep.             
##  [845] Korea, Rep.              Korea, Rep.             
##  [847] Korea, Rep.              Korea, Rep.             
##  [849] Korea, Rep.              Korea, Rep.             
##  [851] Korea, Rep.              Korea, Rep.             
##  [853] Kuwait                   Kuwait                  
##  [855] Kuwait                   Kuwait                  
##  [857] Kuwait                   Kuwait                  
##  [859] Kuwait                   Kuwait                  
##  [861] Kuwait                   Kuwait                  
##  [863] Kuwait                   Kuwait                  
##  [865] Lebanon                  Lebanon                 
##  [867] Lebanon                  Lebanon                 
##  [869] Lebanon                  Lebanon                 
##  [871] Lebanon                  Lebanon                 
##  [873] Lebanon                  Lebanon                 
##  [875] Lebanon                  Lebanon                 
##  [877] Lesotho                  Lesotho                 
##  [879] Lesotho                  Lesotho                 
##  [881] Lesotho                  Lesotho                 
##  [883] Lesotho                  Lesotho                 
##  [885] Lesotho                  Lesotho                 
##  [887] Lesotho                  Lesotho                 
##  [889] Liberia                  Liberia                 
##  [891] Liberia                  Liberia                 
##  [893] Liberia                  Liberia                 
##  [895] Liberia                  Liberia                 
##  [897] Liberia                  Liberia                 
##  [899] Liberia                  Liberia                 
##  [901] Libya                    Libya                   
##  [903] Libya                    Libya                   
##  [905] Libya                    Libya                   
##  [907] Libya                    Libya                   
##  [909] Libya                    Libya                   
##  [911] Libya                    Libya                   
##  [913] Madagascar               Madagascar              
##  [915] Madagascar               Madagascar              
##  [917] Madagascar               Madagascar              
##  [919] Madagascar               Madagascar              
##  [921] Madagascar               Madagascar              
##  [923] Madagascar               Madagascar              
##  [925] Malawi                   Malawi                  
##  [927] Malawi                   Malawi                  
##  [929] Malawi                   Malawi                  
##  [931] Malawi                   Malawi                  
##  [933] Malawi                   Malawi                  
##  [935] Malawi                   Malawi                  
##  [937] Malaysia                 Malaysia                
##  [939] Malaysia                 Malaysia                
##  [941] Malaysia                 Malaysia                
##  [943] Malaysia                 Malaysia                
##  [945] Malaysia                 Malaysia                
##  [947] Malaysia                 Malaysia                
##  [949] Mali                     Mali                    
##  [951] Mali                     Mali                    
##  [953] Mali                     Mali                    
##  [955] Mali                     Mali                    
##  [957] Mali                     Mali                    
##  [959] Mali                     Mali                    
##  [961] Mauritania               Mauritania              
##  [963] Mauritania               Mauritania              
##  [965] Mauritania               Mauritania              
##  [967] Mauritania               Mauritania              
##  [969] Mauritania               Mauritania              
##  [971] Mauritania               Mauritania              
##  [973] Mauritius                Mauritius               
##  [975] Mauritius                Mauritius               
##  [977] Mauritius                Mauritius               
##  [979] Mauritius                Mauritius               
##  [981] Mauritius                Mauritius               
##  [983] Mauritius                Mauritius               
##  [985] Mexico                   Mexico                  
##  [987] Mexico                   Mexico                  
##  [989] Mexico                   Mexico                  
##  [991] Mexico                   Mexico                  
##  [993] Mexico                   Mexico                  
##  [995] Mexico                   Mexico                  
##  [997] Mongolia                 Mongolia                
##  [999] Mongolia                 Mongolia                
## [1001] Mongolia                 Mongolia                
## [1003] Mongolia                 Mongolia                
## [1005] Mongolia                 Mongolia                
## [1007] Mongolia                 Mongolia                
## [1009] Montenegro               Montenegro              
## [1011] Montenegro               Montenegro              
## [1013] Montenegro               Montenegro              
## [1015] Montenegro               Montenegro              
## [1017] Montenegro               Montenegro              
## [1019] Montenegro               Montenegro              
## [1021] Morocco                  Morocco                 
## [1023] Morocco                  Morocco                 
## [1025] Morocco                  Morocco                 
## [1027] Morocco                  Morocco                 
## [1029] Morocco                  Morocco                 
## [1031] Morocco                  Morocco                 
## [1033] Mozambique               Mozambique              
## [1035] Mozambique               Mozambique              
## [1037] Mozambique               Mozambique              
## [1039] Mozambique               Mozambique              
## [1041] Mozambique               Mozambique              
## [1043] Mozambique               Mozambique              
## [1045] Myanmar                  Myanmar                 
## [1047] Myanmar                  Myanmar                 
## [1049] Myanmar                  Myanmar                 
## [1051] Myanmar                  Myanmar                 
## [1053] Myanmar                  Myanmar                 
## [1055] Myanmar                  Myanmar                 
## [1057] Namibia                  Namibia                 
## [1059] Namibia                  Namibia                 
## [1061] Namibia                  Namibia                 
## [1063] Namibia                  Namibia                 
## [1065] Namibia                  Namibia                 
## [1067] Namibia                  Namibia                 
## [1069] Nepal                    Nepal                   
## [1071] Nepal                    Nepal                   
## [1073] Nepal                    Nepal                   
## [1075] Nepal                    Nepal                   
## [1077] Nepal                    Nepal                   
## [1079] Nepal                    Nepal                   
## [1081] Netherlands              Netherlands             
## [1083] Netherlands              Netherlands             
## [1085] Netherlands              Netherlands             
## [1087] Netherlands              Netherlands             
## [1089] Netherlands              Netherlands             
## [1091] Netherlands              Netherlands             
## [1093] New Zealand              New Zealand             
## [1095] New Zealand              New Zealand             
## [1097] New Zealand              New Zealand             
## [1099] New Zealand              New Zealand             
## [1101] New Zealand              New Zealand             
## [1103] New Zealand              New Zealand             
## [1105] Nicaragua                Nicaragua               
## [1107] Nicaragua                Nicaragua               
## [1109] Nicaragua                Nicaragua               
## [1111] Nicaragua                Nicaragua               
## [1113] Nicaragua                Nicaragua               
## [1115] Nicaragua                Nicaragua               
## [1117] Niger                    Niger                   
## [1119] Niger                    Niger                   
## [1121] Niger                    Niger                   
## [1123] Niger                    Niger                   
## [1125] Niger                    Niger                   
## [1127] Niger                    Niger                   
## [1129] Nigeria                  Nigeria                 
## [1131] Nigeria                  Nigeria                 
## [1133] Nigeria                  Nigeria                 
## [1135] Nigeria                  Nigeria                 
## [1137] Nigeria                  Nigeria                 
## [1139] Nigeria                  Nigeria                 
## [1141] Norway                   Norway                  
## [1143] Norway                   Norway                  
## [1145] Norway                   Norway                  
## [1147] Norway                   Norway                  
## [1149] Norway                   Norway                  
## [1151] Norway                   Norway                  
## [1153] Oman                     Oman                    
## [1155] Oman                     Oman                    
## [1157] Oman                     Oman                    
## [1159] Oman                     Oman                    
## [1161] Oman                     Oman                    
## [1163] Oman                     Oman                    
## [1165] Pakistan                 Pakistan                
## [1167] Pakistan                 Pakistan                
## [1169] Pakistan                 Pakistan                
## [1171] Pakistan                 Pakistan                
## [1173] Pakistan                 Pakistan                
## [1175] Pakistan                 Pakistan                
## [1177] Panama                   Panama                  
## [1179] Panama                   Panama                  
## [1181] Panama                   Panama                  
## [1183] Panama                   Panama                  
## [1185] Panama                   Panama                  
## [1187] Panama                   Panama                  
## [1189] Paraguay                 Paraguay                
## [1191] Paraguay                 Paraguay                
## [1193] Paraguay                 Paraguay                
## [1195] Paraguay                 Paraguay                
## [1197] Paraguay                 Paraguay                
## [1199] Paraguay                 Paraguay                
## [1201] Peru                     Peru                    
## [1203] Peru                     Peru                    
## [1205] Peru                     Peru                    
## [1207] Peru                     Peru                    
## [1209] Peru                     Peru                    
## [1211] Peru                     Peru                    
## [1213] Philippines              Philippines             
## [1215] Philippines              Philippines             
## [1217] Philippines              Philippines             
## [1219] Philippines              Philippines             
## [1221] Philippines              Philippines             
## [1223] Philippines              Philippines             
## [1225] Poland                   Poland                  
## [1227] Poland                   Poland                  
## [1229] Poland                   Poland                  
## [1231] Poland                   Poland                  
## [1233] Poland                   Poland                  
## [1235] Poland                   Poland                  
## [1237] Portugal                 Portugal                
## [1239] Portugal                 Portugal                
## [1241] Portugal                 Portugal                
## [1243] Portugal                 Portugal                
## [1245] Portugal                 Portugal                
## [1247] Portugal                 Portugal                
## [1249] Puerto Rico              Puerto Rico             
## [1251] Puerto Rico              Puerto Rico             
## [1253] Puerto Rico              Puerto Rico             
## [1255] Puerto Rico              Puerto Rico             
## [1257] Puerto Rico              Puerto Rico             
## [1259] Puerto Rico              Puerto Rico             
## [1261] Reunion                  Reunion                 
## [1263] Reunion                  Reunion                 
## [1265] Reunion                  Reunion                 
## [1267] Reunion                  Reunion                 
## [1269] Reunion                  Reunion                 
## [1271] Reunion                  Reunion                 
## [1273] Romania                  Romania                 
## [1275] Romania                  Romania                 
## [1277] Romania                  Romania                 
## [1279] Romania                  Romania                 
## [1281] Romania                  Romania                 
## [1283] Romania                  Romania                 
## [1285] Rwanda                   Rwanda                  
## [1287] Rwanda                   Rwanda                  
## [1289] Rwanda                   Rwanda                  
## [1291] Rwanda                   Rwanda                  
## [1293] Rwanda                   Rwanda                  
## [1295] Rwanda                   Rwanda                  
## [1297] Sao Tome and Principe    Sao Tome and Principe   
## [1299] Sao Tome and Principe    Sao Tome and Principe   
## [1301] Sao Tome and Principe    Sao Tome and Principe   
## [1303] Sao Tome and Principe    Sao Tome and Principe   
## [1305] Sao Tome and Principe    Sao Tome and Principe   
## [1307] Sao Tome and Principe    Sao Tome and Principe   
## [1309] Saudi Arabia             Saudi Arabia            
## [1311] Saudi Arabia             Saudi Arabia            
## [1313] Saudi Arabia             Saudi Arabia            
## [1315] Saudi Arabia             Saudi Arabia            
## [1317] Saudi Arabia             Saudi Arabia            
## [1319] Saudi Arabia             Saudi Arabia            
## [1321] Senegal                  Senegal                 
## [1323] Senegal                  Senegal                 
## [1325] Senegal                  Senegal                 
## [1327] Senegal                  Senegal                 
## [1329] Senegal                  Senegal                 
## [1331] Senegal                  Senegal                 
## [1333] Serbia                   Serbia                  
## [1335] Serbia                   Serbia                  
## [1337] Serbia                   Serbia                  
## [1339] Serbia                   Serbia                  
## [1341] Serbia                   Serbia                  
## [1343] Serbia                   Serbia                  
## [1345] Sierra Leone             Sierra Leone            
## [1347] Sierra Leone             Sierra Leone            
## [1349] Sierra Leone             Sierra Leone            
## [1351] Sierra Leone             Sierra Leone            
## [1353] Sierra Leone             Sierra Leone            
## [1355] Sierra Leone             Sierra Leone            
## [1357] Singapore                Singapore               
## [1359] Singapore                Singapore               
## [1361] Singapore                Singapore               
## [1363] Singapore                Singapore               
## [1365] Singapore                Singapore               
## [1367] Singapore                Singapore               
## [1369] Slovak Republic          Slovak Republic         
## [1371] Slovak Republic          Slovak Republic         
## [1373] Slovak Republic          Slovak Republic         
## [1375] Slovak Republic          Slovak Republic         
## [1377] Slovak Republic          Slovak Republic         
## [1379] Slovak Republic          Slovak Republic         
## [1381] Slovenia                 Slovenia                
## [1383] Slovenia                 Slovenia                
## [1385] Slovenia                 Slovenia                
## [1387] Slovenia                 Slovenia                
## [1389] Slovenia                 Slovenia                
## [1391] Slovenia                 Slovenia                
## [1393] Somalia                  Somalia                 
## [1395] Somalia                  Somalia                 
## [1397] Somalia                  Somalia                 
## [1399] Somalia                  Somalia                 
## [1401] Somalia                  Somalia                 
## [1403] Somalia                  Somalia                 
## [1405] South Africa             South Africa            
## [1407] South Africa             South Africa            
## [1409] South Africa             South Africa            
## [1411] South Africa             South Africa            
## [1413] South Africa             South Africa            
## [1415] South Africa             South Africa            
## [1417] Spain                    Spain                   
## [1419] Spain                    Spain                   
## [1421] Spain                    Spain                   
## [1423] Spain                    Spain                   
## [1425] Spain                    Spain                   
## [1427] Spain                    Spain                   
## [1429] Sri Lanka                Sri Lanka               
## [1431] Sri Lanka                Sri Lanka               
## [1433] Sri Lanka                Sri Lanka               
## [1435] Sri Lanka                Sri Lanka               
## [1437] Sri Lanka                Sri Lanka               
## [1439] Sri Lanka                Sri Lanka               
## [1441] Sudan                    Sudan                   
## [1443] Sudan                    Sudan                   
## [1445] Sudan                    Sudan                   
## [1447] Sudan                    Sudan                   
## [1449] Sudan                    Sudan                   
## [1451] Sudan                    Sudan                   
## [1453] Swaziland                Swaziland               
## [1455] Swaziland                Swaziland               
## [1457] Swaziland                Swaziland               
## [1459] Swaziland                Swaziland               
## [1461] Swaziland                Swaziland               
## [1463] Swaziland                Swaziland               
## [1465] Sweden                   Sweden                  
## [1467] Sweden                   Sweden                  
## [1469] Sweden                   Sweden                  
## [1471] Sweden                   Sweden                  
## [1473] Sweden                   Sweden                  
## [1475] Sweden                   Sweden                  
## [1477] Switzerland              Switzerland             
## [1479] Switzerland              Switzerland             
## [1481] Switzerland              Switzerland             
## [1483] Switzerland              Switzerland             
## [1485] Switzerland              Switzerland             
## [1487] Switzerland              Switzerland             
## [1489] Syria                    Syria                   
## [1491] Syria                    Syria                   
## [1493] Syria                    Syria                   
## [1495] Syria                    Syria                   
## [1497] Syria                    Syria                   
## [1499] Syria                    Syria                   
## [1501] Taiwan                   Taiwan                  
## [1503] Taiwan                   Taiwan                  
## [1505] Taiwan                   Taiwan                  
## [1507] Taiwan                   Taiwan                  
## [1509] Taiwan                   Taiwan                  
## [1511] Taiwan                   Taiwan                  
## [1513] Tanzania                 Tanzania                
## [1515] Tanzania                 Tanzania                
## [1517] Tanzania                 Tanzania                
## [1519] Tanzania                 Tanzania                
## [1521] Tanzania                 Tanzania                
## [1523] Tanzania                 Tanzania                
## [1525] Thailand                 Thailand                
## [1527] Thailand                 Thailand                
## [1529] Thailand                 Thailand                
## [1531] Thailand                 Thailand                
## [1533] Thailand                 Thailand                
## [1535] Thailand                 Thailand                
## [1537] Togo                     Togo                    
## [1539] Togo                     Togo                    
## [1541] Togo                     Togo                    
## [1543] Togo                     Togo                    
## [1545] Togo                     Togo                    
## [1547] Togo                     Togo                    
## [1549] Trinidad and Tobago      Trinidad and Tobago     
## [1551] Trinidad and Tobago      Trinidad and Tobago     
## [1553] Trinidad and Tobago      Trinidad and Tobago     
## [1555] Trinidad and Tobago      Trinidad and Tobago     
## [1557] Trinidad and Tobago      Trinidad and Tobago     
## [1559] Trinidad and Tobago      Trinidad and Tobago     
## [1561] Tunisia                  Tunisia                 
## [1563] Tunisia                  Tunisia                 
## [1565] Tunisia                  Tunisia                 
## [1567] Tunisia                  Tunisia                 
## [1569] Tunisia                  Tunisia                 
## [1571] Tunisia                  Tunisia                 
## [1573] Turkey                   Turkey                  
## [1575] Turkey                   Turkey                  
## [1577] Turkey                   Turkey                  
## [1579] Turkey                   Turkey                  
## [1581] Turkey                   Turkey                  
## [1583] Turkey                   Turkey                  
## [1585] Uganda                   Uganda                  
## [1587] Uganda                   Uganda                  
## [1589] Uganda                   Uganda                  
## [1591] Uganda                   Uganda                  
## [1593] Uganda                   Uganda                  
## [1595] Uganda                   Uganda                  
## [1597] United Kingdom           United Kingdom          
## [1599] United Kingdom           United Kingdom          
## [1601] United Kingdom           United Kingdom          
## [1603] United Kingdom           United Kingdom          
## [1605] United Kingdom           United Kingdom          
## [1607] United Kingdom           United Kingdom          
## [1609] United States            United States           
## [1611] United States            United States           
## [1613] United States            United States           
## [1615] United States            United States           
## [1617] United States            United States           
## [1619] United States            United States           
## [1621] Uruguay                  Uruguay                 
## [1623] Uruguay                  Uruguay                 
## [1625] Uruguay                  Uruguay                 
## [1627] Uruguay                  Uruguay                 
## [1629] Uruguay                  Uruguay                 
## [1631] Uruguay                  Uruguay                 
## [1633] Venezuela                Venezuela               
## [1635] Venezuela                Venezuela               
## [1637] Venezuela                Venezuela               
## [1639] Venezuela                Venezuela               
## [1641] Venezuela                Venezuela               
## [1643] Venezuela                Venezuela               
## [1645] Vietnam                  Vietnam                 
## [1647] Vietnam                  Vietnam                 
## [1649] Vietnam                  Vietnam                 
## [1651] Vietnam                  Vietnam                 
## [1653] Vietnam                  Vietnam                 
## [1655] Vietnam                  Vietnam                 
## [1657] West Bank and Gaza       West Bank and Gaza      
## [1659] West Bank and Gaza       West Bank and Gaza      
## [1661] West Bank and Gaza       West Bank and Gaza      
## [1663] West Bank and Gaza       West Bank and Gaza      
## [1665] West Bank and Gaza       West Bank and Gaza      
## [1667] West Bank and Gaza       West Bank and Gaza      
## [1669] Yemen, Rep.              Yemen, Rep.             
## [1671] Yemen, Rep.              Yemen, Rep.             
## [1673] Yemen, Rep.              Yemen, Rep.             
## [1675] Yemen, Rep.              Yemen, Rep.             
## [1677] Yemen, Rep.              Yemen, Rep.             
## [1679] Yemen, Rep.              Yemen, Rep.             
## [1681] Zambia                   Zambia                  
## [1683] Zambia                   Zambia                  
## [1685] Zambia                   Zambia                  
## [1687] Zambia                   Zambia                  
## [1689] Zambia                   Zambia                  
## [1691] Zambia                   Zambia                  
## [1693] Zimbabwe                 Zimbabwe                
## [1695] Zimbabwe                 Zimbabwe                
## [1697] Zimbabwe                 Zimbabwe                
## [1699] Zimbabwe                 Zimbabwe                
## [1701] Zimbabwe                 Zimbabwe                
## [1703] Zimbabwe                 Zimbabwe                
## 142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe
attach(gapminder)  #
gdpPercap
##    [1]    779.4453    820.8530    853.1007    836.1971    739.9811    786.1134
##    [7]    978.0114    852.3959    649.3414    635.3414    726.7341    974.5803
##   [13]   1601.0561   1942.2842   2312.8890   2760.1969   3313.4222   3533.0039
##   [19]   3630.8807   3738.9327   2497.4379   3193.0546   4604.2117   5937.0295
##   [25]   2449.0082   3013.9760   2550.8169   3246.9918   4182.6638   4910.4168
##   [31]   5745.1602   5681.3585   5023.2166   4797.2951   5288.0404   6223.3675
##   [37]   3520.6103   3827.9405   4269.2767   5522.7764   5473.2880   3008.6474
##   [43]   2756.9537   2430.2083   2627.8457   2277.1409   2773.2873   4797.2313
##   [49]   5911.3151   6856.8562   7133.1660   8052.9530   9443.0385  10079.0267
##   [55]   8997.8974   9139.6714   9308.4187  10967.2820   8797.6407  12779.3796
##   [61]  10039.5956  10949.6496  12217.2269  14526.1246  16788.6295  18334.1975
##   [67]  19477.0093  21888.8890  23424.7668  26997.9366  30687.7547  34435.3674
##   [73]   6137.0765   8842.5980  10750.7211  12834.6024  16661.6256  19749.4223
##   [79]  21597.0836  23687.8261  27042.0187  29095.9207  32417.6077  36126.4927
##   [85]   9867.0848  11635.7995  12753.2751  14804.6727  18268.6584  19340.1020
##   [91]  19211.1473  18524.0241  19035.5792  20292.0168  23403.5593  29796.0483
##   [97]    684.2442    661.6375    686.3416    721.1861    630.2336    659.8772
##  [103]    676.9819    751.9794    837.8102    972.7700   1136.3904   1391.2538
##  [109]   8343.1051   9714.9606  10991.2068  13149.0412  16672.1436  19117.9745
##  [115]  20979.8459  22525.5631  25575.5707  27561.1966  30485.8838  33692.6051
##  [121]   1062.7522    959.6011    949.4991   1035.8314   1085.7969   1029.1613
##  [127]   1277.8976   1225.8560   1191.2077   1232.9753   1372.8779   1441.2849
##  [133]   2677.3263   2127.6863   2180.9725   2586.8861   2980.3313   3548.0978
##  [139]   3156.5105   2753.6915   2961.6997   3326.1432   3413.2627   3822.1371
##  [145]    973.5332   1353.9892   1709.6837   2172.3524   2860.1698   3528.4813
##  [151]   4126.6132   4314.1148   2546.7814   4766.3559   6018.9752   7446.2988
##  [157]    851.2411    918.2325    983.6540   1214.7093   2263.6111   3214.8578
##  [163]   4551.1421   6205.8839   7954.1116   8647.1423  11003.6051  12569.8518
##  [169]   2108.9444   2487.3660   3336.5858   3429.8644   4985.7115   6660.1187
##  [175]   7030.8359   7807.0958   6950.2830   7957.9808   8131.2128   9065.8008
##  [181]   2444.2866   3008.6707   4254.3378   5577.0028   6597.4944   7612.2404
##  [187]   8224.1916   8239.8548   6302.6234   5970.3888   7696.7777  10680.7928
##  [193]    543.2552    617.1835    722.5120    794.8266    854.7360    743.3870
##  [199]    807.1986    912.0631    931.7528    946.2950   1037.6452   1217.0330
##  [205]    339.2965    379.5646    355.2032    412.9775    464.0995    556.1033
##  [211]    559.6032    621.8188    631.6999    463.1151    446.4035    430.0707
##  [217]    368.4693    434.0383    496.9136    523.4323    421.6240    524.9722
##  [223]    624.4755    683.8956    682.3032    734.2852    896.2260   1713.7787
##  [229]   1172.6677   1313.0481   1399.6074   1508.4531   1684.1465   1783.4329
##  [235]   2367.9833   2602.6642   1793.1633   1694.3375   1934.0114   2042.0952
##  [241]  11367.1611  12489.9501  13462.4855  16076.5880  18970.5709  22090.8831
##  [247]  22898.7921  26626.5150  26342.8843  28954.9259  33328.9651  36319.2350
##  [253]   1071.3107   1190.8443   1193.0688   1136.0566   1070.0133   1109.3743
##  [259]    956.7530    844.8764    747.9055    740.5063    738.6906    706.0165
##  [265]   1178.6659   1308.4956   1389.8176   1196.8106   1104.1040   1133.9850
##  [271]    797.9081    952.3861   1058.0643   1004.9614   1156.1819   1704.0637
##  [277]   3939.9788   4315.6227   4519.0943   5106.6543   5494.0244   4756.7638
##  [283]   5095.6657   5547.0638   7596.1260  10118.0532  10778.7838  13171.6388
##  [289]    400.4486    575.9870    487.6740    612.7057    676.9001    741.2375
##  [295]    962.4214   1378.9040   1655.7842   2289.2341   3119.2809   4959.1149
##  [301]   2144.1151   2323.8056   2492.3511   2678.7298   3264.6600   3815.8079
##  [307]   4397.5757   4903.2191   5444.6486   6117.3617   5755.2600   7006.5804
##  [313]   1102.9909   1211.1485   1406.6483   1876.0296   1937.5777   1172.6030
##  [319]   1267.1001   1315.9808   1246.9074   1173.6182   1075.8116    986.1479
##  [325]    780.5423    905.8602    896.3146    861.5932    904.8961    795.7573
##  [331]    673.7478    672.7748    457.7192    312.1884    241.1659    277.5519
##  [337]   2125.6214   2315.0566   2464.7832   2677.9396   3213.1527   3259.1790
##  [343]   4879.5075   4201.1949   4016.2395   3484.1644   3484.0620   3632.5578
##  [349]   2627.0095   2990.0108   3460.9370   4161.7278   5118.1469   5926.8770
##  [355]   5262.7348   5629.9153   6160.4163   6677.0453   7723.4472   9645.0614
##  [361]   1388.5947   1500.8959   1728.8694   2052.0505   2378.2011   2517.7365
##  [367]   2602.7102   2156.9561   1648.0738   1786.2654   1648.8008   1544.7501
##  [373]   3119.2365   4338.2316   5477.8900   6960.2979   9164.0901  11305.3852
##  [379]  13221.8218  13822.5839   8447.7949   9875.6045  11628.3890  14619.2227
##  [385]   5586.5388   6092.1744   5180.7559   5690.2680   5305.4453   6380.4950
##  [391]   7316.9181   7532.9248   5592.8440   5431.9904   6340.6467   8948.1029
##  [397]   6876.1403   8256.3439  10136.8671  11399.4449  13108.4536  14800.1606
##  [403]  15377.2285  16310.4434  14297.0212  16048.5142  17596.2102  22833.3085
##  [409]   9692.3852  11099.6593  13583.3135  15937.2112  18866.2072  20422.9015
##  [415]  21688.0405  25116.1758  26406.7399  29804.3457  32166.5001  35278.4187
##  [421]   2669.5295   2864.9691   3020.9893   3020.0505   3694.2124   3081.7610
##  [427]   2879.4681   2880.1026   2377.1562   1895.0170   1908.2609   2082.4816
##  [433]   1397.7171   1544.4030   1662.1374   1653.7230   2189.8745   2681.9889
##  [439]   2861.0924   2899.8422   3044.2142   3614.1013   4563.8082   6025.3748
##  [445]   3522.1107   3780.5467   4086.1141   4579.0742   5280.9947   6679.6233
##  [451]   7213.7913   6481.7770   7103.7026   7429.4559   5773.0445   6873.2623
##  [457]   1418.8224   1458.9153   1693.3359   1814.8807   2024.0081   2785.4936
##  [463]   3503.7296   3885.4607   3794.7552   4173.1818   4754.6044   5581.1810
##  [469]   3048.3029   3421.5232   3776.8036   4358.5954   4520.2460   5138.9224
##  [475]   4098.3442   4140.4421   4444.2317   5154.8255   5351.5687   5728.3535
##  [481]    375.6431    426.0964    582.8420    915.5960    672.4123    958.5668
##  [487]    927.8253    966.8968   1132.0550   2814.4808   7703.4959  12154.0897
##  [493]    328.9406    344.1619    380.9958    468.7950    514.3242    505.7538
##  [499]    524.8758    521.1341    582.8585    913.4708    765.3500    641.3695
##  [505]    362.1463    378.9042    419.4564    516.1186    566.2439    556.8084
##  [511]    577.8607    573.7413    421.3535    515.8894    530.0535    690.8056
##  [517]   6424.5191   7545.4154   9371.8426  10921.6363  14358.8759  15605.4228
##  [523]  18533.1576  21141.0122  20647.1650  23723.9502  28204.5906  33207.0844
##  [529]   7029.8093   8662.8349  10560.4855  12999.9177  16107.1917  18292.6351
##  [535]  20293.8975  22066.4421  24703.7961  25889.7849  28926.0323  30470.0167
##  [541]   4293.4765   4976.1981   6631.4592   8358.7620  11401.9484  21745.5733
##  [547]  15113.3619  11864.4084  13522.1575  14722.8419  12521.7139  13206.4845
##  [553]    485.2307    520.9267    599.6503    734.7829    756.0868    884.7553
##  [559]    835.8096    611.6589    665.6244    653.7302    660.5856    752.7497
##  [565]   7144.1144  10187.8267  12902.4629  14745.6256  18016.1803  20512.9212
##  [571]  22031.5327  24639.1857  26505.3032  27788.8842  30035.8020  32170.3744
##  [577]    911.2989   1043.5615   1190.0411   1125.6972   1178.2237    993.2240
##  [583]    876.0326    847.0061    925.0602   1005.2458   1111.9846   1327.6089
##  [589]   3530.6901   4916.2999   6017.1907   8513.0970  12724.8296  14195.5243
##  [595]  15268.4209  16120.5284  17541.4963  18747.6981  22514.2548  27538.4119
##  [601]   2428.2378   2617.1560   2750.3644   3242.5311   4031.4083   4879.9927
##  [607]   4820.4948   4246.4860   4439.4508   4684.3138   4858.3475   5186.0500
##  [613]    510.1965    576.2670    686.3737    708.7595    741.6662    874.6859
##  [619]    857.2504    805.5725    794.3484    869.4498    945.5836    942.6542
##  [625]    299.8503    431.7905    522.0344    715.5806    820.2246    764.7260
##  [631]    838.1240    736.4154    745.5399    796.6645    575.7047    579.2317
##  [637]   1840.3669   1726.8879   1796.5890   1452.0577   1654.4569   1874.2989
##  [643]   2011.1595   1823.0160   1456.3095   1341.7269   1270.3649   1201.6372
##  [649]   2194.9262   2220.4877   2291.1568   2538.2694   2529.8423   3203.2081
##  [655]   3121.7608   3023.0967   3081.6946   3160.4549   3099.7287   3548.3308
##  [661]   3054.4212   3629.0765   4692.6483   6197.9628   8315.9281  11186.1413
##  [667]  14560.5305  20038.4727  24757.6030  28377.6322  30209.0152  39724.9787
##  [673]   5263.6738   6040.1800   7550.3599   9326.6447  10168.6561  11674.8374
##  [679]  12545.9907  12986.4800  10535.6285  11712.7768  14843.9356  18008.9444
##  [685]   7267.6884   9244.0014  10350.1591  13319.8957  15798.0636  19654.9625
##  [691]  23269.6075  26923.2063  25144.3920  28061.0997  31163.2020  36180.7892
##  [697]    546.5657    590.0620    658.3472    700.7706    724.0325    813.3373
##  [703]    855.7235    976.5127   1164.4068   1458.8174   1746.7695   2452.2104
##  [709]    749.6817    858.9003    849.2898    762.4318   1111.1079   1382.7021
##  [715]   1516.8730   1748.3570   2383.1409   3119.3356   2873.9129   3540.6516
##  [721]   3035.3260   3290.2576   4187.3298   5906.7318   9613.8186  11888.5951
##  [727]   7608.3346   6642.8814   7235.6532   8263.5903   9240.7620  11605.7145
##  [733]   4129.7661   6229.3336   8341.7378   8931.4598   9576.0376  14688.2351
##  [739]  14517.9071  11643.5727   3745.6407   3076.2398   4390.7173   4471.0619
##  [745]   5210.2803   5599.0779   6631.5973   7655.5690   9530.7729  11150.9811
##  [751]  12618.3214  13872.8665  17558.8155  24521.9471  34077.0494  40675.9964
##  [757]   4086.5221   5385.2785   7105.6307   8393.7414  12786.9322  13306.6192
##  [763]  15367.0292  17122.4799  18051.5225  20896.6092  21905.5951  25523.2771
##  [769]   4931.4042   6248.6562   8243.5823  10022.4013  12269.2738  14255.9847
##  [775]  16537.4835  19207.2348  22013.6449  24675.0245  27968.0982  28569.7197
##  [781]   2898.5309   4756.5258   5246.1075   6124.7035   7433.8893   6650.1956
##  [787]   6068.0513   6351.2375   7404.9237   7121.9247   6994.7749   7320.8803
##  [793]   3216.9563   4317.6944   6576.6495   9847.7886  14778.7864  16610.3770
##  [799]  19384.1057  22375.9419  26824.8951  28816.5850  28604.5919  31656.0681
##  [805]   1546.9078   1886.0806   2348.0092   2741.7963   2110.8563   2852.3516
##  [811]   4161.4160   4448.6799   3431.5936   3645.3796   3844.9172   4519.4612
##  [817]    853.5409    944.4383    896.9664   1056.7365   1222.3600   1267.6132
##  [823]   1348.2258   1361.9369   1341.9217   1360.4850   1287.5147   1463.2493
##  [829]   1088.2778   1571.1347   1621.6936   2143.5406   3701.6215   4106.3012
##  [835]   4106.5253   4106.4923   3726.0635   1690.7568   1646.7582   1593.0655
##  [841]   1030.5922   1487.5935   1536.3444   2029.2281   3030.8767   4657.2210
##  [847]   5622.9425   8533.0888  12104.2787  15993.5280  19233.9882  23348.1397
##  [853] 108382.3529 113523.1329  95458.1118  80894.8833 109347.8670  59265.4771
##  [859]  31354.0357  28118.4300  34932.9196  40300.6200  35110.1057  47306.9898
##  [865]   4834.8041   6089.7869   5714.5606   6006.9830   7486.3843   8659.6968
##  [871]   7640.5195   5377.0913   6890.8069   8754.9639   9313.9388  10461.0587
##  [877]    298.8462    335.9971    411.8006    498.6390    496.5816    745.3695
##  [883]    797.2631    773.9932    977.4863   1186.1480   1275.1846   1569.3314
##  [889]    575.5730    620.9700    634.1952    713.6036    803.0055    640.3224
##  [895]    572.1996    506.1139    636.6229    609.1740    531.4824    414.5073
##  [901]   2387.5481   3448.2844   6757.0308  18772.7517  21011.4972  21951.2118
##  [907]  17364.2754  11770.5898   9640.1385   9467.4461   9534.6775  12057.4993
##  [913]   1443.0117   1589.2027   1643.3871   1634.0473   1748.5630   1544.2286
##  [919]   1302.8787   1155.4419   1040.6762    986.2959    894.6371   1044.7701
##  [925]    369.1651    416.3698    427.9011    495.5148    584.6220    663.2237
##  [931]    632.8039    635.5174    563.2000    692.2758    665.4231    759.3499
##  [937]   1831.1329   1810.0670   2036.8849   2277.7424   2849.0948   3827.9216
##  [943]   4920.3560   5249.8027   7277.9128  10132.9096  10206.9779  12451.6558
##  [949]    452.3370    490.3822    496.1743    545.0099    581.3689    686.3953
##  [955]    618.0141    684.1716    739.0144    790.2580    951.4098   1042.5816
##  [961]    743.1159    846.1203   1055.8960   1421.1452   1586.8518   1497.4922
##  [967]   1481.1502   1421.6036   1361.3698   1483.1361   1579.0195   1803.1515
##  [973]   1967.9557   2034.0380   2529.0675   2475.3876   2575.4842   3710.9830
##  [979]   3688.0377   4783.5869   6058.2538   7425.7053   9021.8159  10956.9911
##  [985]   3478.1255   4131.5466   4581.6094   5754.7339   6809.4067   7674.9291
##  [991]   9611.1475   8688.1560   9472.3843   9767.2975  10742.4405  11977.5750
##  [997]    786.5669    912.6626   1056.3540   1226.0411   1421.7420   1647.5117
## [1003]   2000.6031   2338.0083   1785.4020   1902.2521   2140.7393   3095.7723
## [1009]   2647.5856   3682.2599   4649.5938   5907.8509   7778.4140   9595.9299
## [1015]  11222.5876  11732.5102   7003.3390   6465.6133   6557.1943   9253.8961
## [1021]   1688.2036   1642.0023   1566.3535   1711.0448   1930.1950   2370.6200
## [1027]   2702.6204   2755.0470   2948.0473   2982.1019   3258.4956   3820.1752
## [1033]    468.5260    495.5868    556.6864    566.6692    724.9178    502.3197
## [1039]    462.2114    389.8762    410.8968    472.3461    633.6179    823.6856
## [1045]    331.0000    350.0000    388.0000    349.0000    357.0000    371.0000
## [1051]    424.0000    385.0000    347.0000    415.0000    611.0000    944.0000
## [1057]   2423.7804   2621.4481   3173.2156   3793.6948   3746.0809   3876.4860
## [1063]   4191.1005   3693.7313   3804.5380   3899.5243   4072.3248   4811.0604
## [1069]    545.8657    597.9364    652.3969    676.4422    674.7881    694.1124
## [1075]    718.3731    775.6325    897.7404   1010.8921   1057.2063   1091.3598
## [1081]   8941.5719  11276.1934  12790.8496  15363.2514  18794.7457  21209.0592
## [1087]  21399.4605  23651.3236  26790.9496  30246.1306  33724.7578  36797.9333
## [1093]  10556.5757  12247.3953  13175.6780  14463.9189  16046.0373  16233.7177
## [1099]  17632.4104  19007.1913  18363.3249  21050.4138  23189.8014  25185.0091
## [1105]   3112.3639   3457.4159   3634.3644   4643.3935   4688.5933   5486.3711
## [1111]   3470.3382   2955.9844   2170.1517   2253.0230   2474.5488   2749.3210
## [1117]    761.8794    835.5234    997.7661   1054.3849    954.2092    808.8971
## [1123]    909.7221    668.3000    581.1827    580.3052    601.0745    619.6769
## [1129]   1077.2819   1100.5926   1150.9275   1014.5141   1698.3888   1981.9518
## [1135]   1576.9738   1385.0296   1619.8482   1624.9413   1615.2864   2013.9773
## [1141]  10095.4217  11653.9730  13450.4015  16361.8765  18965.0555  23311.3494
## [1147]  26298.6353  31540.9748  33965.6611  41283.1643  44683.9753  49357.1902
## [1153]   1828.2303   2242.7466   2924.6381   4720.9427  10618.0385  11848.3439
## [1159]  12954.7910  18115.2231  18616.7069  19702.0558  19774.8369  22316.1929
## [1165]    684.5971    747.0835    803.3427    942.4083   1049.9390   1175.9212
## [1171]   1443.4298   1704.6866   1971.8295   2049.3505   2092.7124   2605.9476
## [1177]   2480.3803   2961.8009   3536.5403   4421.0091   5364.2497   5351.9121
## [1183]   7009.6016   7034.7792   6618.7431   7113.6923   7356.0319   9809.1856
## [1189]   1952.3087   2046.1547   2148.0271   2299.3763   2523.3380   3248.3733
## [1195]   4258.5036   3998.8757   4196.4111   4247.4003   3783.6742   4172.8385
## [1201]   3758.5234   4245.2567   4957.0380   5788.0933   5937.8273   6281.2909
## [1207]   6434.5018   6360.9434   4446.3809   5838.3477   5909.0201   7408.9056
## [1213]   1272.8810   1547.9448   1649.5522   1814.1274   1989.3741   2373.2043
## [1219]   2603.2738   2189.6350   2279.3240   2536.5349   2650.9211   3190.4810
## [1225]   4029.3297   4734.2530   5338.7521   6557.1528   8006.5070   9508.1415
## [1231]   8451.5310   9082.3512   7738.8812  10159.5837  12002.2391  15389.9247
## [1237]   3068.3199   3774.5717   4727.9549   6361.5180   9022.2474  10172.4857
## [1243]  11753.8429  13039.3088  16207.2666  17641.0316  19970.9079  20509.6478
## [1249]   3081.9598   3907.1562   5108.3446   6929.2777   9123.0417   9770.5249
## [1255]  10330.9891  12281.3419  14641.5871  16999.4333  18855.6062  19328.7090
## [1261]   2718.8853   2769.4518   3173.7233   4021.1757   5047.6586   4319.8041
## [1267]   5267.2194   5303.3775   6101.2558   6071.9414   6316.1652   7670.1226
## [1273]   3144.6132   3943.3702   4734.9976   6470.8665   8011.4144   9356.3972
## [1279]   9605.3141   9696.2733   6598.4099   7346.5476   7885.3601  10808.4756
## [1285]    493.3239    540.2894    597.4731    510.9637    590.5807    670.0806
## [1291]    881.5706    847.9912    737.0686    589.9445    785.6538    863.0885
## [1297]    879.5836    860.7369   1071.5511   1384.8406   1532.9853   1737.5617
## [1303]   1890.2181   1516.5255   1428.7778   1339.0760   1353.0924   1598.4351
## [1309]   6459.5548   8157.5912  11626.4197  16903.0489  24837.4287  34167.7626
## [1315]  33693.1753  21198.2614  24841.6178  20586.6902  19014.5412  21654.8319
## [1321]   1450.3570   1567.6530   1654.9887   1612.4046   1597.7121   1561.7691
## [1327]   1518.4800   1441.7207   1367.8994   1392.3683   1519.6353   1712.4721
## [1333]   3581.4594   4981.0909   6289.6292   7991.7071  10522.0675  12980.6696
## [1339]  15181.0927  15870.8785   9325.0682   7914.3203   7236.0753   9786.5347
## [1345]    879.7877   1004.4844   1116.6399   1206.0435   1353.7598   1348.2852
## [1351]   1465.0108   1294.4478   1068.6963    574.6482    699.4897    862.5408
## [1357]   2315.1382   2843.1044   3674.7356   4977.4185   8597.7562  11210.0895
## [1363]  15169.1611  18861.5308  24769.8912  33519.4766  36023.1054  47143.1796
## [1369]   5074.6591   6093.2630   7481.1076   8412.9024   9674.1676  10922.6640
## [1375]  11348.5459  12037.2676   9498.4677  12126.2306  13638.7784  18678.3144
## [1381]   4215.0417   5862.2766   7402.3034   9405.4894  12383.4862  15277.0302
## [1387]  17866.7218  18678.5349  14214.7168  17161.1073  20660.0194  25768.2576
## [1393]   1135.7498   1258.1474   1369.4883   1284.7332   1254.5761   1450.9925
## [1399]   1176.8070   1093.2450    926.9603    930.5964    882.0818    926.1411
## [1405]   4725.2955   5487.1042   5768.7297   7114.4780   7765.9626   8028.6514
## [1411]   8568.2662   7825.8234   7225.0693   7479.1882   7710.9464   9269.6578
## [1417]   3834.0347   4564.8024   5693.8439   7993.5123  10638.7513  13236.9212
## [1423]  13926.1700  15764.9831  18603.0645  20445.2990  24835.4717  28821.0637
## [1429]   1083.5320   1072.5466   1074.4720   1135.5143   1213.3955   1348.7757
## [1435]   1648.0798   1876.7668   2153.7392   2664.4773   3015.3788   3970.0954
## [1441]   1615.9911   1770.3371   1959.5938   1687.9976   1659.6528   2202.9884
## [1447]   1895.5441   1507.8192   1492.1970   1632.2108   1993.3983   2602.3950
## [1453]   1148.3766   1244.7084   1856.1821   2613.1017   3364.8366   3781.4106
## [1459]   3895.3840   3984.8398   3553.0224   3876.7685   4128.1169   4513.4806
## [1465]   8527.8447   9911.8782  12329.4419  15258.2970  17832.0246  18855.7252
## [1471]  20667.3812  23586.9293  23880.0168  25266.5950  29341.6309  33859.7484
## [1477]  14734.2327  17909.4897  20431.0927  22966.1443  27195.1130  26982.2905
## [1483]  28397.7151  30281.7046  31871.5303  32135.3230  34480.9577  37506.4191
## [1489]   1643.4854   2117.2349   2193.0371   1881.9236   2571.4230   3195.4846
## [1495]   3761.8377   3116.7743   3340.5428   4014.2390   4090.9253   4184.5481
## [1501]   1206.9479   1507.8613   1822.8790   2643.8587   4062.5239   5596.5198
## [1507]   7426.3548  11054.5618  15215.6579  20206.8210  23235.4233  28718.2768
## [1513]    716.6501    698.5356    722.0038    848.2187    915.9851    962.4923
## [1519]    874.2426    831.8221    825.6825    789.1862    899.0742   1107.4822
## [1525]    757.7974    793.5774   1002.1992   1295.4607   1524.3589   1961.2246
## [1531]   2393.2198   2982.6538   4616.8965   5852.6255   5913.1875   7458.3963
## [1537]    859.8087    925.9083   1067.5348   1477.5968   1649.6602   1532.7770
## [1543]   1344.5780   1202.2014   1034.2989    982.2869    886.2206    882.9699
## [1549]   3023.2719   4100.3934   4997.5240   5621.3685   6619.5514   7899.5542
## [1555]   9119.5286   7388.5978   7370.9909   8792.5731  11460.6002  18008.5092
## [1561]   1468.4756   1395.2325   1660.3032   1932.3602   2753.2860   3120.8768
## [1567]   3560.2332   3810.4193   4332.7202   4876.7986   5722.8957   7092.9230
## [1573]   1969.1010   2218.7543   2322.8699   2826.3564   3450.6964   4269.1223
## [1579]   4241.3563   5089.0437   5678.3483   6601.4299   6508.0857   8458.2764
## [1585]    734.7535    774.3711    767.2717    908.9185    950.7359    843.7331
## [1591]    682.2662    617.7244    644.1708    816.5591    927.7210   1056.3801
## [1597]   9979.5085  11283.1779  12477.1771  14142.8509  15895.1164  17428.7485
## [1603]  18232.4245  21664.7877  22705.0925  26074.5314  29478.9992  33203.2613
## [1609]  13990.4821  14847.1271  16173.1459  19530.3656  21806.0359  24072.6321
## [1615]  25009.5591  29884.3504  32003.9322  35767.4330  39097.0995  42951.6531
## [1621]   5716.7667   6150.7730   5603.3577   5444.6196   5703.4089   6504.3397
## [1627]   6920.2231   7452.3990   8137.0048   9230.2407   7727.0020  10611.4630
## [1633]   7689.7998   9802.4665   8422.9742   9541.4742  10505.2597  13143.9510
## [1639]  11152.4101   9883.5846  10733.9263  10165.4952   8605.0478  11415.8057
## [1645]    605.0665    676.2854    772.0492    637.1233    699.5016    713.5371
## [1651]    707.2358    820.7994    989.0231   1385.8968   1764.4567   2441.5764
## [1657]   1515.5923   1827.0677   2198.9563   2649.7150   3133.4093   3682.8315
## [1663]   4336.0321   5107.1974   6017.6548   7110.6676   4515.4876   3025.3498
## [1669]    781.7176    804.8305    825.6232    862.4421   1265.0470   1829.7652
## [1675]   1977.5570   1971.7415   1879.4967   2117.4845   2234.8208   2280.7699
## [1681]   1147.3888   1311.9568   1452.7258   1777.0773   1773.4983   1588.6883
## [1687]   1408.6786   1213.3151   1210.8846   1071.3538   1071.6139   1271.2116
## [1693]    406.8841    518.7643    527.2722    569.7951    799.3622    685.5877
## [1699]    788.8550    706.1573    693.4208    792.4500    672.0386    469.7093

Cuántos individuos?

ncol(gapminder)# numero dde columnas
## [1] 6
nrow(gapminder) # numero de filas
## [1] 1704
dim(gapminder)
## [1] 1704    6

6 primeras filas de la base y 6 ultimas filas de la base.

head(gapminder) # 
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.
tail(gapminder) # 
## # A tibble: 6 × 6
##   country  continent  year lifeExp      pop gdpPercap
##   <fct>    <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Zimbabwe Africa     1982    60.4  7636524      789.
## 2 Zimbabwe Africa     1987    62.4  9216418      706.
## 3 Zimbabwe Africa     1992    60.4 10704340      693.
## 4 Zimbabwe Africa     1997    46.8 11404948      792.
## 5 Zimbabwe Africa     2002    40.0 11926563      672.
## 6 Zimbabwe Africa     2007    43.5 12311143      470.

Si se desean ver más datos

head(gapminder, n=20)
## # A tibble: 20 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## 11 Afghanistan Asia       2002    42.1 25268405      727.
## 12 Afghanistan Asia       2007    43.8 31889923      975.
## 13 Albania     Europe     1952    55.2  1282697     1601.
## 14 Albania     Europe     1957    59.3  1476505     1942.
## 15 Albania     Europe     1962    64.8  1728137     2313.
## 16 Albania     Europe     1967    66.2  1984060     2760.
## 17 Albania     Europe     1972    67.7  2263554     3313.
## 18 Albania     Europe     1977    68.9  2509048     3533.
## 19 Albania     Europe     1982    70.4  2780097     3631.
## 20 Albania     Europe     1987    72    3075321     3739.

DPLYR

dplyr es un paquete que permite manipular datos de forma intuitiva. Tiene 5 funciones o verbos principales. Cada uno de ellos hace “una sola cosa”, así que para realizar transformaciones complejas hay que ir concatenando instrucciones sencillas. Esto se hace con el operador pipeline (%>%)

Pipeline (%>%) (Ctrl + Shift + M)

El pipeline es un simbolo operatorio %>% que sirve para realizar varias operaciones de forma secuencial sin recurrir a parentesis anidados o a sobrescribir muúltiples bases de datos.

Para ver como funciona esto como un vector, supongamos que se tiene una variable a la cual se quiere primero obtener su logaritmo, luego su raíz cuadrada y finalmente su promedio con dos cifras significativas. Para realizar esto se debe seguir lo siguiente:

x <- c(1, 4, 6, 8)
y <- round(mean(sqrt(log(x))), 2)
y
## [1] 0.99

Si se utiliza pipeline, el código sería mucho más ordenado. En ese caso, se partiría por el objeto a procesar y luego cada una de las funciones con sus argumentos si es necesario:

x <- c(1, 4, 6, 8)
y<-x %>% log() %>% sqrt() %>% mean() %>% round(2)
y
## [1] 0.99

Principales funciones de dplyr Hay 6-7 principales.

  • filter() : permite seleccionar filas (que cumplen una o varias condiciones)

  • arrange(): reordena las filas (arrange()).

  • rename() : cambia los nombres de las columnas (variables)

  • select() : selecciona columnas (variables)

  • mutate() : crea nuevas variables

  • summarise() : resume (colapsa) unos cuantos valores a uno sólo. Por ejemplo, calcula la media, moda, etc… de un conjunto de valores Hay una séptima:

  • group_by() : permite agrupar filas en función de una o varias condiciones

  • count() : contar

Primera Función: filter (Filtrar por valores de una variable)

Esta función permite seleccionar filas que cumplen con ciertas condiciones, como tener un valor mayor a un umbral o pertenecer a cierta clase

gapminder %>% filter(year == 2007) # 1ERA FORMA
## # A tibble: 142 × 6
##    country     continent  year lifeExp       pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>     <int>     <dbl>
##  1 Afghanistan Asia       2007    43.8  31889923      975.
##  2 Albania     Europe     2007    76.4   3600523     5937.
##  3 Algeria     Africa     2007    72.3  33333216     6223.
##  4 Angola      Africa     2007    42.7  12420476     4797.
##  5 Argentina   Americas   2007    75.3  40301927    12779.
##  6 Australia   Oceania    2007    81.2  20434176    34435.
##  7 Austria     Europe     2007    79.8   8199783    36126.
##  8 Bahrain     Asia       2007    75.6    708573    29796.
##  9 Bangladesh  Asia       2007    64.1 150448339     1391.
## 10 Belgium     Europe     2007    79.4  10392226    33693.
## # ℹ 132 more rows
gapminder %>% filter(year == 2007)%>%DT::datatable()

Todos los datos excepto 2007

todomenos2007 <-gapminder %>% filter(year!=2007)  
filter(gapminder, year==2007)# 2da forma
## # A tibble: 142 × 6
##    country     continent  year lifeExp       pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>     <int>     <dbl>
##  1 Afghanistan Asia       2007    43.8  31889923      975.
##  2 Albania     Europe     2007    76.4   3600523     5937.
##  3 Algeria     Africa     2007    72.3  33333216     6223.
##  4 Angola      Africa     2007    42.7  12420476     4797.
##  5 Argentina   Americas   2007    75.3  40301927    12779.
##  6 Australia   Oceania    2007    81.2  20434176    34435.
##  7 Austria     Europe     2007    79.8   8199783    36126.
##  8 Bahrain     Asia       2007    75.6    708573    29796.
##  9 Bangladesh  Asia       2007    64.1 150448339     1391.
## 10 Belgium     Europe     2007    79.4  10392226    33693.
## # ℹ 132 more rows
gapminder %>% filter(country == "United States")
## # A tibble: 12 × 6
##    country       continent  year lifeExp       pop gdpPercap
##    <fct>         <fct>     <int>   <dbl>     <int>     <dbl>
##  1 United States Americas   1952    68.4 157553000    13990.
##  2 United States Americas   1957    69.5 171984000    14847.
##  3 United States Americas   1962    70.2 186538000    16173.
##  4 United States Americas   1967    70.8 198712000    19530.
##  5 United States Americas   1972    71.3 209896000    21806.
##  6 United States Americas   1977    73.4 220239000    24073.
##  7 United States Americas   1982    74.6 232187835    25010.
##  8 United States Americas   1987    75.0 242803533    29884.
##  9 United States Americas   1992    76.1 256894189    32004.
## 10 United States Americas   1997    76.8 272911760    35767.
## 11 United States Americas   2002    77.3 287675526    39097.
## 12 United States Americas   2007    78.2 301139947    42952.
# Escriba su código
# Escriba su código
gapminder %>% filter(country == "United States", year == 2007)
## # A tibble: 1 × 6
##   country       continent  year lifeExp       pop gdpPercap
##   <fct>         <fct>     <int>   <dbl>     <int>     <dbl>
## 1 United States Americas   2007    78.2 301139947    42952.

Continente “Americas” expectactiva de vida mayor o igual a 50 y población menor o igual 15000000

gapminder%>%filter(continent=="Americas", lifeExp>=50, pop <=15000000 )
## # A tibble: 188 × 6
##    country continent  year lifeExp      pop gdpPercap
##    <fct>   <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Bolivia Americas   1977    50.0  5079716     3548.
##  2 Bolivia Americas   1982    53.9  5642224     3157.
##  3 Bolivia Americas   1987    57.3  6156369     2754.
##  4 Bolivia Americas   1992    60.0  6893451     2962.
##  5 Bolivia Americas   1997    62.0  7693188     3326.
##  6 Bolivia Americas   2002    63.9  8445134     3413.
##  7 Bolivia Americas   2007    65.6  9119152     3822.
##  8 Canada  Americas   1952    68.8 14785584    11367.
##  9 Chile   Americas   1952    54.7  6377619     3940.
## 10 Chile   Americas   1957    56.1  7048426     4316.
## # ℹ 178 more rows

Ejercicio: filtar que solo aparezcan los continentes Ameerica y Asia

gapminder %>% filter(country == "Colombia", year == 2002)
## # A tibble: 1 × 6
##   country  continent  year lifeExp      pop gdpPercap
##   <fct>    <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Colombia Americas   2002    71.7 41008227     5755.
gapminder %>% filter(lifeExp>=30,lifeExp<=48)
## # A tibble: 414 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1957    30.3  9240934      821.
##  2 Afghanistan Asia       1962    32.0 10267083      853.
##  3 Afghanistan Asia       1967    34.0 11537966      836.
##  4 Afghanistan Asia       1972    36.1 13079460      740.
##  5 Afghanistan Asia       1977    38.4 14880372      786.
##  6 Afghanistan Asia       1982    39.9 12881816      978.
##  7 Afghanistan Asia       1987    40.8 13867957      852.
##  8 Afghanistan Asia       1992    41.7 16317921      649.
##  9 Afghanistan Asia       1997    41.8 22227415      635.
## 10 Afghanistan Asia       2002    42.1 25268405      727.
## # ℹ 404 more rows
#Escriba su código
# Escriba su código

Segunda Función: arrange

(Ordenar de acuerdo a valores en una variable)

gapminder %>% arrange(gdpPercap)
## # A tibble: 1,704 × 6
##    country          continent  year lifeExp      pop gdpPercap
##    <fct>            <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Congo, Dem. Rep. Africa     2002    45.0 55379852      241.
##  2 Congo, Dem. Rep. Africa     2007    46.5 64606759      278.
##  3 Lesotho          Africa     1952    42.1   748747      299.
##  4 Guinea-Bissau    Africa     1952    32.5   580653      300.
##  5 Congo, Dem. Rep. Africa     1997    42.6 47798986      312.
##  6 Eritrea          Africa     1952    35.9  1438760      329.
##  7 Myanmar          Asia       1952    36.3 20092996      331 
##  8 Lesotho          Africa     1957    45.0   813338      336.
##  9 Burundi          Africa     1952    39.0  2445618      339.
## 10 Eritrea          Africa     1957    38.0  1542611      344.
## # ℹ 1,694 more rows
gapminder %>% arrange(desc(gdpPercap))
## # A tibble: 1,704 × 6
##    country   continent  year lifeExp     pop gdpPercap
##    <fct>     <fct>     <int>   <dbl>   <int>     <dbl>
##  1 Kuwait    Asia       1957    58.0  212846   113523.
##  2 Kuwait    Asia       1972    67.7  841934   109348.
##  3 Kuwait    Asia       1952    55.6  160000   108382.
##  4 Kuwait    Asia       1962    60.5  358266    95458.
##  5 Kuwait    Asia       1967    64.6  575003    80895.
##  6 Kuwait    Asia       1977    69.3 1140357    59265.
##  7 Norway    Europe     2007    80.2 4627926    49357.
##  8 Kuwait    Asia       2007    77.6 2505559    47307.
##  9 Singapore Asia       2007    80.0 4553009    47143.
## 10 Norway    Europe     2002    79.0 4535591    44684.
## # ℹ 1,694 more rows
gapminder%>% arrange(desc(lifeExp))
## # A tibble: 1,704 × 6
##    country          continent  year lifeExp       pop gdpPercap
##    <fct>            <fct>     <int>   <dbl>     <int>     <dbl>
##  1 Japan            Asia       2007    82.6 127467972    31656.
##  2 Hong Kong, China Asia       2007    82.2   6980412    39725.
##  3 Japan            Asia       2002    82   127065841    28605.
##  4 Iceland          Europe     2007    81.8    301931    36181.
##  5 Switzerland      Europe     2007    81.7   7554661    37506.
##  6 Hong Kong, China Asia       2002    81.5   6762476    30209.
##  7 Australia        Oceania    2007    81.2  20434176    34435.
##  8 Spain            Europe     2007    80.9  40448191    28821.
##  9 Sweden           Europe     2007    80.9   9031088    33860.
## 10 Israel           Asia       2007    80.7   6426679    25523.
## # ℹ 1,694 more rows
gapminder %>% arrange(desc(gdpPercap)) %>% print(n = 20)
## # A tibble: 1,704 × 6
##    country          continent  year lifeExp       pop gdpPercap
##    <fct>            <fct>     <int>   <dbl>     <int>     <dbl>
##  1 Kuwait           Asia       1957    58.0    212846   113523.
##  2 Kuwait           Asia       1972    67.7    841934   109348.
##  3 Kuwait           Asia       1952    55.6    160000   108382.
##  4 Kuwait           Asia       1962    60.5    358266    95458.
##  5 Kuwait           Asia       1967    64.6    575003    80895.
##  6 Kuwait           Asia       1977    69.3   1140357    59265.
##  7 Norway           Europe     2007    80.2   4627926    49357.
##  8 Kuwait           Asia       2007    77.6   2505559    47307.
##  9 Singapore        Asia       2007    80.0   4553009    47143.
## 10 Norway           Europe     2002    79.0   4535591    44684.
## 11 United States    Americas   2007    78.2 301139947    42952.
## 12 Norway           Europe     1997    78.3   4405672    41283.
## 13 Ireland          Europe     2007    78.9   4109086    40676.
## 14 Kuwait           Asia       1997    76.2   1765345    40301.
## 15 Hong Kong, China Asia       2007    82.2   6980412    39725.
## 16 United States    Americas   2002    77.3 287675526    39097.
## 17 Switzerland      Europe     2007    81.7   7554661    37506.
## 18 Netherlands      Europe     2007    79.8  16570613    36798.
## 19 Canada           Americas   2007    80.7  33390141    36319.
## 20 Iceland          Europe     2007    81.8    301931    36181.
## # ℹ 1,684 more rows

GDP (PIB) del año 2007 en orden descendiente.

RECOMENDACIÓN para estas tareas: Siempre de la grande a la pequeña:

gapminder %>% filter(year == 2007) %>% arrange(gdpPercap) %>% print(n=100)
## # A tibble: 142 × 6
##     country                  continent  year lifeExp        pop gdpPercap
##     <fct>                    <fct>     <int>   <dbl>      <int>     <dbl>
##   1 Congo, Dem. Rep.         Africa     2007    46.5   64606759      278.
##   2 Liberia                  Africa     2007    45.7    3193942      415.
##   3 Burundi                  Africa     2007    49.6    8390505      430.
##   4 Zimbabwe                 Africa     2007    43.5   12311143      470.
##   5 Guinea-Bissau            Africa     2007    46.4    1472041      579.
##   6 Niger                    Africa     2007    56.9   12894865      620.
##   7 Eritrea                  Africa     2007    58.0    4906585      641.
##   8 Ethiopia                 Africa     2007    52.9   76511887      691.
##   9 Central African Republic Africa     2007    44.7    4369038      706.
##  10 Gambia                   Africa     2007    59.4    1688359      753.
##  11 Malawi                   Africa     2007    48.3   13327079      759.
##  12 Mozambique               Africa     2007    42.1   19951656      824.
##  13 Sierra Leone             Africa     2007    42.6    6144562      863.
##  14 Rwanda                   Africa     2007    46.2    8860588      863.
##  15 Togo                     Africa     2007    58.4    5701579      883.
##  16 Somalia                  Africa     2007    48.2    9118773      926.
##  17 Guinea                   Africa     2007    56.0    9947814      943.
##  18 Myanmar                  Asia       2007    62.1   47761980      944 
##  19 Afghanistan              Asia       2007    43.8   31889923      975.
##  20 Comoros                  Africa     2007    65.2     710960      986.
##  21 Mali                     Africa     2007    54.5   12031795     1043.
##  22 Madagascar               Africa     2007    59.4   19167654     1045.
##  23 Uganda                   Africa     2007    51.5   29170398     1056.
##  24 Nepal                    Asia       2007    63.8   28901790     1091.
##  25 Tanzania                 Africa     2007    52.5   38139640     1107.
##  26 Haiti                    Americas   2007    60.9    8502814     1202.
##  27 Burkina Faso             Africa     2007    52.3   14326203     1217.
##  28 Zambia                   Africa     2007    42.4   11746035     1271.
##  29 Ghana                    Africa     2007    60.0   22873338     1328.
##  30 Bangladesh               Asia       2007    64.1  150448339     1391.
##  31 Benin                    Africa     2007    56.7    8078314     1441.
##  32 Kenya                    Africa     2007    54.1   35610177     1463.
##  33 Cote d'Ivoire            Africa     2007    48.3   18013409     1545.
##  34 Lesotho                  Africa     2007    42.6    2012649     1569.
##  35 Korea, Dem. Rep.         Asia       2007    67.3   23301725     1593.
##  36 Sao Tome and Principe    Africa     2007    65.5     199579     1598.
##  37 Chad                     Africa     2007    50.7   10238807     1704.
##  38 Senegal                  Africa     2007    63.1   12267493     1712.
##  39 Cambodia                 Asia       2007    59.7   14131858     1714.
##  40 Mauritania               Africa     2007    64.2    3270065     1803.
##  41 Nigeria                  Africa     2007    46.9  135031164     2014.
##  42 Cameroon                 Africa     2007    50.4   17696293     2042.
##  43 Djibouti                 Africa     2007    54.8     496374     2082.
##  44 Yemen, Rep.              Asia       2007    62.7   22211743     2281.
##  45 Vietnam                  Asia       2007    74.2   85262356     2442.
##  46 India                    Asia       2007    64.7 1110396331     2452.
##  47 Sudan                    Africa     2007    58.6   42292929     2602.
##  48 Pakistan                 Asia       2007    65.5  169270617     2606.
##  49 Nicaragua                Americas   2007    72.9    5675356     2749.
##  50 West Bank and Gaza       Asia       2007    73.4    4018332     3025.
##  51 Mongolia                 Asia       2007    66.8    2874127     3096.
##  52 Philippines              Asia       2007    71.7   91077287     3190.
##  53 Indonesia                Asia       2007    70.6  223547000     3541.
##  54 Honduras                 Americas   2007    70.2    7483763     3548.
##  55 Congo, Rep.              Africa     2007    55.3    3800610     3633.
##  56 Morocco                  Africa     2007    71.2   33757175     3820.
##  57 Bolivia                  Americas   2007    65.6    9119152     3822.
##  58 Sri Lanka                Asia       2007    72.4   20378239     3970.
##  59 Paraguay                 Americas   2007    71.8    6667147     4173.
##  60 Syria                    Asia       2007    74.1   19314747     4185.
##  61 Iraq                     Asia       2007    59.5   27499638     4471.
##  62 Swaziland                Africa     2007    39.6    1133066     4513.
##  63 Jordan                   Asia       2007    72.5    6053193     4519.
##  64 Angola                   Africa     2007    42.7   12420476     4797.
##  65 Namibia                  Africa     2007    52.9    2055080     4811.
##  66 China                    Asia       2007    73.0 1318683096     4959.
##  67 Guatemala                Americas   2007    70.3   12572928     5186.
##  68 Egypt                    Africa     2007    71.3   80264543     5581.
##  69 El Salvador              Americas   2007    71.9    6939688     5728.
##  70 Albania                  Europe     2007    76.4    3600523     5937.
##  71 Dominican Republic       Americas   2007    72.2    9319622     6025.
##  72 Algeria                  Africa     2007    72.3   33333216     6223.
##  73 Ecuador                  Americas   2007    75.0   13755680     6873.
##  74 Colombia                 Americas   2007    72.9   44227550     7007.
##  75 Tunisia                  Africa     2007    73.9   10276158     7093.
##  76 Jamaica                  Americas   2007    72.6    2780132     7321.
##  77 Peru                     Americas   2007    71.4   28674757     7409.
##  78 Bosnia and Herzegovina   Europe     2007    74.9    4552198     7446.
##  79 Thailand                 Asia       2007    70.6   65068149     7458.
##  80 Reunion                  Africa     2007    76.4     798094     7670.
##  81 Turkey                   Europe     2007    71.8   71158647     8458.
##  82 Cuba                     Americas   2007    78.3   11416987     8948.
##  83 Brazil                   Americas   2007    72.4  190010647     9066.
##  84 Montenegro               Europe     2007    74.5     684736     9254.
##  85 South Africa             Africa     2007    49.3   43997828     9270.
##  86 Costa Rica               Americas   2007    78.8    4133884     9645.
##  87 Serbia                   Europe     2007    74.0   10150265     9787.
##  88 Panama                   Americas   2007    75.5    3242173     9809.
##  89 Lebanon                  Asia       2007    72.0    3921278    10461.
##  90 Uruguay                  Americas   2007    76.4    3447496    10611.
##  91 Bulgaria                 Europe     2007    73.0    7322858    10681.
##  92 Romania                  Europe     2007    72.5   22276056    10808.
##  93 Mauritius                Africa     2007    72.8    1250882    10957.
##  94 Venezuela                Americas   2007    73.7   26084662    11416.
##  95 Iran                     Asia       2007    71.0   69453570    11606.
##  96 Mexico                   Americas   2007    76.2  108700891    11978.
##  97 Libya                    Africa     2007    74.0    6036914    12057.
##  98 Equatorial Guinea        Africa     2007    51.6     551201    12154.
##  99 Malaysia                 Asia       2007    74.2   24821286    12452.
## 100 Botswana                 Africa     2007    50.7    1639131    12570.
## # ℹ 42 more rows

Tercera Funcion: Select (columnas)

Esta función permite seleccionar las variables a utilizar dado que en muchos casos nos encontraremos con bases de datos con demasiadas variables y por lo tanto, se querrá reducirlas para solo trabajar en una tabla con las variables necesarias.

gapminder %>% select(pop)
## # A tibble: 1,704 × 1
##         pop
##       <int>
##  1  8425333
##  2  9240934
##  3 10267083
##  4 11537966
##  5 13079460
##  6 14880372
##  7 12881816
##  8 13867957
##  9 16317921
## 10 22227415
## # ℹ 1,694 more rows
select(gapminder, pop)  # segunda forma
## # A tibble: 1,704 × 1
##         pop
##       <int>
##  1  8425333
##  2  9240934
##  3 10267083
##  4 11537966
##  5 13079460
##  6 14880372
##  7 12881816
##  8 13867957
##  9 16317921
## 10 22227415
## # ℹ 1,694 more rows

Seleccionar dos variables(columnas)

gapminder %>% select(pop, year)
## # A tibble: 1,704 × 2
##         pop  year
##       <int> <int>
##  1  8425333  1952
##  2  9240934  1957
##  3 10267083  1962
##  4 11537966  1967
##  5 13079460  1972
##  6 14880372  1977
##  7 12881816  1982
##  8 13867957  1987
##  9 16317921  1992
## 10 22227415  1997
## # ℹ 1,694 more rows
gapminder %>% select( -c(year, pop)) # QUITAR LAS COLUMNAS DE Year y pob
## # A tibble: 1,704 × 4
##    country     continent lifeExp gdpPercap
##    <fct>       <fct>       <dbl>     <dbl>
##  1 Afghanistan Asia         28.8      779.
##  2 Afghanistan Asia         30.3      821.
##  3 Afghanistan Asia         32.0      853.
##  4 Afghanistan Asia         34.0      836.
##  5 Afghanistan Asia         36.1      740.
##  6 Afghanistan Asia         38.4      786.
##  7 Afghanistan Asia         39.9      978.
##  8 Afghanistan Asia         40.8      852.
##  9 Afghanistan Asia         41.7      649.
## 10 Afghanistan Asia         41.8      635.
## # ℹ 1,694 more rows
gapminder %>% select(continent:pop ) %>% filter(year==2007) %>% arrange(pop) %>% print(n=30)
## # A tibble: 142 × 4
##    continent  year lifeExp     pop
##    <fct>     <int>   <dbl>   <int>
##  1 Africa     2007    65.5  199579
##  2 Europe     2007    81.8  301931
##  3 Africa     2007    54.8  496374
##  4 Africa     2007    51.6  551201
##  5 Europe     2007    74.5  684736
##  6 Asia       2007    75.6  708573
##  7 Africa     2007    65.2  710960
##  8 Africa     2007    76.4  798094
##  9 Americas   2007    69.8 1056608
## 10 Africa     2007    39.6 1133066
## 11 Africa     2007    72.8 1250882
## 12 Africa     2007    56.7 1454867
## 13 Africa     2007    46.4 1472041
## 14 Africa     2007    50.7 1639131
## 15 Africa     2007    59.4 1688359
## 16 Europe     2007    77.9 2009245
## 17 Africa     2007    42.6 2012649
## 18 Africa     2007    52.9 2055080
## 19 Asia       2007    77.6 2505559
## 20 Americas   2007    72.6 2780132
## 21 Asia       2007    66.8 2874127
## 22 Africa     2007    45.7 3193942
## 23 Asia       2007    75.6 3204897
## 24 Americas   2007    75.5 3242173
## 25 Africa     2007    64.2 3270065
## 26 Americas   2007    76.4 3447496
## 27 Europe     2007    76.4 3600523
## 28 Africa     2007    55.3 3800610
## 29 Asia       2007    72.0 3921278
## 30 Americas   2007    78.7 3942491
## # ℹ 112 more rows

Cuarta Función: mutate (Agregar o cambia variables) agregar columnas

La función mutate permite ir creando nuevas columnas, que serán resultado de aplicar una función. Por tanto, dentro de la función de mutate, siempre deberemos tener dos elementos: el nombre de la nueva columna y la función que tendrá como resultado los valores de la columna.

gapminder %>% mutate(PIB=gdpPercap*pop)
## # A tibble: 1,704 × 7
##    country     continent  year lifeExp      pop gdpPercap          PIB
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>        <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.  6567086330.
##  2 Afghanistan Asia       1957    30.3  9240934      821.  7585448670.
##  3 Afghanistan Asia       1962    32.0 10267083      853.  8758855797.
##  4 Afghanistan Asia       1967    34.0 11537966      836.  9648014150.
##  5 Afghanistan Asia       1972    36.1 13079460      740.  9678553274.
##  6 Afghanistan Asia       1977    38.4 14880372      786. 11697659231.
##  7 Afghanistan Asia       1982    39.9 12881816      978. 12598563401.
##  8 Afghanistan Asia       1987    40.8 13867957      852. 11820990309.
##  9 Afghanistan Asia       1992    41.7 16317921      649. 10595901589.
## 10 Afghanistan Asia       1997    41.8 22227415      635. 14121995875.
## # ℹ 1,694 more rows
gapminder %>% mutate(population= pop/1000000)
## # A tibble: 1,704 × 7
##    country     continent  year lifeExp      pop gdpPercap population
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>      <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.       8.43
##  2 Afghanistan Asia       1957    30.3  9240934      821.       9.24
##  3 Afghanistan Asia       1962    32.0 10267083      853.      10.3 
##  4 Afghanistan Asia       1967    34.0 11537966      836.      11.5 
##  5 Afghanistan Asia       1972    36.1 13079460      740.      13.1 
##  6 Afghanistan Asia       1977    38.4 14880372      786.      14.9 
##  7 Afghanistan Asia       1982    39.9 12881816      978.      12.9 
##  8 Afghanistan Asia       1987    40.8 13867957      852.      13.9 
##  9 Afghanistan Asia       1992    41.7 16317921      649.      16.3 
## 10 Afghanistan Asia       1997    41.8 22227415      635.      22.2 
## # ℹ 1,694 more rows
gapminder %>% mutate(pop= pop/1000000)
## # A tibble: 1,704 × 6
##    country     continent  year lifeExp   pop gdpPercap
##    <fct>       <fct>     <int>   <dbl> <dbl>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8.43      779.
##  2 Afghanistan Asia       1957    30.3  9.24      821.
##  3 Afghanistan Asia       1962    32.0 10.3       853.
##  4 Afghanistan Asia       1967    34.0 11.5       836.
##  5 Afghanistan Asia       1972    36.1 13.1       740.
##  6 Afghanistan Asia       1977    38.4 14.9       786.
##  7 Afghanistan Asia       1982    39.9 12.9       978.
##  8 Afghanistan Asia       1987    40.8 13.9       852.
##  9 Afghanistan Asia       1992    41.7 16.3       649.
## 10 Afghanistan Asia       1997    41.8 22.2       635.
## # ℹ 1,694 more rows

Quinta Función: summarise (Crear resumenes de información)

gapminder %>% summarise(medianLifeExp=median(lifeExp))
## # A tibble: 1 × 1
##   medianLifeExp
##           <dbl>
## 1          60.7
gapminder %>% summarize(expectativamediadevida=mean(lifeExp))
## # A tibble: 1 × 1
##   expectativamediadevida
##                    <dbl>
## 1                   59.5

-Por ejemplo: Expectativas de vida

gapminder %>% summarize(medianLifeExp=median(lifeExp),meanLifeExp=mean(lifeExp))
## # A tibble: 1 × 2
##   medianLifeExp meanLifeExp
##           <dbl>       <dbl>
## 1          60.7        59.5
gapminder %>% summarize(medianLifeExp=median(lifeExp),meanLifeExp=mean(lifeExp), maximo=max(lifeExp), minimo=min(lifeExp), desviacionestandar
                        =sd(lifeExp), maxipop=max(pop))
## # A tibble: 1 × 6
##   medianLifeExp meanLifeExp maximo minimo desviacionestandar    maxipop
##           <dbl>       <dbl>  <dbl>  <dbl>              <dbl>      <int>
## 1          60.7        59.5   82.6   23.6               12.9 1318683096

Nuevamente, cuando se mezclan las funciones, se eleva enormemente la capacidad de la herramienta

-Ejemplo: Expectativa de vida mediana en 1957

gapminder %>% filter(year ==1957) %>% summarize(medianLifeExp=median(lifeExp))
## # A tibble: 1 × 1
##   medianLifeExp
##           <dbl>
## 1          48.4
gapminder %>% filter(year == 1957) %>% summarize(medianLifeExp=median(lifeExp),maxGdpPercap=max(gdpPercap))
## # A tibble: 1 × 2
##   medianLifeExp maxGdpPercap
##           <dbl>        <dbl>
## 1          48.4      113523.

Sexta Función: gruop_by

(Hacer resumenes pero por grupos, no por toda la base de datos)

gapminder %>% group_by(year) %>% summarize(medianLifeExp=median(lifeExp),maxGdpPercap=max(gdpPercap))
## # A tibble: 12 × 3
##     year medianLifeExp maxGdpPercap
##    <int>         <dbl>        <dbl>
##  1  1952          45.1      108382.
##  2  1957          48.4      113523.
##  3  1962          50.9       95458.
##  4  1967          53.8       80895.
##  5  1972          56.5      109348.
##  6  1977          59.7       59265.
##  7  1982          62.4       33693.
##  8  1987          65.8       31541.
##  9  1992          67.7       34933.
## 10  1997          69.4       41283.
## 11  2002          70.8       44684.
## 12  2007          71.9       49357.
gapminder %>% filter(year==2007) %>% group_by(continent) %>% summarize(medianLifeExp=median(lifeExp),maxGdpPercap=max(gdpPercap))
## # A tibble: 5 × 3
##   continent medianLifeExp maxGdpPercap
##   <fct>             <dbl>        <dbl>
## 1 Africa             52.9       13206.
## 2 Americas           72.9       42952.
## 3 Asia               72.4       47307.
## 4 Europe             78.6       49357.
## 5 Oceania            80.7       34435.

-Ejemplo: Mediana de expectativa de vida y GDP máximo por año y continente

gapminder %>% group_by(continent,year) %>% summarize(medianLifeExp=median(lifeExp),maxGdpPercap=max(gdpPercap))
## `summarise()` has grouped output by 'continent'. You can override using the
## `.groups` argument.
## # A tibble: 60 × 4
## # Groups:   continent [5]
##    continent  year medianLifeExp maxGdpPercap
##    <fct>     <int>         <dbl>        <dbl>
##  1 Africa     1952          38.8        4725.
##  2 Africa     1957          40.6        5487.
##  3 Africa     1962          42.6        6757.
##  4 Africa     1967          44.7       18773.
##  5 Africa     1972          47.0       21011.
##  6 Africa     1977          49.3       21951.
##  7 Africa     1982          50.8       17364.
##  8 Africa     1987          51.6       11864.
##  9 Africa     1992          52.4       13522.
## 10 Africa     1997          52.8       14723.
## # ℹ 50 more rows
gapminder %>% group_by(year) %>% summarize(meanLifeExp=mean(lifeExp),totalPop=sum(pop))
## # A tibble: 12 × 3
##     year meanLifeExp   totalPop
##    <int>       <dbl>      <dbl>
##  1  1952        49.1 2406957150
##  2  1957        51.5 2664404580
##  3  1962        53.6 2899782974
##  4  1967        55.7 3217478384
##  5  1972        57.6 3576977158
##  6  1977        59.6 3930045807
##  7  1982        61.5 4289436840
##  8  1987        63.2 4691477418
##  9  1992        64.2 5110710260
## 10  1997        65.0 5515204472
## 11  2002        65.7 5886977579
## 12  2007        67.0 6251013179
gapminder %>% filter(year==2007) %>% group_by(continent) %>% summarize(meanLifeExp=mean(lifeExp),totalPop=sum(pop))
## # A tibble: 5 × 3
##   continent meanLifeExp   totalPop
##   <fct>           <dbl>      <dbl>
## 1 Africa           54.8  929539692
## 2 Americas         73.6  898871184
## 3 Asia             70.7 3811953827
## 4 Europe           77.6  586098529
## 5 Oceania          80.7   24549947
gapminder %>% group_by(year, continent) %>% summarise(meanLifeExp=mean(lifeExp)) %>% print(n = 60)
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
## # A tibble: 60 × 3
## # Groups:   year [12]
##     year continent meanLifeExp
##    <int> <fct>           <dbl>
##  1  1952 Africa           39.1
##  2  1952 Americas         53.3
##  3  1952 Asia             46.3
##  4  1952 Europe           64.4
##  5  1952 Oceania          69.3
##  6  1957 Africa           41.3
##  7  1957 Americas         56.0
##  8  1957 Asia             49.3
##  9  1957 Europe           66.7
## 10  1957 Oceania          70.3
## 11  1962 Africa           43.3
## 12  1962 Americas         58.4
## 13  1962 Asia             51.6
## 14  1962 Europe           68.5
## 15  1962 Oceania          71.1
## 16  1967 Africa           45.3
## 17  1967 Americas         60.4
## 18  1967 Asia             54.7
## 19  1967 Europe           69.7
## 20  1967 Oceania          71.3
## 21  1972 Africa           47.5
## 22  1972 Americas         62.4
## 23  1972 Asia             57.3
## 24  1972 Europe           70.8
## 25  1972 Oceania          71.9
## 26  1977 Africa           49.6
## 27  1977 Americas         64.4
## 28  1977 Asia             59.6
## 29  1977 Europe           71.9
## 30  1977 Oceania          72.9
## 31  1982 Africa           51.6
## 32  1982 Americas         66.2
## 33  1982 Asia             62.6
## 34  1982 Europe           72.8
## 35  1982 Oceania          74.3
## 36  1987 Africa           53.3
## 37  1987 Americas         68.1
## 38  1987 Asia             64.9
## 39  1987 Europe           73.6
## 40  1987 Oceania          75.3
## 41  1992 Africa           53.6
## 42  1992 Americas         69.6
## 43  1992 Asia             66.5
## 44  1992 Europe           74.4
## 45  1992 Oceania          76.9
## 46  1997 Africa           53.6
## 47  1997 Americas         71.2
## 48  1997 Asia             68.0
## 49  1997 Europe           75.5
## 50  1997 Oceania          78.2
## 51  2002 Africa           53.3
## 52  2002 Americas         72.4
## 53  2002 Asia             69.2
## 54  2002 Europe           76.7
## 55  2002 Oceania          79.7
## 56  2007 Africa           54.8
## 57  2007 Americas         73.6
## 58  2007 Asia             70.7
## 59  2007 Europe           77.6
## 60  2007 Oceania          80.7

by_year_continent <- gapminder %>% 
  group_by(year, continent) %>% 
  summarize(medianGdpPercap=median(gdpPercap))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.

Función rename()

Renombrar una variable en un data frame es sorprendentemente en R muy difícil de realizar. La función rename() esta diseñada para hacer este proceso de una forma más fácil.

names(gapminder)
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"
gapminder %>% rename(año=year, pais=country)
## # A tibble: 1,704 × 6
##    pais        continent   año lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## # ℹ 1,694 more rows