-2

I am new to R. I am trying to scrape data from wikipedia but o/p is not in readable format.

Below is my code:

library("rvest")
library("xml2")
library("magrittr")
library("tidyverse")


wiki_url <- "https://en.wikipedia.org/wiki/List_of_countries_by_intentional_homicide_rate"


population <- wiki_url %>%
  read_html() %>%
  html_nodes(xpath='//*[@id="mw-content-text"]/div/table[2]') %>%
  html_table(fill = TRUE)

O/P:

 [[1]]
         X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22
         X23 X24 X25 X26 X27 X28 X29 X30 X31 X32 X33 X34 X35 X36 X37 X38 X39 X40 X41 X42
        .
        .
        .
        . 
         X1843 X1844 X1845 X1846 X1847 X1848 X1849 X1850 X1851 X1852 X1853 X1854 X1855
         X1856 X1857 X1858
     [ reached 'max' / getOption("max.print") -- omitted 465 rows ]

I want all columns and rows in table format only.

2 Answers2

1

I think you want the fourth table from the page.

library(rvest)
wiki_url <- "https://en.wikipedia.org/wiki/List_of_countries_by_intentional_homicide_rate"

page <- wiki_url %>%read_html()

#find all of the tables on the page
tables<- page %>% read_html() %>%  html_nodes('table');

#get the desired table (searched by trial and error)
answer<-html_table(tables[4])[[1]] 

head(answer)

    Country  Region      Subregion Rate Count Yearlisted       Source
1  NA               NA             
2  Burundi Africa Eastern Africa 6.02   635       2016      CTS/SDG
3  Comoros Africa Eastern Africa 7.70    60       2015 WHO Estimate
4  Djibouti Africa Eastern Africa 6.48    60       2015 WHO Estimate
5  Eritrea Africa Eastern Africa 8.04   390       2015 WHO Estimate
6  Ethiopia Africa Eastern Africa 7.56 7,552       2015 WHO Estimate
Dave2e
  • 22,192
  • 18
  • 42
  • 50
0

This works for me!!

library(rvest)
URL <- 'https://en.wikipedia.org/wiki/List_of_countries_by_intentional_homicide_rate'
table <- URL %>%  
  read_html %>% 
  html_nodes("table")  %>% 
  .[[4]] %>% 
  html_table(trim=TRUE)

table

    Country (or dependent territory,subnational area, etc.)   Region                 Subregion
1                                                                                             
2                                                   Burundi   Africa            Eastern Africa
3                                                   Comoros   Africa            Eastern Africa
4                                                  Djibouti   Africa            Eastern Africa
5                                                   Eritrea   Africa            Eastern Africa
6                                                  Ethiopia   Africa            Eastern Africa
7                                                     Kenya   Africa            Eastern Africa
8                                                Madagascar   Africa            Eastern Africa
9                                                    Malawi   Africa            Eastern Africa
10                                                Mauritius   Africa            Eastern Africa
ASH
  • 20,759
  • 19
  • 87
  • 200