- You don't need to filter twice, by state and then by county, since the county already contains the state information.
- 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
.
- 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.
- 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).
ClearAll[usaCountyCases]
usaCountyCases[state_String, county_String, dataset_Dataset] := Module[{c},
c = Entity[
"AdministrativeDivision",
{
StringReplace[county, " " -> ""] <> "County",
StringReplace[state, " " -> ""], "UnitedStates"
}
];
First[#, #]& @
Normal@
Query[
Select[#County == c &],
#ConfirmedCases["LastValue"]&
][dataset]
]
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.
Interpreter
bit withEntity["AdministrativeDivision", {s, "UnitedStates"}]
? You will lose some flexibility though: for instance, North Dakota will have to be entered as"NorthDakota"
(no space). $\endgroup$