SESIÓN 3 - TABLAS DESDE LA WEB

LINKGUERRAS = "https://en.wikipedia.org/wiki/List_of_wars_by_death_toll"

library(htmltab)
ancient = htmltab(doc = LINKGUERRAS,
                  which = '//*[@id="mw-content-text"]/div/table[4]')
medieval = htmltab(doc = LINKGUERRAS,
                   which = '//*[@id="mw-content-text"]/div/table[6]')
## Warning: Columns [Notes] seem to have no data and are removed. Use
## rm_nodata_cols = F to suppress this behavior
modern = htmltab(doc = LINKGUERRAS,
                 which = '//*[@id="mw-content-text"]/div/table[7]')
## Warning: Columns [Notes] seem to have no data and are removed. Use
## rm_nodata_cols = F to suppress this behavior
names(ancient)
## [1] "War"                   "Deathrange"            "Geometricmean(Note 1)"
## [4] "Date"                  "Combatants"            "Location"             
## [7] "Notes"
names(medieval)
## [1] "War"           "Deathrange"    "Geometricmean" "Date"         
## [5] "Combatants"    "Location"
names(modern)
## [1] "War"           "Deathrange"    "Geometricmean" "Date"         
## [5] "Comabatants"   "Location"
guerras1=ancient[,c("War","Location")]
guerras2=medieval[,c("War","Location")]
guerras3=modern[,c("War","Location")]

guerras=rbind(guerras1,guerras2,guerras3)

str(guerras)
## 'data.frame':    162 obs. of  2 variables:
##  $ War     : chr  "Conquests of Cyrus the Great" "Greco–Persian Wars" "Samnite Wars" "Wars of Alexander the Great" ...
##  $ Location: chr  "Middle East" "Greece" "Italy" "Middle East / North Africa / Central Asia / India" ...
table(guerras$Location)
## 
##                                       Afghanistan 
##                                                 3 
##                                           Algeria 
##                                                 3 
##                                          Americas 
##                                                 1 
##                                            Angola 
##                                                 2 
##                                  Balkan Peninsula 
##                                                 1 
##                                        Bangladesh 
##                                                 1 
##                                            Bosnia 
##                                                 1 
##                                     British Isles 
##                                                 1 
##                                           Burundi 
##                                                 1 
##                                   Caucasus region 
##                                                 1 
##                                    Central Africa 
##                                                 2 
##                                             China 
##                                                 9 
##                                  China /  Vietnam 
##                                                 1 
##                                          Colombia 
##                                                 3 
##                                             Congo 
##                                                 4 
##                                 Crimean Peninsula 
##                                                 1 
##                                              Cuba 
##                                                 2 
##                                         East Asia 
##                                                 1 
##                                    Eastern Europe 
##                                                 4 
##       Eastern Europe / Middle East / North Africa 
##                                                 1 
##                                           England 
##                                                 2 
##                                   England / Wales 
##                                                 1 
##                                          Ethiopia 
##                                                 2 
##                                           Eurasia 
##                                                 2 
##                                            Europe 
##                                                 4 
##                                 Europe / Americas 
##                                                 2 
##                Europe / Middle East ("Holy Land") 
##                                                 1 
##                                           Finland 
##                                                 1 
##                                            France 
##                                                 3 
##                                           Germany 
##                                                 1 
##                                            Greece 
##                                                 2 
##                                             Haiti 
##                                                 1 
##                                 Iberian Peninsula 
##                                                 1 
##                                             India 
##                                                 4 
##                                  India-Bangladesh 
##                                                 1 
##                                         Indonesia 
##                                                 1 
##                                              Iraq 
##                                                 5 
##                                             Italy 
##                                                 1 
##                                             Korea 
##                                                 3 
##                                              Laos 
##                                                 1 
##                                           Lebanon 
##                                                 1 
##                                             Libya 
##                                                 1 
##                                        Madagascar 
##                                                 1 
##                                         Manchuria 
##                                                 1 
##                                            Mexico 
##                                                 4 
##                                       Middle East 
##                                                 7 
##                        Middle East / North Africa 
##                                                 1 
## Middle East / North Africa / Central Asia / India 
##                                                 1 
##      Middle East / North Africa / Southern Europe 
##                                                 1 
##                          Middle East/North Africa 
##                                                 1 
##                                        Mozambique 
##                                                 1 
##                                           Myanmar 
##                                                 1 
##                                        New Guinea 
##                                                 1 
##                                           Nigeria 
##                                                 2 
##                                      North Africa 
##                                                 1 
##                                     North America 
##                                                 1 
##                            North India / Pakistan 
##                                                 1 
##                                   Northeast India 
##                                                 2 
##                                    Northern China 
##                                                 1 
##                                   Northern Europe 
##                                                 1 
##                                          Pakistan 
##                                                 1 
##                                         Patagonia 
##                                                 1 
##                                              Peru 
##                                                 2 
##                                       Philippines 
##                                                 3 
##                                            Russia 
##                                                 2 
##                                            Rwanda 
##                                                 1 
##                                Scotland / England 
##                                                 1 
##                                      Sierra Leone 
##                                                 1 
##                                           Somalia 
##                                                 1 
##                                      South Africa 
##                                                 1 
##                                     South America 
##                                                 1 
##                                    Southeast Asia 
##                                                 2 
##                                  Southeast Europe 
##                                                 1 
##                                   Southern Africa 
##                                                 1 
##                                   Southern Europe 
##                                                 1 
##                    Southern Europe / North Africa 
##                                                 2 
##                                             Spain 
##                                                 2 
##                                         Sri Lanka 
##                                                 1 
##                                             Sudan 
##                                                 3 
##                                             Syria 
##                                                 1 
##                                           Tunisia 
##                                                 1 
##                                            Uganda 
##                                                 1 
##                                               USA 
##                                                 1 
##                                         Venezuela 
##                                                 1 
##                                           Vietnam 
##                                                 1 
##                                    Western Europe 
##                                                 4 
##                     Western Europe / North Africa 
##                                                 2 
##                                         Worldwide 
##                                                 7 
##                                             Yemen 
##                                                 2

