1
$\begingroup$

This question already has an answer here:

My dataset is pretty simple so far:

Questions = Dataset[{
<|"Statement" -> "What is the capital of Switzerland?", "Answer" -> "Bern", 
"Incorrect Answers" -> {"Zurich", "Berlin", "Vienna"}|>,
<|"Statement" -> "Who is the king of the jungle?", "Answer" -> "Lion", 
"Incorrect Answers" -> {"Giraffe", "Mouse", "Monkey"}|>,
<|"Statement" -> "2+2?" , "Answer" -> "4", "Incorrect Answers" -> {"15", "13", "1e15"}|>}]

Questions

If I try the following, everything goes right:

NewQuestion1 = <|"Statement" -> "To be...", "Answer" -> "or not to be", 
"Incorrect Answers" -> {"or to eat", "continued", "dummy"}|>;
Append[Questions, NewQuestion1]

NewQuestions1

Nevertheless, if I delete the dummy answer of the NewQuestion, it will not work:

NewQuestion2 = <|"Statement" -> "To be...", "Answer" -> "or not to be", 
"Incorrect Answers" -> {"or to eat", "continued"}|>;
Append[Questions, NewQuestion2]

NewQuestion2

This issue seems weird since you can declare a data set with uneven lists from the beginning:

Questions = Dataset[{
   <|"Statement" -> "What is the capital of Switzerland?", 
    "Answer" -> "Bern", 
    "Incorrect Answers" -> {"Zurich", "Berlin", "Vienna"}|>,
   <|"Statement" -> "Who is the king of the jungle?", 
    "Answer" -> "Lion", 
    "Incorrect Answers" -> {"Giraffe", "Mouse", "Monkey"}|>,
   <|"Statement" -> "2+2?" , "Answer" -> "4", 
    "Incorrect Answers" -> {"15", "13", "1e15"}|>,
   <|"Statement" -> "To be...", "Answer" -> "or not to be", 
    "Incorrect Answers" -> {"or to eat", "continued"}|>
   }]

QuestionsUneven

Any suggestions on how to append NewQuestion2 and/or explanations of why this error is happening?

$\endgroup$

marked as duplicate by RunnyKine, Mr.Wizard Jul 28 '14 at 18:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ I read that question before posting mine and find it different since m_goldberg is trying to append an element of a different class (I must say that I found that quite interesting and certainly something for Mathematica to improve). Here the thing is that I want to append another list. $\endgroup$ – Carlos Chida Jul 28 '14 at 18:09
  • 1
    $\begingroup$ Insert[Questions, NewQuestion2, -1]. All credits to @m_goldberg in the linked Q&A. $\endgroup$ – seismatica Jul 28 '14 at 18:15
  • $\begingroup$ Super! Not as "elegant" as I am used to in Mathematica, but works perfectly. Thank you very much. $\endgroup$ – Carlos Chida Jul 28 '14 at 18:19
  • $\begingroup$ I voted for this being a duplicate. (Which, as a moderator, takes immediate effect.) The underlying problem appears to be the same, and the solutions are the same. Since your problem is resolved I don't see the need to keep this open longer. $\endgroup$ – Mr.Wizard Jul 28 '14 at 18:28
  • $\begingroup$ I enquire against since even though the solution is the same, the fact that I explained in the first comment here made it hard to get at the first sight that it could work. Moreover, we talking about undocumented functions and extending the its use. $\endgroup$ – Carlos Chida Jul 28 '14 at 18:38
4
$\begingroup$

Well, here is a workaround:

First, we load the package TypeSystem:

Needs["TypeSystem`"]

Now we define the Dataset to expect any type:

Questions = 
 Dataset[{<|"Statement" -> "What is the capital of Switzerland?", 
    "Answer" -> "Bern", 
    "Incorrect Answers" -> {"Zurich", "Berlin", "Vienna"}|>, <|
    "Statement" -> "Who is the king of the jungle?", 
    "Answer" -> "Lion", 
    "Incorrect Answers" -> {"Giraffe", "Mouse", "Monkey"}|>, <|
    "Statement" -> "2+2?", "Answer" -> "4", 
    "Incorrect Answers" -> {"15", "13", "1e15"}|>}, 
  Vector[Struct[{"Statement", "Answer", "Incorrect Answers"}, {AnyType, AnyType, AnyType}]]]

Note the undocumented second argument of Dataset

We can now append with no problems:

NewQuestion2 = <|"Statement" -> "To be...", "Answer" -> "or not to be", 
   "Incorrect Answers" -> {"or to eat", "continued"}|>;

Then

Append[Questions, NewQuestion2]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Amazing, thanks. Do you also know why this happens? And do you know where does this second argument work? For instance if I used Join to add more columns, could I also specify there that they are AnyType? $\endgroup$ – Carlos Chida Jul 28 '14 at 18:33
  • $\begingroup$ @CarlosChida, I don't know for sure why it happens but think it's a bug. The second argument is undocumented and I don't know the details of its functionality. $\endgroup$ – RunnyKine Jul 28 '14 at 18:37

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