Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I would like to translate a imported txt file to Dataset.

test = Import["E:\\mathematica\\example.txt", "table", 
       {"FieldSeparators" -> ",", "CharacterEncoding" -> "ASCII"}]

The result is:

{{"naam A", "naam B", "naam C", "naam D"}, {1, 2, 1.2, "a"}, {2, 3, 2.3, "b"}, {3, 4, 2.3, "c"}, {4, 5, 4.5, "d"}, {5, 6, 5.6, "e"}, {6, 7, 6.7, "f"}, {7, 8, 7.8, "g"}, {8, 9, 8.9, "h"}, {9, 10, 9.1, "i"}, {10, 11, 10.1, "j"}}

Now I would like to translate it to Dataset which is new in Methematica 10.0.

dataset = Dataset[{
   <|"naam A" -> 1, "naam B" -> "2", "naam C" -> {1.1}, "naam D" -> {a}|>,
   <|"naam A" -> 2, "naam B" -> "3", "naam C" -> {1.2}, "naam D" -> {b}|>,
   <|"naam A" -> 3, "naam B" -> "4", "naam C" -> {1.3}, "naam D" -> {c}|>,
   ....
   }]

I tried it with the next code:

data = Dataset[<|"key1" -> test[[All, 1]], "key2" -> test[[All, 2]], 
   "key3" -> test[[All, 3]], "key4" -> test[[All, 4]]|>]

This code gives the wrong output. Anyone a suggestion how to do it?

share|improve this question
1  
Where did {1.1} come from? 1.1 does not exist in the Imported data. And is {} necessary? Anyway, you can code like "Module[{header = First@test}, Inner[Rule, header, #, Association] & /@ (Rest@test) ] // Dataset". –  Tetsuo Ichii Jul 22 at 12:02
1  
you could just use SemanticImport which generates a Dataset for you –  gogoolplex Jul 22 at 12:03
    
What error are you getting? "Unknown box name TypeSystem`TypeMap"? –  mfvonh Jul 22 at 12:12
    
@gogoolplex, SemanticImport doesn't guarantee anything and in 10.0.0 is not ready for prime time. –  alancalvitti Jul 22 at 13:36

1 Answer 1

gogoolplex's suggestion to use SemanticImport is probably the easiest solution to your particular problem, but here is a way to assemble a dataset yourself.

table = {{"naam A", "naam B", "naam C", "naam D"}, {1, 2, 1.2, 
"a"}, {2, 3, 2.3, "b"}, {3, 4, 2.3, "c"}, {4, 5, 4.5, "d"}, {5, 6,
 5.6, "e"}, {6, 7, 6.7, "f"}, {7, 8, 7.8, "g"}, {8, 9, 8.9, 
"h"}, {9, 10, 9.1, "i"}, {10, 11, 10.1, "j"}};

With[{header = First@table}, 
  AssociationThread[header -> #] & /@ Rest@table] // Dataset

enter image description here

share|improve this answer
    
Try replacing one of the values with missing data, eg the empty String "" and see if SemanticImport still works. –  alancalvitti Jul 22 at 13:39
    
This is also a useful approach if you already have an array of data (i.e. from a database query in SQLExecute) and would like to turn it into a Dataset without reading and writing files. –  Italianice Jul 31 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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