Pablo Boswell
Pablo Boswell

Reputation: 845

Scrape .txt table from URL in R

Here is the URL:

https://www.bls.gov/lau/laucnty15.txt

Unfortunately, I think the problem is that the table is tab-delimited but values are not delimited by quotes (""). So I don't know how I would strip out values using spaces without breaking apart the county name, for example.

I have tried:

webData <- data.frame(read.table(usrWebsiteURL,sep="\t",skip=5,header=F))

webData <- data.frame(readLines(usrWebsiteURL)),sep="\t",skip=5,header=F)

Either method creates a large, 1-column data.frame.

Why is the sep="\t" not working? Worse-case scenario, I can accept a split apart County Name and put it back together later, but I can't even achieve splitting apart the words in the table.

Upvotes: 0

Views: 781

Answers (1)

Fred Boehm
Fred Boehm

Reputation: 668

This code, using the readr (from CRAN) package worked for me:

    readr::read_table("https://www.bls.gov/lau/laucnty15.txt", skip = 6, col_names = FALSE)

You would probably want to add the column names after reading the file, but this can be done manually, eg., by using

    dat <- readr::read_table("https://www.bls.gov/lau/laucnty15.txt", skip = 6, col_names = FALSE)
    colnames(dat) <- c("LAUS Code", "State FIPS Code", "County FIPS Code", "County name", "Year", "Labor Force", "Employed", "Unemployed Level", "Unemployed Rate")

Upvotes: 1

Related Questions