问题
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