library(htmltab)
linkPage ="https://en.wikipedia.org/wiki/Democracy_Index"
linkTabla ='//*[@id="mw-content-text"]/div/table[2]/tbody'
Demo=htmltab(doc = linkPage,
which = linkTabla)
#Visibilizar nuestras variables
str(Demo)
#Revisar los nombre de las variables
names(Demo)
Sin la parte de [] en el código, solo se partirá el nombre en partes.
library(stringr)
names(Demo)=str_split(names(Demo)," ",simplify = T)[,1]
# _código_ _espacio_ _partequesequeda_
names(Demo) #para revisar los nombres (opcional)
## [1] "Rank" "Country"
## [3] "Score" "Electoral"
## [5] "Functioning" "Politicalparticipation"
## [7] "Politicalculture" "Civilliberties"
## [9] "Regimetype" "Continent"
names(Demo)=str_replace_all(names(Demo),"[^[:ascii:]]", "")
# _simbolosraros_
Demo[,]=lapply(Demo[,], str_replace_all, "[^[:ascii:]]", "")
Demo$Rank=NULL
library(readr)
Demo[,c(2:7)]=lapply(Demo[,c(2:7)],parse_number)
#viendo la tabla
table(Demo$Regimetype)
##
## Authoritarian Flawed democracy Full democracy Hybrid regime
## 53 55 20 39
#Arroja los datos en desorden
#Para ordenar los datos de forma ordinal:
RegTypeOK=c("Authoritarian","Hybrid regime", "Flawed democracy","Full democracy")
Demo$Regimetype=factor(Demo$Regimetype, levels = RegTypeOK, ordered = TRUE)
table(Demo$Regimetype)
##
## Authoritarian Hybrid regime Flawed democracy Full democracy
## 53 39 55 20
Demo$Continent=as.factor(Demo$Continent)
#HASTA AHORA TENEMOS:
str(Demo)
## 'data.frame': 167 obs. of 9 variables:
## $ Country : chr "Norway" "Iceland" "Sweden" "New Zealand" ...
## $ Score : num 9.87 9.58 9.39 9.26 9.22 9.15 9.15 9.14 9.09 9.03 ...
## $ Electoral : num 10 10 9.58 10 10 9.58 9.58 10 10 9.58 ...
## $ Functioning : num 9.64 9.29 9.64 9.29 9.29 7.86 9.64 8.93 8.93 9.29 ...
## $ Politicalparticipation: num 10 8.89 8.33 8.89 8.33 8.33 7.78 8.33 7.78 7.78 ...
## $ Politicalculture : num 10 10 10 8.13 9.38 10 8.75 8.75 8.75 9.38 ...
## $ Civilliberties : num 9.71 9.71 9.41 10 9.12 10 10 9.71 10 9.12 ...
## $ Regimetype : Ord.factor w/ 4 levels "Authoritarian"<..: 4 4 4 4 4 4 4 4 4 4 ...
## $ Continent : Factor w/ 6 levels "Africa","Asia",..: 3 3 3 5 3 3 4 3 5 3 ...
1. ¿Cual es el valor representativo de Tipo de Regimen
valor represntativo: nominal= moda - para saber si la moda es muy prominente ordinales= mediana - Median con mayuscula (con minuscula es otra funcion) numérica= media
library(DescTools)
Median(Demo$Regimetype)
## [1] Hybrid regime
## 4 Levels: Authoritarian < Hybrid regime < ... < Full democracy
2. ¿Podemos afirmar que 3 cuartas partes de los paises del mundo que tenemos en nuestra tabla no superan el tipo hibrido de democracia?
library(questionr)
library(magrittr)
OrdDF=freq(Demo$Regimetype, total = F, exclude = c(NA), cum = T) %>%data.frame()
OrdDF=data.frame(row.names(OrdDF),OrdDF,row.names = NULL)
names(OrdDF)=c("Categoria","Conteo","Porcentaje","Porcentaje Acumulado")
OrdDF
## Categoria Conteo Porcentaje Porcentaje Acumulado
## 1 Authoritarian 53 31.7 31.7
## 2 Hybrid regime 39 23.4 55.1
## 3 Flawed democracy 55 32.9 88.0
## 4 Full democracy 20 12.0 100.0
3.1. ¿Cual es el valor representativo de Continente?
valor represntativo: nominal= moda - para saber si la moda es muy prominente ordinales= mediana - Median con mayuscula (con minuscula es otra funcion) numérica= media
Mode(Demo$Continent)
## [1] "Africa"
3.2. ¿Es el valor representativo de continente muy prominente? (quizas otros continentes no envian informacion?)
valor represntativo: nominal= moda - para saber si la moda es muy prominente ordinales= mediana - Median con mayuscula (con minuscula es otra funcion) numérica= media
tablaContinente=prop.table(table(Demo$Continent))
Herfindahl(tablaContinente)
## [1] 0.238266
simetria cuando la cola está a un solo lado cuando la cola se presenta a ambos lados, es asimetrica