1
$\begingroup$

Suppose that the information for constructing a dataset is split over two separate files: one file contains the numeric data, and a second file contains the names of the columns.

Is there a simple way to create a dataset from these two separate files?

$\endgroup$
1

2 Answers 2

6
$\begingroup$
columnames=CharacterRange["a","h"];
data= RandomInteger[9,{20,8}];

ds=Dataset[AssociationThread[columnames->#]&/@data]

Mathematica graphics

$\endgroup$
2
$\begingroup$

OK, I found a second way. Like kglr's solution, this one also uses AssociationThread, but applies it only once:

makeDataset[columns_, columnLabels_] := Module[{labeledColumns, records},
   labeledColumns = AssociationThread[columnLabels, columns];
   records = Transpose[labeledColumns, AllowedHeads -> All];
   Dataset[records]
];

The importer can be easily written in terms of makeDataset:

readDataset[datafile_, headerfile_] := Module[{columns, columnLabels},
   columns = Transpose[Import[datafile]];
   columnLabels = First[Import[headerfile]];
   makeDataset[columns, columnLabels]
];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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