如何整理数据框中的数据(维基百科内部链接)?
How to tidyr data from a dataframe (wikipedia internal links)?
我正在使用 WikipediR 包处理维基百科内部链接
我正在寻找有关 Hérodote 的内部链接,(法语)
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
我的结果:
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)
如何整理我的 data.frame
物品?
预期结果:
pageid title
60 Attributs du pharaon
133 Apis
152 Antropologie
159 Asie
您正在使用的函数 returns 列表中的命名字符向量。我们可以使用 purrr::map_df()
和 as.list()
。 map_df()
将对 all_bls
列表中的每个元素执行 as.list()
并自动将它们行绑定到数据框中:
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
我正在使用 WikipediR 包处理维基百科内部链接 我正在寻找有关 Hérodote 的内部链接,(法语)
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
我的结果:
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)
如何整理我的 data.frame
物品?
预期结果:
pageid title
60 Attributs du pharaon
133 Apis
152 Antropologie
159 Asie
您正在使用的函数 returns 列表中的命名字符向量。我们可以使用 purrr::map_df()
和 as.list()
。 map_df()
将对 all_bls
列表中的每个元素执行 as.list()
并自动将它们行绑定到数据框中:
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