2
$\begingroup$

My goal is to turn this 5 by 3 table with named rows and columns into a Dataset. I found a method using association of associations in the document and I got stuck while preparing the keys and values to use for the association of associations. I would appreciate your help on how to convert the table below into a dataset.

Table

tableu

Related Document

Doc

Tried

keys=Outer[List,{B1,B2,B3,B4,B5},{{A1,A2,A3}},1]
data={{12,3,5},{3,6,11},{3,30,2},{9,14,7},{13,7,25}}
(* I got stuck while preparing the keys and values to use for the association of associations. Could I get help to make dataset of named rows and columns *) 
$\endgroup$

2 Answers 2

2
$\begingroup$

I am puzzled why this works:

keys = Flatten[
  Outer[List, 
   ToString /@ {B1, B2, B3, B4, B5}, {ToString /@ {A1, A2, A3}}, 1], 1]
data = {{12, 3, 5}, {3, 6, 11}, {3, 30, 2}, {9, 14, 7}, {13, 7, 25}}

dset = Association@
 MapThread[<|
    First[#1] -> Association@Thread[Last[#1] -> #2]|> &, {keys, data}]

Dataset[dset]

But this version, where A1, A2, B1, B2 are not strings doesn't:

keys = Flatten[Outer[List, {B1, B2, B3, B4, B5}, {{A1, A2, A3}}, 1], 1]
data = {{12, 3, 5}, {3, 6, 11}, {3, 30, 2}, {9, 14, 7}, {13, 7, 25}}

dset = Association@MapThread[<|First[#1] -> Association@Thread[Last[#1] -> #2]|> &, {keys, data}]

Dataset[dset]
$\endgroup$
1
  • $\begingroup$ Thank you for your answer!(※I have to look up MapThread and Thread[f[args]]] usage again) $\endgroup$ Commented Sep 25 at 0:23
2
$\begingroup$
Dataset[AssociationThread[{B1, B2, B3, B4, B5} -> (AssociationThread[{A1, A2, A3} -> #] & /@ data)]]

or

Dataset[AssociationThread[{A1, A2, A3}, #] & /@ AssociationThread[{B1, B2, B3, B4, B5}, data]]


Probably better to use strings as your keys:

Dataset[AssociationThread[ToString /@ {B1, B2, B3, B4, B5} -> (AssociationThread[ToString /@ {A1, A2, A3} -> #] & /@ data)]]

or

Dataset[AssociationThread[ToString /@ {A1, A2, A3}, #] & /@ AssociationThread[ToString /@ {B1, B2, B3, B4, B5}, data]]
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.