Home > Mobile >  How do I download a table from the CDC website as a CSV file?
How do I download a table from the CDC website as a CSV file?

Time:10-27

I am trying to use BRFSS Data from the CDC in R. In particular, I am trying to read the 2014-2018 data into separate dataframes (step 1 complete), add column titles to the dataframes (what I'm working on), and combine all years into one dataframe.

The column titles are not in the ASC data file, but they are on this website in an HTML table: https://www.cdc.gov/brfss/annual_data/2017/llcp_varlayout_17_onecolumn.html

How can I take the table from this website and download it as a CSV file?

p.s. this is the code I am trying to replicate in order to use the data (if anyone uses BRFSS data and has a better way, let me know). He already created a CSV of column title data that he is using, but it is for a different year so I can't use it and he doesn't give instructions. https://michaelminn.net/tutorials/r-brfss/

CodePudding user response:

You can use rvest

library(rvest)

url <- "https://www.cdc.gov/brfss/annual_data/2017/llcp_varlayout_17_onecolumn.html"
data <- read_html(url) %>% 
  html_element(xpath="//main//table") %>% 
  html_table()

data
#> # A tibble: 358 × 3
#>    `Starting Column` `Variable Name` `Field Length`
#>                <int> <chr>                    <int>
#>  1                 1 _STATE                       2
#>  2                17 FMONTH                       2
#>  3                19 IDATE                        8
#>  4                19 IMONTH                       2
#>  5                21 IDAY                         2
#>  6                23 IYEAR                        4
#>  7                32 DISPCODE                     4
#>  8                36 SEQNO                       10
#>  9                36 _PSU                        10
#> 10                63 CTELENM1                     1
#> # … with 348 more rows
  • Related