1
$\begingroup$

I need to make queries and operations on a large dataset. What is the easiest way to add keys to matrix so that it is transformed to a dataset, which can later be used to make queries?

Let's take this matrix:

mat = RandomInteger[{0, 1}, {50, 20}];
v = RandomInteger[{100, 110}, 50];
mat1 = Join[List /@ v, mat, 2] // MatrixForm

Now I want to insert "column names" and thereby change tha matrix to a Dataset object/SparseArray type without making an association with every entry by hand, like in the basic example in the help function.

dataset = 
  Dataset[
    {<|"a" -> 101, "b" -> 1, "c" -> 0|>,
     <|"a" -> 101, "b" -> 1, "c" -> 0|>,
     <|"a" -> 107, "b" -> 1, "c" -> 1|>,
     <|"a" -> 106, "b" -> 1, "c" -> 0|>,
     <|"a" -> 102, "b" -> 0, "c" -> 1|>,
     <|"a" -> 101, "b" -> 0, "c" -> 0|>}] 

I'm sure there is an easy way to do this basic operation. I would also be grateful if you would point me to some resources with example code operations like this.

$\endgroup$

closed as unclear what you're asking by m_goldberg, user9660, MarcoB, RunnyKine, Jens May 2 '16 at 17:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ The dataset you describe as the desired result seems to have no relationship to mat1, so it is not clear what you are asking. $\endgroup$ – m_goldberg May 1 '16 at 13:02
  • $\begingroup$ The dataset was just there to illustrate the creation "by hand" which I want to avoid. The anwer below solves the problem. Thank you $\endgroup$ – Zappageck May 3 '16 at 5:29
4
$\begingroup$
mat = RandomInteger[{0, 1}, {5, 10}];
v = RandomInteger[{100, 110}, 5];
(mat1 = Join[List /@ v, mat, 2]) // MatrixForm

Mathematica graphics

ds=Dataset[mat1]

Mathematica graphics

colnames=CharacterRange["a","z"][[;;11]];
ds2=Dataset[AssociationThread[colnames->#]&/@mat1]

Mathematica graphics

$\endgroup$

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