I have this.

data = ResourceData["Epidemic Data for Novel Coronavirus COVID-19", 

usaCountyCases[s_, c_] := 
 data[Select[MatchQ[Interpreter["USState"][s], #State] &]][
        Interpreter["USCounty"][c <> " county, " <> s], #County] &]][
    All, #ConfirmedCases["LastValue"] &] // Normal // First

usaCountyCases["California", "Alpine"]

gives 124 (as of today). How do I speed this up? TIA.

  • 2
    $\begingroup$ Replace the Interpreter bit with Entity["AdministrativeDivision", {s, "UnitedStates"}]? You will lose some flexibility though: for instance, North Dakota will have to be entered as "NorthDakota" (no space). $\endgroup$
    – MarcoB
    Jul 26 at 2:08
  • $\begingroup$ Thanks. And how would you replace Interpreter for the counties? $\endgroup$
    – atat
    Jul 26 at 3:38
  • $\begingroup$ One more thing of note: It appears that this dataset is no longer actively updated, having been last updated in January 2022. $\endgroup$
    – MarcoB
    Jul 26 at 18:08
  • $\begingroup$ How do I get the updated coronavirus then? $\endgroup$
    – atat
    Jul 30 at 10:27
  • $\begingroup$ I don't know. CDC? WHO maybe? I'm not sure if or where updated data is available. $\endgroup$
    – MarcoB
    Jul 30 at 11:16

1 Answer 1

  1. You don't need to filter twice, by state and then by county, since the county already contains the state information.
  2. The following modification to your function stuffs the string with the name of the county and the state into the appropriate Entity form, avoiding the calls to Interpreter.
  3. The input strings are sanitized to remove spaces, so you can enter, for instance, "North Dakota", and the function will know to use "NorthDakota" in its query.
  4. I've added the dataset as a parameter to your function so it is not hard-coded and you can use it on different datasets (e.g. subsets of the original).
usaCountyCases[state_String, county_String, dataset_Dataset] := Module[{c},
  c = Entity[
          StringReplace[county, " " -> ""] <> "County",
          StringReplace[state, " " -> ""], "UnitedStates"
  First[#, #]& @
      Select[#County == c &], 

usaCountyCases["California", "Alpine", data]
(* 124 *)

usaCountyCases["North Dakota", "Cass", data]
(* 54993 *)

From a performance perspective, these calls returned results practically instantaneously when I tried them.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.