1
$\begingroup$

I have provided a dataset bellow and I would like to store the data as an Association that I can query. Such that e.g. dataSet["Blink"] returns all of the Blink data I have stored.

dataSet=
{"Blink" -> {-7.80207, -6.81625, -8.95471, -7.94497}, 
"Blink" -> {-8.04093, -7.15899, -8.87598, -8.20657}, 
"Blink" -> {-8.30509, -7.26396, -8.97337, -8.43762}, 
"Blink" -> {-7.93339, -7.26834, -8.96581, -8.06195}, 
"Blink" -> {-7.51735, -7.54408, -8.60107, -7.58897}, 
"Blink" -> {-7.85602, -7.01129, -8.93354, -7.94467}, 
"Blink" -> {-7.81115, -6.82194, -8.94245, -7.94546}, 
"Blink" -> {-8.08047, -7.011, -8.95986, -8.18716}, 
"Blink" -> {-8.00258, -6.94715, -8.99978, -8.12634}, 
"Blink" -> {-7.78044, -7.82569, -8.95928, -7.86188},
"Brow" ->{-6.87407, -7.6617, -7.61289, -6.8707}, 
"Brow" -> {-6.96051, -7.85301, -7.7866, -6.98906}, 
"Brow" -> {-6.81728, -7.58617, -7.55388, -6.84338}, 
"Brow" -> {-6.99797, -7.81099, -7.71307, -7.03809}, 
"Brow" -> {-7.15693, -7.98096, -7.7831, -7.22676}, 
"Brow" -> {-7.21755, -8.15857, -7.89819, -7.19838}, 
"Brow" -> {-7.49575, -7.12824, -8.8197, -7.47529}, 
"Brow" -> {-6.90696, -7.66507, -7.5255, -6.87899}, 
"Brow" -> {-6.85744, -7.58705, -7.75462, -6.86313}, 
"Brow" -> {-6.77376, -7.61734, -7.63576, -6.82904},
"Control" -> {-5.95623, -6.22505, -8.91221, -5.95269}, 
"Control" -> {-6.03934, -5.74061, -8.9441, -6.00019}, 
"Control" -> {-6.17787, -5.8216, -8.9672, -6.08643}, 
"Control" -> {-5.91856, -5.86446, -8.88035, -5.98493}, 
"Control" -> {-6.29312, -6.18381, -9.01361, -6.28984}, 
"Control" -> {-6.10166, -6.07776, -8.92354, -5.98715}, 
"Control" -> {-6.14962, -5.80242, -8.97278, -6.06053}, 
"Control" -> {-5.92913, -5.76531, -8.97248, -5.88653}, 
"Control" -> {-6.02622, -6.01878, -8.93221, -6.03248}, 
"Control" -> {-5.86525, -6.28864, -8.91767, -5.84206}, 
"Smile" -> {-6.99006, -7.06889, -9.0568, -7.14185}, 
"Smile" -> {-6.28371, -5.58937, -8.98667, -6.62768}, 
"Smile" -> {-6.45543, -5.93095, -9.08733, -6.65501}, 
"Smile" -> {-6.46983, -5.82567, -9.01648, -6.89222}, 
"Smile" -> {-6.59885, -5.70165, -9.11828, -6.84112}, 
"Smile" -> {-6.34537, -5.97214, -9.01509, -6.35281}, 
"Smile" -> {-6.4589, -5.62745, -9.07188, -6.62343}, 
"Smile" -> {-6.39873, -5.63368, -9.07097, -6.66046}, 
"Smile" -> {-6.50042, -5.70267, -9.07739, -6.61835}, 
"Smile" -> {-6.8984, -7.01631, -9.07822, -6.91803}}

I have tried:

association = <||>;
Do[AppendTo[association, Association@dataSet[[i]]], {i, 1,Length[dataSet]}];

and this only returns:

<|"Blink" -> {-7.78044, -7.82569, -8.95928, -7.86188}, 
"Brow" -> {-6.77376, -7.61734, -7.63576, -6.82904}, 
"Control" -> {-5.86525, -6.28864, -8.91767, -5.84206}, 
"Smile" -> {-6.8984, -7.01631, -9.07822, -6.91803}|> 

Which is just a small portion of the data set. How do I convert my data set into an Association?

$\endgroup$
4
  • $\begingroup$ How do you want the data associated with, say, "Smile" returned? Do you want a Flattened list of all of the data? Or do you want a list of the four element lists? $\endgroup$
    – march
    Commented Feb 26, 2016 at 19:13
  • $\begingroup$ A list of four element lists. $\endgroup$ Commented Feb 26, 2016 at 19:15
  • 1
    $\begingroup$ Then @Kuba has your answer below. $\endgroup$
    – march
    Commented Feb 26, 2016 at 19:16
  • 2
    $\begingroup$ You want Merge[dataSet, Identity]. Identity can be changed to a different function if you want to process the combined lists differently. $\endgroup$
    – Mr.Wizard
    Commented Feb 26, 2016 at 21:14

2 Answers 2

6
$\begingroup$
GroupBy[dataSet, First -> Last]

Is that ok?

$\endgroup$
6
  • $\begingroup$ This works wonderfully, thank you! $\endgroup$ Commented Feb 26, 2016 at 19:16
  • $\begingroup$ dimData = GroupBy[Table[ state -> DimensionReduce[dataSet[state]], {state, Keys[dataSet]}], First -> Last]; Any idea why this is nesting the lists inside the association?? $\endgroup$ Commented Feb 26, 2016 at 19:40
  • $\begingroup$ @WilliamJohn-PierreDuhe GroupBy[dataSet, First -> Last, function] $\endgroup$
    – Kuba
    Commented Feb 26, 2016 at 19:42
  • $\begingroup$ Wahhhhhh, so simple. Thanks again haha $\endgroup$ Commented Feb 26, 2016 at 19:43
  • $\begingroup$ Merge seems cleaner here, IMO. $\endgroup$
    – Mr.Wizard
    Commented Feb 26, 2016 at 21:15
2
$\begingroup$

Or...

Cases[dataSet , ("Blink" -> x_) -> x]
$\endgroup$

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