library(rvest)

link = "https://es.wikipedia.org/wiki/Elecciones_municipales_de_Lima_de_2022#Resultados_por_distrito"
path = '//*[@id="mw-content-text"]/div[1]/table[9]'
eleccioneslima <- read_html(link)%>%html_nodes(xpath = path)%>%html_table()%>% .[[1]]
head(eleccioneslima)
## # A tibble: 6 × 17
##   Distrito RP       RP     PP    PP    SP    SP    FE    FE    APP   APP   JP   
##   <chr>    <chr>    <chr>  <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Distrito ""       ""     ""    ""    ""    ""    ""    ""    ""    ""    ""   
## 2 Distrito "V"      "%"    "V"   "%"   "V"   "%"   "V"   "%"   "V"   "%"   "V"  
## 3 Ancón    "3,725"  "13.2… "9,3… "33.… "5,9… "21.… "2,0… "7.2… "3,5… "12.… "1,6…
## 4 Ate      "57,374" "17.4… "98,… "29.… "52,… "15.… "27,… "8.5… "26,… "7.9… "25,…
## 5 Barranco "11,604" "36.9… "5,7… "18.… "6,4… "20.… "2,7… "8.8… "2,1… "6.9… "1,6…
## 6 Breña    "22,721" "31.1… "18,… "25.… "14,… "19.… "8,2… "11.… "2,4… "3.3… "3,8…
## # ℹ 5 more variables: JP <chr>, AvP <chr>, AvP <chr>, PL <chr>, PL <chr>
eleccioneslima<- eleccioneslima[-1,]
eleccioneslima<- eleccioneslima[-44,]
eleccioneslima <- subset(eleccioneslima, select = -FE)
eleccioneslima <- subset(eleccioneslima, select = -APP)
eleccioneslima <- subset(eleccioneslima, select = -JP)
eleccioneslima <- subset(eleccioneslima, select = -AvP)
eleccioneslima <- subset(eleccioneslima, select = -PL)
#UNA VEZ:
eleccioneslima <- subset(eleccioneslima, select = -RP)
eleccioneslima <- subset(eleccioneslima, select = -PP)
eleccioneslima <- subset(eleccioneslima, select = -SP)
str(eleccioneslima)
## tibble [44 × 9] (S3: tbl_df/tbl/data.frame)
##  $ Distrito: chr [1:44] "Distrito" "Ancón" "Ate" "Barranco" ...
##  $ RP      : chr [1:44] "%" "13.28" "17.49" "36.92" ...
##  $ PP      : chr [1:44] "%" "33.27" "29.99" "18.34" ...
##  $ SP      : chr [1:44] "%" "21.35" "15.87" "20.37" ...
##  $ FE      : chr [1:44] "%" "7.28" "8.51" "8.82" ...
##  $ APP     : chr [1:44] "%" "12.79" "7.97" "6.98" ...
##  $ JP      : chr [1:44] "%" "5.99" "7.66" "5.18" ...
##  $ AvP     : chr [1:44] "%" "4.38" "10.01" "2.69" ...
##  $ PL      : chr [1:44] "%" "1.67" "2.50" "0.71" ...
eleccioneslima$RP<- as.numeric(eleccioneslima$RP)
## Warning: NAs introducidos por coerción
eleccioneslima$PP<- as.numeric(eleccioneslima$PP)
## Warning: NAs introducidos por coerción
eleccioneslima$SP<- as.numeric(eleccioneslima$SP)
## Warning: NAs introducidos por coerción