0

I am trying to download some Covid info (Finland) to R df's with following script. Download is Ok, but No success in conversion to DF.

Would appreciate your help!

urlcases <- "https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.json?row=hcdmunicipality2020-445222&column=dateweek20200101-509030&filter=measure-444833"

jasoncases <- jsonlite::fromJSON(urlcases)
jasoncases <- lapply(jasoncases, function(x) {
  x[sapply(x, is.null)] <- NA
  unlist(x)
})

cases_df <- as.data.frame(do.call("cbind", jasoncases))
  
datatable(head(cases_df))
r2evans
  • 141,215
  • 6
  • 77
  • 149
JuriJ
  • 1
  • 1
  • `cases_df` is a `data.frame` that is structured correctly, and for me, it contains 1605 rows and 1 column. I don't know why you're calling what I'm inferring is `DT::datatable`, that does not make an R-friendly structure, that's for the javascript DataTable library (which also formats correctly). Please explain what's wrong. – r2evans Mar 21 '21 at 14:36
  • OK. So if I want for instance gglot number of cases by region and time...how do I di it in this structure? – JuriJ Mar 21 '21 at 15:02
  • The structure can be converted to a frame, but there's no one-function that'll do it for you. It's not obvious to be which elements of the `dataset$dimension` portion matches with what. A good start would be to look just at `jasoncases$dataset$dimension$dataweek20200101` and figure out hot to flatten that into a frame; then adapt that process to `hcdmunicipality2020`, and then look into `jasoncases$dataset$value` and see how things should match what you created from `dimension`. – r2evans Mar 21 '21 at 17:12

1 Answers1

0

JSON-stat is a special JSON-format for data cubes. For getting JSON-stat into an R data-frame the easiest is to use specific Cran packages.

This will load your data into a dataframe:

library(PxWebApiData)
urlcases <- "https://sampo.thl.fi/pivot/prod/fi/epirapo/covid19case/fact_epirapo_covid19case.json?row=hcdmunicipality2020-445222&column=dateweek20200101-509030&filter=measure-444833"
cases_df <- GetApiData(urlcases)
janbrus
  • 1
  • 2