#Escuela de Ciencia Política ## Estadística
¿Cómo desargar data de una tabla que está en la web?
Vamos a ver una data sobre muertos en guerras en Wikipedia 1. Leer datos Guarda el link en un objeto de R, aquí el objeto se llama LINKGUERRAS
LINKGUERRAS = "https://en.wikipedia.org/wiki/List_of_wars_by_death_toll"
Visita ese link, y ve con el mouse a cada tabla (son 3). Elije la opcion inspeccionar usando el boton derecho:
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"
¿Quieres combinar los tres en uno solo? Eso se puede hacer si todas las columnas tienen nombres identicos. 3. Sub conjuntos necesitados Quédate con estas columnas
guerras1=ancient[ ,c("War", "Location") ]
guerras2=medieval[ ,c("War", "Location")]
guerras3=modern[ ,c("War", "Location")]
Cada data frame tiene ahora dos columnas cada uno 4. Juntando sus conjuntos Usa la función rbind para poner las tablas una debajo de la otra:
guerras=rbind(guerras1, guerras2, guerras3)
El data frame guerras tiene todo
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" ...
Ahora a analizar la columna Location (la única que vale la pena y es nominal) 5. Análisis estadístico - Tabla
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
table(guerras$War)
##
## 1991 Iraqi coup d'état attempt
## 1
## Aceh War
## 1
## Albigensian Crusade
## 1
## Algerian Civil War
## 1
## Algerian War
## 1
## American Civil War
## 1
## American Revolutionary War
## 1
## An Lushan Rebellion
## 1
## Anglo-Spanish War (1585–1604)
## 1
## Angolan Civil War
## 1
## Angolan War of Independence
## 1
## Arab–Byzantine Wars
## 1
## Arab–Israeli conflict
## 1
## Balkan Wars
## 1
## Bangladesh Liberation War
## 1
## Bar Kokhba Revolt
## 1
## Boko Haram insurgency
## 1
## Bosnian War
## 1
## Burundian Civil War
## 1
## Campaigns of Suleiman the Magnificent
## 1
## Carlist Wars
## 1
## Chinese Civil War
## 1
## Cimbrian War
## 1
## Colombian conflict
## 1
## Congo Crisis
## 1
## Conquest of the Desert
## 1
## Conquests of Cyrus the Great
## 1
## Conquests of Mehmed II 'the Conqueror'
## 1
## Conquests of Timur
## 1
## Continuation War
## 1
## CPP-NPA-NDF rebellion
## 1
## Crimean War
## 1
## Crusades
## 1
## Cuban War of Independence
## 1
## Dungan Revolt
## 1
## Eighty Years' War
## 1
## English Civil War
## 1
## Ethiopian Civil War
## 1
## Ethnic conflict in Nagaland
## 1
## First Congo War
## 1
## First Indochina War
## 1
## First Jewish–Roman War
## 1
## First Punic War
## 1
## First Sino–Japanese War
## 1
## First Sudanese Civil War
## 1
## Franco-Dutch War
## 1
## Franco-Spanish War (1635–59)
## 1
## French campaign in Egypt and Syria
## 1
## French conquest of Algeria
## 1
## French intervention in Mexico
## 1
## French invasion of Russia
## 1
## French Wars of Religion
## 1
## Gallic Wars
## 1
## German Peasants' War
## 1
## Goryeo–Khitan Wars
## 1
## Gothic War (269)
## 1
## Gothic War (376–382)
## 1
## Great Northern War
## 1
## Great Turkish War
## 1
## Greco-Italian War
## 1
## Greco–Persian Wars
## 1
## Greek Civil War
## 1
## Gulf War
## 1
## Hundred Years' War
## 1
## Hunnic Invasions
## 1
## Iceni Revolt
## 1
## Indian annexation of Hyderabad
## 1
## Indian Rebellion of 1857
## 1
## Insurgency in Laos
## 1
## Insurgency in Northeast India
## 1
## Internal conflict in Myanmar
## 1
## Internal conflict in Peru
## 1
## Iran–Iraq War
## 1
## Iraq War
## 1
## Iraqi Civil War (2014–2017)
## 1
## Iraqi–Kurdish conflict
## 1
## Italian Wars
## 1
## Ituri conflict
## 1
## Japanese invasions of Korea
## 1
## Jewish–Roman Wars
## 1
## Kalinga War
## 1
## Kashmir Conflict
## 1
## Kitos War
## 1
## Kivu Conflict
## 1
## Korean War
## 1
## Kurdish rebellions in Turkey
## 1
## Kurdish–Turkish conflict
## 1
## La Violencia
## 1
## Lebanese Civil War
## 1
## Lord's Resistance Army insurgency
## 1
## Malagasy Uprising
## 1
## Maratha expeditions in Bengal
## 1
## Mexican Drug War
## 1
## Mexican Revolution
## 1
## Mfecane
## 1
## Mongol conquests
## 1
## Moorish Wars
## 1
## Moro Conflict
## 1
## Mozambican War of Independence
## 1
## Mughal–Maratha Wars
## 1
## Nagorno-Karabakh War
## 1
## Napoleonic Wars
## 1
## Nigerian Civil War
## 1
## North Yemen Civil War
## 1
## Panthay Rebellion
## 1
## Paraguayan War
## 1
## Philippine–American War
## 1
## Probus's German War
## 1
## Punic Wars
## 1
## Qin's Wars of Unification
## 1
## Reconquista
## 1
## Russian Civil War
## 1
## Rwandan genocide
## 1
## Saint-Domingue expedition
## 1
## Samnite Wars
## 1
## Second Congo War
## 1
## Second Italo-Ethiopian War
## 1
## Second Italo-Senussi War
## 1
## Second Punic War
## 1
## Second Sino-Japanese War
## 1
## Second Sudanese Civil War
## 1
## Seven Years' War
## 1
## Sierra Leone Civil War
## 1
## Sino-Burmese War (1765–69)
## 1
## Somali Civil War
## 1
## South African War (Second Boer War)
## 1
## Soviet–Afghan War
## 1
## Soviet–Japanese War
## 1
## Spanish American Wars of Independence
## 1
## Spanish Civil War
## 1
## Spanish conquest of the Aztec Empire
## 1
## Spanish conquest of the Inca Empire
## 1
## Spanish conquest of Yucatán
## 1
## Sri Lankan Civil War
## 1
## Syrian Civil War
## 1
## Taiping Rebellion
## 1
## Ten Years' War
## 1
## Third Punic War
## 1
## Thirty Years' War
## 1
## Thousand Days' War
## 1
## Three Kingdoms War
## 1
## Transition from Ming to Qing
## 1
## Ugandan Bush War
## 1
## Venezuelan War of Independence
## 1
## Vietnam War
## 1
## War in Afghanistan
## 1
## War in Afghanistan (2001–present)
## 1
## War in Darfur
## 1
## War in North-West Pakistan
## 1
## War of the Spanish Succession
## 1
## War on Terror
## 1
## Wars of Alexander the Great
## 1
## Wars of Scottish Independence
## 1
## Wars of the Roses
## 1
## Wars of the Sixteen Kingdoms
## 1
## Wars of the Three Kingdoms
## 1
## West Papua conflict
## 1
## Winter War
## 1
## World War I
## 1
## World War II
## 1
## Yellow Turban Rebellion
## 1
## Yemeni Civil War
## 1
Esto luce mejor:
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(knitr)
dataTable=table(guerras$War)
kable(dataTable)
| Var1 | Freq |
|---|---|
| 1991 Iraqi coup d’état attempt | 1 |
| Aceh War | 1 |
| Albigensian Crusade | 1 |
| Algerian Civil War | 1 |
| Algerian War | 1 |
| American Civil War | 1 |
| American Revolutionary War | 1 |
| An Lushan Rebellion | 1 |
| Anglo-Spanish War (1585–1604) | 1 |
| Angolan Civil War | 1 |
| Angolan War of Independence | 1 |
| Arab–Byzantine Wars | 1 |
| Arab–Israeli conflict | 1 |
| Balkan Wars | 1 |
| Bangladesh Liberation War | 1 |
| Bar Kokhba Revolt | 1 |
| Boko Haram insurgency | 1 |
| Bosnian War | 1 |
| Burundian Civil War | 1 |
| Campaigns of Suleiman the Magnificent | 1 |
| Carlist Wars | 1 |
| Chinese Civil War | 1 |
| Cimbrian War | 1 |
| Colombian conflict | 1 |
| Congo Crisis | 1 |
| Conquest of the Desert | 1 |
| Conquests of Cyrus the Great | 1 |
| Conquests of Mehmed II ‘the Conqueror’ | 1 |
| Conquests of Timur | 1 |
| Continuation War | 1 |
| CPP-NPA-NDF rebellion | 1 |
| Crimean War | 1 |
| Crusades | 1 |
| Cuban War of Independence | 1 |
| Dungan Revolt | 1 |
| Eighty Years’ War | 1 |
| English Civil War | 1 |
| Ethiopian Civil War | 1 |
| Ethnic conflict in Nagaland | 1 |
| First Congo War | 1 |
| First Indochina War | 1 |
| First Jewish–Roman War | 1 |
| First Punic War | 1 |
| First Sino–Japanese War | 1 |
| First Sudanese Civil War | 1 |
| Franco-Dutch War | 1 |
| Franco-Spanish War (1635–59) | 1 |
| French campaign in Egypt and Syria | 1 |
| French conquest of Algeria | 1 |
| French intervention in Mexico | 1 |
| French invasion of Russia | 1 |
| French Wars of Religion | 1 |
| Gallic Wars | 1 |
| German Peasants’ War | 1 |
| Goryeo–Khitan Wars | 1 |
| Gothic War (269) | 1 |
| Gothic War (376–382) | 1 |
| Great Northern War | 1 |
| Great Turkish War | 1 |
| Greco-Italian War | 1 |
| Greco–Persian Wars | 1 |
| Greek Civil War | 1 |
| Gulf War | 1 |
| Hundred Years’ War | 1 |
| Hunnic Invasions | 1 |
| Iceni Revolt | 1 |
| Indian annexation of Hyderabad | 1 |
| Indian Rebellion of 1857 | 1 |
| Insurgency in Laos | 1 |
| Insurgency in Northeast India | 1 |
| Internal conflict in Myanmar | 1 |
| Internal conflict in Peru | 1 |
| Iran–Iraq War | 1 |
| Iraq War | 1 |
| Iraqi Civil War (2014–2017) | 1 |
| Iraqi–Kurdish conflict | 1 |
| Italian Wars | 1 |
| Ituri conflict | 1 |
| Japanese invasions of Korea | 1 |
| Jewish–Roman Wars | 1 |
| Kalinga War | 1 |
| Kashmir Conflict | 1 |
| Kitos War | 1 |
| Kivu Conflict | 1 |
| Korean War | 1 |
| Kurdish rebellions in Turkey | 1 |
| Kurdish–Turkish conflict | 1 |
| La Violencia | 1 |
| Lebanese Civil War | 1 |
| Lord’s Resistance Army insurgency | 1 |
| Malagasy Uprising | 1 |
| Maratha expeditions in Bengal | 1 |
| Mexican Drug War | 1 |
| Mexican Revolution | 1 |
| Mfecane | 1 |
| Mongol conquests | 1 |
| Moorish Wars | 1 |
| Moro Conflict | 1 |
| Mozambican War of Independence | 1 |
| Mughal–Maratha Wars | 1 |
| Nagorno-Karabakh War | 1 |
| Napoleonic Wars | 1 |
| Nigerian Civil War | 1 |
| North Yemen Civil War | 1 |
| Panthay Rebellion | 1 |
| Paraguayan War | 1 |
| Philippine–American War | 1 |
| Probus’s German War | 1 |
| Punic Wars | 1 |
| Qin’s Wars of Unification | 1 |
| Reconquista | 1 |
| Russian Civil War | 1 |
| Rwandan genocide | 1 |
| Saint-Domingue expedition | 1 |
| Samnite Wars | 1 |
| Second Congo War | 1 |
| Second Italo-Ethiopian War | 1 |
| Second Italo-Senussi War | 1 |
| Second Punic War | 1 |
| Second Sino-Japanese War | 1 |
| Second Sudanese Civil War | 1 |
| Seven Years’ War | 1 |
| Sierra Leone Civil War | 1 |
| Sino-Burmese War (1765–69) | 1 |
| Somali Civil War | 1 |
| South African War (Second Boer War) | 1 |
| Soviet–Afghan War | 1 |
| Soviet–Japanese War | 1 |
| Spanish American Wars of Independence | 1 |
| Spanish Civil War | 1 |
| Spanish conquest of the Aztec Empire | 1 |
| Spanish conquest of the Inca Empire | 1 |
| Spanish conquest of Yucatán | 1 |
| Sri Lankan Civil War | 1 |
| Syrian Civil War | 1 |
| Taiping Rebellion | 1 |
| Ten Years’ War | 1 |
| Third Punic War | 1 |
| Thirty Years’ War | 1 |
| Thousand Days’ War | 1 |
| Three Kingdoms War | 1 |
| Transition from Ming to Qing | 1 |
| Ugandan Bush War | 1 |
| Venezuelan War of Independence | 1 |
| Vietnam War | 1 |
| War in Afghanistan | 1 |
| War in Afghanistan (2001–present) | 1 |
| War in Darfur | 1 |
| War in North-West Pakistan | 1 |
| War of the Spanish Succession | 1 |
| War on Terror | 1 |
| Wars of Alexander the Great | 1 |
| Wars of Scottish Independence | 1 |
| Wars of the Roses | 1 |
| Wars of the Sixteen Kingdoms | 1 |
| Wars of the Three Kingdoms | 1 |
| West Papua conflict | 1 |
| Winter War | 1 |
| World War I | 1 |
| World War II | 1 |
| Yellow Turban Rebellion | 1 |
| Yemeni Civil War | 1 |
| - Gráfico | |
| Versión básica: |
library(ggplot2)
base=ggplot(data=guerras,aes(x=Location))
bar1=base + geom_bar()
bar1
Con títulos
bar1 = bar1 + labs(x="País/Zona", y="cantidad", title="Ubicación de las Guerras en la historia",subtitle="Solo con más de 2500 muertos", caption="Fuente: Wikipedia")
bar1
Mejorando etiquetas del eje X:
bar1+ theme(axis.text.x = element_text(angle=25, size=7, hjust = 1) )
Estadísticos Centrales: - La moda
library(DescTools)
Mode(guerras$Location)
## [1] "China"
## 0.25: La moda se diferencia de los demás
library(DescTools)
Herfindahl(dataTable)
## [1] 0.00617284
1/Herfindahl(dataTable)
## [1] 162