Search code examples
rweb-scrapingcensus

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!


Solution

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