2017-02-17 8 views
0

Rに新品ですので、これを説明するために最善を尽くします。 私は "rvest"パッケージを使用してデータスクレイピングをしてきました。この例では、私はウィキペディアのテーブルから米国の州の人口を集めています。私が使用したコードは次のとおりです。rvestの出力をテーブルに変換する方法

library(rvest) 
statepop = read_html("https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population") 
forecasthtml = html_nodes(statepop, "td") 
forecasttext = html_text(forecasthtml) 
forecasttext 

次のように出力結果だった:

私はそれが上に提示される方法と同じように設定されているテーブルにテキストのこれらの文字列を有効にするにはどうすればよい
[2] "7000100000000000000♠1"           
[3] " California"             
[4] "39,250,017"             
[5] "37,254,503"             
[6] "7001530000000000000♠53"          
[7] "738,581"              
[8] "702,905"              
[9] "12.15%"              
[10] "7000200000000000000♠2"           
[11] "7000200000000000000♠2"           
[12] " Texas"              
[13] "27,862,596"             
[14] "25,146,105"             
[15] "7001360000000000000♠36"          
[16] "763,031"              
[17] "698,487"              
[18] "8.62%" 

を元のWikipediaのページ(列、行など)?

+1

使用 'HTML_TABLE()'の代わりに 'HTML_TEXT()' – Nate

答えて

2

rvestのhtml_table関数を使用してみてください。
ページに5つのテーブルがあるので、どのテーブルを解析するかを指定する必要があります。

library(rvest) 

statepop = read_html("https://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population") 
#find all of the tables on the page 
tables<-html_nodes(statepop, "table") 
#convert the first table into a dataframe 
table1<-html_table(tables[1]) 
関連する問題