2

I am trying to import a dataset from CMS using an API. My code, however, only returns 1,000 of the 155,262 observations. I don't know what I am doing wrong. Another user posted a similar problem, but regrettably, I still cannot it figure out.

library(jsonlite)

# url for CMS dataset
url <- 'https://data.cms.gov/data-api/v1/dataset/3cc6ad89-5cc0-4071-91e1-2a91aff79975/data?'

# read url and convert to data.frame
document <- fromJSON(url)

This is the link to the website on CMS: https://data.cms.gov/provider-characteristics/hospitals-and-other-facilities/provider-of-services-file-hospital-non-hospital-facilities. I am interested in accessing the POS file for Q4 2021. Thanks for your help.

Mel G
  • 132
  • 1
  • 10
  • 1
    When I run your code, `document` is a `data.frame`. What isn't working for you? (I'm using jsonlite-1.7.2 on R-4.1.2, for comparison.) – r2evans Feb 07 '22 at 04:25
  • You have to add a few parameters to your URL, like: `offset=1000` and `size=2000`. Please check the [API docs:](https://data.cms.gov/provider-characteristics/hospitals-and-other-facilities/provider-of-services-file-hospital-non-hospital-facilities/api-docs). Then you can pass the parameters with loop or lapply or whatever. `url <- 'https://data.cms.gov/data-api/v1/dataset/3cc6ad89-5cc0-4071-91e1-2a91aff79975/data?offset=1000&size=2000'` – Grzegorz Sapijaszko Feb 07 '22 at 08:30
  • @GrzegorzSapijaszko I set the `size`= 155262 because I want to import every row in the dataset. The code returns a maximum of 5000 observations. I don't know if that is all the API can handle or if I need to add something else to the code. – Mel G Feb 07 '22 at 13:39
  • @MelG, seems API has a restriction to 5k. Which is understandable. You can create a loop, like: `for (i in 0:(floor(155262/5000)+1)) {offset = i * 5000; url <- paste0("https://data.cms.gov/data-api/v1/dataset/3cc6ad89-5cc0-4071-91e1-2a91aff79975/data?offset=", offset, "&size=5000"); # get and process the data # rbind to dataframe }` – Grzegorz Sapijaszko Feb 07 '22 at 14:46
  • @GrzegorzSapijaszko thanks for your help! I will take your suggestion to create the loop. – Mel G Feb 07 '22 at 15:56

0 Answers0