Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...