también

library(knitr)
dataTable=table(guerras$Location)
kable(dataTable)
Var1 Freq
Afghanistan 3
Algeria 3
Americas 1
Angola 2
Balkan Peninsula 1
Bangladesh 1
Bosnia 1
British Isles 1
Burundi 1
Caucasus region 1
Central Africa 2
China 9
China / Vietnam 1
Colombia 3
Congo 4
Crimean Peninsula 1
Cuba 2
East Asia 1
Eastern Europe 4
Eastern Europe / Middle East / North Africa 1
England 2
England / Wales 1
Ethiopia 2
Eurasia 2
Europe 4
Europe / Americas 2
Europe / Middle East (“Holy Land”) 1
Finland 1
France 3
Germany 1
Greece 2
Haiti 1
Iberian Peninsula 1
India 4
India-Bangladesh 1
Indonesia 1
Iraq 5
Italy 1
Korea 3
Laos 1
Lebanon 1
Libya 1
Madagascar 1
Manchuria 1
Mexico 4
Middle East 7
Middle East / North Africa 1
Middle East / North Africa / Central Asia / India 1
Middle East / North Africa / Southern Europe 1
Middle East/North Africa 1
Mozambique 1
Myanmar 1
New Guinea 1
Nigeria 2
North Africa 1
North America 1
North India / Pakistan 1
Northeast India 2
Northern China 1
Northern Europe 1
Pakistan 1
Patagonia 1
Peru 2
Philippines 3
Russia 2
Rwanda 1
Scotland / England 1
Sierra Leone 1
Somalia 1
South Africa 1
South America 1
Southeast Asia 2
Southeast Europe 1
Southern Africa 1
Southern Europe 1
Southern Europe / North Africa 2
Spain 2
Sri Lanka 1
Sudan 3
Syria 1
Tunisia 1
Uganda 1
USA 1
Venezuela 1
Vietnam 1
Western Europe 4
Western Europe / North Africa 2
Worldwide 7
Yemen 2
library(ggplot2)
base = ggplot(data=guerras,aes(x=Location))
bar1 = base + geom_bar()
bar1 

Con títulos

bar1 = bar1 + labs(x="Pais/zona", 
                   y="Cantidad",
                  title="Ubicacion de las Guerras en la historia", 
                  subtitle = "Solo con mas 25000 muertos",
                  caption = "Fuente: Wikipedia")

bar1

Datos en ángulo

bar1 + theme(axis.text.x = element_text(angle = 90,size=6,hjust = 1))

library(DescTools)
Mode(guerras$Location)
## [1] "China"
library(DescTools)
Herfindahl(dataTable)
## [1] 0.01851852
1/Herfindahl(dataTable)
## [1] 54

Hay 54 locaciones importantes de 89.