5
$\begingroup$

This is my original data:

data = {<|"GSMName" -> "GSM2142639", 
    "tumor (t) or normal (n)" -> "L886T", "stage tnm" -> "2", 
    "histology" -> "1", "surgery date" -> "2006-01-24", "age" -> "71",
     "gender" -> "male", "vital date" -> "2011-05-10", "dead" -> "1", 
    "smoking" -> "2", "ps who" -> "0"|>, <|"GSMName" -> "GSM2142640", 
    "tumor (t) or normal (n)" -> "L887T", "stage tnm" -> "1", 
    "histology" -> "2", "surgery date" -> "2007-11-06", "age" -> "77",
     "gender" -> "male", "vital date" -> "2008-11-14", "dead" -> "1", 
    "smoking" -> "2", "ps who" -> "0"|>, <|"GSMName" -> "GSM2142641", 
    "tumor (t) or normal (n)" -> "L890T", "stage tnm" -> "4", 
    "histology" -> "2", "surgery date" -> "2010-02-02", "age" -> "56",
     "gender" -> "female", "vital date" -> "2013-07-20", 
    "dead" -> "1", "smoking" -> "1", 
    "ps who" -> "1"|>, <|"GSMName" -> "GSM2142642", 
    "tumor (t) or normal (n)" -> "L511N", 
    "Custom" -> "30"|>, <|"GSMName" -> "GSM2142643", 
    "tumor (t) or normal (n)" -> "L532N", 
    "Custom" -> "30"|>, <|"GSMName" -> "GSM2142644", 
    "tumor (t) or normal (n)" -> "L561N", 
    "Custom" -> "31"|>, <|"GSMName" -> "GSM2142645", 
    "tumor (t) or normal (n)" -> "L563N"|>, <|"GSMName" -> 
     "GSM2142646", 
    "tumor (t) or normal (n)" -> "L566N"|>, <|"GSMName" -> 
     "GSM2142647", "tumor (t) or normal (n)" -> "L572N", 
    "Custom" -> "25"|>};

Note that not every row has exactly the same key, so I can't get a normal Dataset to export a ".xslx" file:

Dataset[data]

enter image description here

Now I want to complete the missing key with an empty string and get a normal Dataset that I can use to export the .xslx file: enter image description here This is my current solution:

keyall=Union@@Keys[data];
data=Table[AssociateTo[row,Thread[Complement[keyall,Keys[row]]->""]],{row,data}];
Dataset[ResourceFunction["KeySortLike"][#,Keys[First[data]]]&/@data,MaxItems->{All,All}]

Is there a more appropriate method?

$\endgroup$

1 Answer 1

6
$\begingroup$
Dataset[KeyUnion[data, "" &], MaxItems -> {All, All}] (*thanks: SjoerdSmith*)

enter image description here

$\endgroup$
2
  • 4
    $\begingroup$ KeyUnion[data, "" &] is preferable to ReplaceAll $\endgroup$ Jun 30, 2023 at 13:37
  • $\begingroup$ Thank you so much @SjoerdSmit; I will update. $\endgroup$
    – kglr
    Jun 30, 2023 at 14:18

Your Answer

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

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