How do I extract religion fractions data from CountryData?

In this code:

data = CountryData[“UnitedStates”, “ReligionsFractions”]

I need to extract all of the names of the various religions as a variable and the fraction as an associated value.

=================

1

 

You can use data = Association @@ CountryData[“UnitedStates”, “ReligionsFractions”]; to create the association, then Keys@data to get the list of religions and Values@data to get the fractions.
– JasonB
Jul 18 at 16:42

1

 

How about Evaluate[ToExpression /@ data[[All, 1, 2]]] = data[[All, 2]]? This sets the religions as variables names, so Protestant evaluates to 0.513. However, I don’t like cluttering the namespace with lots of global variables, so Associations are better, as mentioned by @JasonB. A modified version is AssociationThread[data[[All, 1, 2]] -> data[[All, 2]]].
– march
Jul 18 at 16:42

=================

1 Answer
1

=================

data = CountryData[“UnitedStates”, “ReligionsFractions”];
{#[[1, 2]], #[[2]]} & /@ data

{{“Protestant”, 0.513}, {“RomanCatholic”, 0.239}, {“LatterDaySaints”, 0.017},
{“Jewish”, 0.017}, {“OtherChristian”, 0.016}, {“Buddhist”, 0.007},
{“Muslim”, 0.006}, {“OtherNone”, 0.186}}

or

Set @@@ ({Symbol[#[[1, 2]]], #[[2]]} & /@ data);
{Protestant, RomanCatholic, LatterDaySaints, Jewish, OtherChristian, Buddhist,
Muslim, OtherNone}

{0.513, 0.239, 0.017, 0.017, 0.016, 0.007, 0.006, 0.186}

or

Association[#[[1, 2]] -> #[[2]] & /@ data]

<|"Protestant" -> 0.513, “RomanCatholic” -> 0.239, “LatterDaySaints” -> 0.017,
“Jewish” -> 0.017, “OtherChristian” -> 0.016, “Buddhist” -> .007,
“Muslim” -> 0.006, “OtherNone” -> 0.186|>

  

 

One more, equivalent output to the last one, data /. Entity[a_, b_] :> b // Association
– JasonB
Jul 18 at 17:45