library(htmltab)
link="https://es.wikipedia.org/wiki/Elecciones_parlamentarias_de_Per%C3%BA_de_2016"
path='//*/div/table[6]/tbody/tr/td/table'
Tabla=htmltab(doc = link,which = path)
espacios en blanco
Tabla[,]=lapply(Tabla[,],trimws,whitespace = "[\\h\\v]")
Convierto en variable categórica nominal
Tabla$Partido=as.factor(Tabla$Partido)
Convirtiendo en variable númerica
Tabla$Votos=as.numeric(Tabla$Votos)
NAs introduced by coercion
Tabla$Escaños=as.numeric(Tabla$Escaños)
str(Tabla)
'data.frame': 130 obs. of 6 variables:
$ Distrito Electoral : chr "Amazonas(al 100,00%)" "Amazonas(al 100,00%)" "Áncash(al 100,00%)" "Áncash(al 100,00%)" ...
$ Escaños : num 2 2 5 5 5 5 5 2 2 6 ...
$ Congresistas electos: chr "2" "1" "2" "1" ...
$ Congresistas electos: chr "Marita Herrera Arévalo" "Miguel Antonio Castro Grández" "Eloy Ricardo Narváez Soto" "María Elena Foronda Farro" ...
$ Partido : Factor w/ 6 levels "Acción Popular",..: 5 5 2 4 5 5 5 4 5 1 ...
$ Votos : num NA NA NA NA NA NA NA NA NA NA ...
Tabla[,c(2,3,6)]=lapply(Tabla[,c(2,3,6)],as.numeric)
str(Tabla)
'data.frame': 130 obs. of 6 variables:
$ Distrito Electoral : chr "Amazonas(al 100,00%)" "Amazonas(al 100,00%)" "Áncash(al 100,00%)" "Áncash(al 100,00%)" ...
$ Escaños : num 2 2 5 5 5 5 5 2 2 6 ...
$ Congresistas electos: num 2 1 2 1 3 1 2 1 2 1 ...
$ Congresistas electos: chr "Marita Herrera Arévalo" "Miguel Antonio Castro Grández" "Eloy Ricardo Narváez Soto" "María Elena Foronda Farro" ...
$ Partido : Factor w/ 6 levels "Acción Popular",..: 5 5 2 4 5 5 5 4 5 1 ...
$ Votos : num NA NA NA NA NA NA NA NA NA NA ...
Tabla[,]=lapply(Tabla[,],trimws,whitespace = "[\\h\\v]")
f1=formula(Votos~Partido)
aggregate(f1,Tabla,mean)
Error in aggregate.data.frame(mf[1L], mf[-1L], FUN = FUN, ...) :
no rows to aggregate
summary(Tabla)
Distrito Electoral Escaños Congresistas electos Congresistas electos Partido Votos
Length:130 Length:130 Length:130 Length:130 Length:130 Length:130
Class :character Class :character Class :character Class :character Class :character Class :character
Mode :character Mode :character Mode :character Mode :character Mode :character Mode :character
This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.