Wilcar Wilcar - 3 months ago 10
R Question

How to tidyr data from a dataframe (wikipedia internal links)?

I am working on wikipedia internal links using WikipediR package
I am looking for internal links about Hérodote, (in french)

install.packages("WikipediR")
library (WikipediR)

all_bls <- page_backlinks("fr","wikipedia",
page = "Hérodote",
clean_response = TRUE)

all_bls_df <- as.data.frame(all_bls) # converting in d.f


my result:

enter image description here

str(all_bls_df)
## 'data.frame': 3 obs. of 50 variables:
## $ structure.c..60....0....Attributs.du.pharaon.....Names...c..pageid... : Factor w/ 3 levels "0","60","Attributs du pharaon": 2 1 3
## $ structure.c..133....0....Apis.....Names...c..pageid....ns....title. : Factor w/ 3 levels "0","133","Apis": 2 1 3
## $ structure.c..152....0....Anthropologie.....Names...c..pageid... : Factor w/ 3 levels "0","152","Anthropologie": 2 1 3
## $ structure.c..159....0....Asie.....Names...c..pageid....ns....title. : Factor w/ 3 levels "0","159","Asie": 2 1 3
## $ structure.c..325....0....Ahmôsis.II.....Names...c..pageid... : Factor w/ 3 levels "0","325","Ahmôsis II": 2 1 3
## $ structure.c..412....0....Bastet.....Names...c..pageid....ns... : Factor w/ 3 levels "0","412","Bastet": 2 1 3
## $ structure.c..542....0....Corse.....Names...c..pageid....ns... : Factor w/ 3 levels "0","542","Corse": 2 1 3
## $ structure.c..715....0....Cyclades.....Names...c..pageid....ns... : Factor w/ 3 levels "0","715","Cyclades": 2 1 3
## (goes on for 42 more variables)


How can I tidy my
data.frame
object?

Expect result:

pageid title
60 Attributs du pharaon
133 Apis
152 Antropologie
159 Asie

Answer

The function you're using returns named character vectors in a list. We can use purrr::map_df() with as.list(). map_df() will execute the as.list() on each element in your all_bls list and automagically row-bind them into a data frame:

purrr::map_df(all_bls, as.list) 
## # A tibble: 50 × 3
##    pageid    ns                title
##     <chr> <chr>                <chr>
## 1      60     0 Attributs du pharaon
## 2     133     0                 Apis
## 3     152     0        Anthropologie
## 4     159     0                 Asie
## 5     325     0           Ahmôsis II
## 6     412     0               Bastet
## 7     542     0                Corse
## 8     715     0             Cyclades
## 9     734     0     Culte à mystères
## 10    821     0           Chamanisme
## # ... with 40 more rows