Practica Dirigida
Guarda el link de wiki en un objeto de R, aqui 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 la tabla. Elije la opcion inspeccionar usando el boton derecho:
Si ya has encontrado la tabla en los codigos, copia el XPATH (usando el boton derecho del mouse):
Has eso para cada tabla:
library(htmltab) #instalemos primero
guerras1 = htmltab(doc = LINKGUERRAS,
which ='//*[@id="mw-content-text"]/div/table[5]') # aqui va XPATH
guerras2 = htmltab(doc = LINKGUERRAS,
which ='//*[@id="mw-content-text"]/div/table[7]') # aqui va XPATH
## Warning: Columns [Notes] seem to have no data and are removed. Use
## rm_nodata_cols = F to suppress this behavior
guerras3 = htmltab(doc = LINKGUERRAS,
which ='//*[@id="mw-content-text"]/div/table[8]') # aqui va XPATH
## Warning: Columns [Notes] seem to have no data and are removed. Use
## rm_nodata_cols = F to suppress this behavior
Mira las columas que tienes:
names(guerras1)
## [1] "War" "Deathrange" "Geometricmean(Note 1)"
## [4] "Date" "Combatants" "Location"
## [7] "Notes"
names(guerras2)
## [1] "War" "Deathrange" "Geometricmean" "Date"
## [5] "Combatants" "Location"
names(guerras3)
## [1] "War" "Deathrange" "Geometricmean" "Date"
## [5] "Comabatants" "Location"
Quedate con estas columnas:
guerras1=guerras1[,c("War","Location")]
guerras2=guerras1[,c("War","Location")]
guerras3=guerras1[,c("War","Location")]
Cada data frame tiene ahora 2 columnas cada uno.
Usa la funcion rbind para poner las tablas una debajo de la otra:
guerras=rbind(guerras1,guerras2,guerras3)
El dataframe guerras tiene todo.
Ahora a analizar la columna Location (la unica que vale la pena y es nominal).
table(guerras$Location)
##
## China
## 9
## Eastern Europe
## 3
## England
## 3
## Europe
## 9
## France
## 3
## Greece
## 3
## India
## 3
## Italy
## 3
## Middle East
## 9
## Middle East / North Africa / Central Asia / India
## 3
## Middle East/North Africa
## 3
## Northern China
## 3
## Southern Europe / North Africa
## 6
## Tunisia
## 3
## Western Europe
## 3
## Western Europe / North Africa
## 6
Esto luce mejor:
library(knitr)
dataTable=table(guerras$Location)
kable(dataTable)
| Var1 | Freq |
|---|---|
| China | 9 |
| Eastern Europe | 3 |
| England | 3 |
| Europe | 9 |
| France | 3 |
| Greece | 3 |
| India | 3 |
| Italy | 3 |
| Middle East | 9 |
| Middle East / North Africa / Central Asia / India | 3 |
| Middle East/North Africa | 3 |
| Northern China | 3 |
| Southern Europe / North Africa | 6 |
| Tunisia | 3 |
| Western Europe | 3 |
| Western Europe / North Africa | 6 |
Version basica:
library(ggplot2)
base = ggplot(data=guerras,aes(x=Location))
bar1 = base + geom_bar()
bar1
Con titulos:
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
Mejorando etiquetas del eje X:
bar1 + theme(axis.text.x = element_text(angle = 25,size=7,hjust = 1))
Estadísticos:
library(DescTools)
Mode(guerras$Location)
## [1] "China" "Europe" "Middle East"
# mientras mayor, la moda representa menos
dataTable=table(guerras$Location)
1-max(prop.table(dataTable))
## [1] 0.875
Concentracion: Herfindahl- Hirschman
0.25: La moda se diferencia de los demas
library(DescTools)
Herfindahl(dataTable)
## [1] 0.07986111
1/Herfindahl(dataTable)
## [1] 12.52174
Hay 12 grupos representativos.