在R中导入维基百科表 [英] Importing wikipedia tables in R
问题描述
= ImportHtml(http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan ,table,3)
此功能将下载第3个表,其中列出所有密歇根州的UP县,从该页。
R中有什么类似的东西吗?或者可以通过用户定义的函数创建?
函数 readHTMLTable
尝试以下操作:
库(XML)
doc< - readHTMLTable(
doc =http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan )
doc [[6]]
V1 V2 V3 V4
1县人口面积(平方英里)人口密度(每平方英里)
2阿尔及尔9,862 918 10.7
3巴拉加8,735 904 9.7
4奇皮瓦38,413 1561 24.7
5三角洲38,520 1170 32.9
6迪金森27,427 766 35.8
7 Gogebic 17, 370 1102 15.8
8霍顿36,016 1012 35.6
9铁13,138 1166 11.3
10 Keweenaw 2,301 541 4.3
11 Luce 7,024 903 7.8
12麦金纳11,943 1022 11.7
13 Marquette 64,634 1821 35.5
14 Menominee 25,109 1043 24.3
15 Ontonagon 7,818 1312 6.0
16教科书8,903 1178 7.6
17总计317,258 16,420 19.3
readHTMLTable
返回列表的 data.frame
s HTML页面的ach元素。您可以使用名称
获取有关每个元素的信息:
>名称(doc)
[1]NULL
[2]toc
[3]2008年上半岛县级总统选举的选举结果
[4]空
[5]上半岛城乡村
[6]上半岛土地面积和县人口密度
[7]19世纪人口普查人口普查年份上半岛县
[8]第20和21世纪人口普查人口按县人口上半岛年份
[9]NULL
[ 10]NULL
[11]NULL
[12]NULL
[13]NULL
[14]NULL
[15]NULL
[16]NULL
I regularly extract tables from Wikipedia. Excel's web import does not work properly for wikipedia, as it treats the whole page as a table. In google spreadsheet, I can enter this:
=ImportHtml("http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan","table",3)
and this function will download the 3rd table, which lists all the counties of the UP of Michigan, from that page.
Is there something similar in R? or can be created via a user defined function?
The function readHTMLTable
in package XML
is ideal for this.
Try the following:
library(XML)
doc <- readHTMLTable(
doc="http://en.wikipedia.org/wiki/Upper_Peninsula_of_Michigan")
doc[[6]]
V1 V2 V3 V4
1 County Population Land Area (sq mi) Population Density (per sq mi)
2 Alger 9,862 918 10.7
3 Baraga 8,735 904 9.7
4 Chippewa 38,413 1561 24.7
5 Delta 38,520 1170 32.9
6 Dickinson 27,427 766 35.8
7 Gogebic 17,370 1102 15.8
8 Houghton 36,016 1012 35.6
9 Iron 13,138 1166 11.3
10 Keweenaw 2,301 541 4.3
11 Luce 7,024 903 7.8
12 Mackinac 11,943 1022 11.7
13 Marquette 64,634 1821 35.5
14 Menominee 25,109 1043 24.3
15 Ontonagon 7,818 1312 6.0
16 Schoolcraft 8,903 1178 7.6
17 TOTAL 317,258 16,420 19.3
readHTMLTable
returns a list of data.frame
s for each element of the HTML page. You can use names
to get information about each element:
> names(doc)
[1] "NULL"
[2] "toc"
[3] "Election results of the 2008 Presidential Election by County in the Upper Peninsula"
[4] "NULL"
[5] "Cities and Villages of the Upper Peninsula"
[6] "Upper Peninsula Land Area and Population Density by County"
[7] "19th Century Population by Census Year of the Upper Peninsula by County"
[8] "20th & 21st Centuries Population by Census Year of the Upper Peninsula by County"
[9] "NULL"
[10] "NULL"
[11] "NULL"
[12] "NULL"
[13] "NULL"
[14] "NULL"
[15] "NULL"
[16] "NULL"
这篇关于在R中导入维基百科表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!