How to drop factor levels while scraping data off US Census HTML site

元气小坏坏 提交于 2019-11-27 07:33:12

问题


Thank you in advance for your help. On the US Census website (below), I am looking for an element in the 6th row, 3rd column of the 4th table.

Here's the code I am writing:

complete_URL <- "http://quickfacts.census.gov/qfd/states/01/01011.html"
temp_TBL <- readHTMLTable(complete_URL, which=4)
business_number_vector <- temp_TBL[6,3]
print(business_number_vector)

What I get is:

[1] 417
Levels: 417

What I'd like is:

[1] 417

Thank you again so much for your help!


回答1:


It's actually R-FAQ 7.10:

You should be able to see the FAQ with your R-help() system. On my machine it is set up as html:

http://127.0.0.1:23603/doc/manual/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f

7.10 How do I convert factors to numeric?

It may happen that when reading numeric data into R (usually, when reading in a file), they come in as factors. If f is such a factor object, you can use

as.numeric(as.character(f)) to get the numbers back. More efficient, but harder to remember, is

as.numeric(levels(f))[as.integer(f)] In any case, do not call as.numeric() or their likes directly for the task at hand (as as.numeric() or unclass() give the internal codes).




回答2:


Arun and Dason provided these answers:

Arun recommended:

complete_URL <- "http://quickfacts.census.gov/qfd/states/01/01011.html"
temp_TBL <- readHTMLTable(complete_URL, which=4)
business_number_vector <- as.numeric(as.character(temp_TBL[6,3]))
print(business_number_vector)

see line 3, where as.numeric(as.character()) helped big-time!

Dason recommended:

complete_URL <- "http://quickfacts.census.gov/qfd/states/01/01011.html"
temp_TBL <- readHTMLTable(complete_URL, which=4)
business_number_vector <- temp_TBL[6,3]
business_number_vector <- as.numeric(levels(business_number_vector)[business_number_vector])
print(business_number_vector)

see line 4 where as.numeric(levels(business_number_vector)[business_number_vector]) also helped big-time!



来源:https://stackoverflow.com/questions/15670151/how-to-drop-factor-levels-while-scraping-data-off-us-census-html-site

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!