Dear Dave :
Thank you for this well - crafted example that allowed me to learn new tricks . The following light blue marks are my usual usage in
work .
On the one hand, my colleagues think it is easier to understand, and on the other hand I can modify it to understand .
Pred Liu
Thank you for this well - crafted example that allowed me to learn new tricks . The following light blue marks are my usual usage in
work .
On the one hand, my colleagues think it is easier to understand, and on the other hand I can modify it to understand .
Pred Liu
Planets ExampleData
Planets ExampleData
In[]:=
planets=ExampleData[{"Dataset","Planets"}]
1 Create a separate dataset of all Moons Properties
1
Create a separate dataset of all Moons PropertiesIn[]:=
Join@@Query[Values,"Moons"]@planets;
planets[Values/*Apply[Join],"Moons"]
2 Create a Dataset with a list of all moons per planet
2
Create a Dataset with a list of all moons per planetIn[]:=
Query[All,{"Moons"},Keys]@planets;
planets[All,"Moons",Keys]
3 Create a sorted Dataset with the number of moons per planet
3
Create a sorted Dataset with the number of moons per planetIn[]:=
ReverseSort@Query[All,{"Moons"},Length]@planets;
planets[All,{"Moons"},Length]
4 Create a sorted Dataset with the total moon mass per planet
4
Create a sorted Dataset with the total moon mass per planetIn[]:=
ReverseSortBy[#Moons&]@Query[All,{"Moons"},Total@*Values,"Mass"]@planets;
planets[NumericalSort/*Reverse,{"Moons"},Values/*Total,"Mass"];
planets[All,{"Moons"},Values/*Total,"Mass"][ReverseSort]
5 Select the heaviest moons per planet
5
Select the heaviest moons per planetIn[]:=
Query[All,Query[TakeLargest[#,1]&,"Mass"]@#Moons&]@Select[Keys@#Moons!={}&]@planets;
planets[Select[#["Moons"]=!=<||>&],"Moons",MaximalBy[{"Mass"}],"Mass"]
In[]:=
Query[Transpose,Query[TakeLargest[#,1]&,"Mass"]@#Moons&]@Select[Keys@#Moons!={}&]@planets;
planets[Select[#["Moons"]=!=<||>&]/*Transpose,"Moons",MaximalBy[{"Mass"}],"Mass"]
In[]:=
Query[All,"Moons",TakeLargestBy[#Mass&,1]]@Select[Keys@#Moons!={}&]@planets;
planets[Select[#["Moons"]=!=<||>&],"Moons",MaximalBy[{"Mass"}]]
6 Select all planets with a larger mass than earth’s
6
Select all planets with a larger mass than earth’sIn[]:=
Query[Select[#Mass>planets["Earth","Mass"]&],{"Mass"}]@planets;
planets[Select[#["Mass"]>planets["Earth","Mass"]&],{"Mass"}]
7 Add a column with the planets’ density
7
Add a column with the planets’ densityIn[]:=
e3=QueryAll,<|"Mass"->#Mass,"Radius"->#Radius,"Density"->N#MassPi#Radius^3,"Moons"->#Moons|>&@planets;
4
3
planetsAll,Insert#,"Density"->N#["Mass"]Pi#["Radius"]^3,3&
4
3
8 Create a new Dataset of the planets’ density relative to earth’s
8
Create a new Dataset of the planets’ density relative to earth’sIn[]:=
ReverseSort@Query[All,<|"RelativeDensity"->With[{mass=Query["Earth","Density"]@e3},#Density/mass]|>&]@e3;
e3[All,<|"RelativeDensity"->With[{mass=Query["Earth","Density"]@e3},#Density/
mass]|>&][ReverseSortBy["RelativeDensity"]];
e3[NumericalSort/*Reverse,<|"RelativeDensity"->With[{mass=Query["Earth","Density"]@e3},#Density/mass]|>&]
9 Create a Dataset of the planets’ density relative to water’s
9
Create a Dataset of the planets’ density relative to water’sIn[]:=
Sort@QueryAll,<|"RelativeDensity"->Withmass=UnitConvert,Times["Kilograms",Power["Kilometers",-3]],#Densitymass|>&@e3;
e3NumericalSort,<|"RelativeDensity"->Withmass=UnitConvert,Times["Kilograms",
Power["Kilometers",-3]],#Densitymass|>&
10 Convert in-place Jupiter’s moons radii measured in earth’s moon units
10
Convert in-place Jupiter’s moons radii measured in earth’s moon unitsradii=planets["Earth","Moons","Moon","Radius"];MapAt[With[{r=Query["Earth","Moons",1,"Radius"]@planets},#/radii&],{"Jupiter","Moons",All,"Radius"}]@planets;
planets[MapAt[#/radii&,{"Jupiter","Moons",All,"Radius"}]]
11 Convert-in-place Mars’s Deimos and Jupiter’s Metis mass and radius resp. relative to Earth’s moon
11
Convert-in-place Mars’s Deimos and Jupiter’s Metis mass and radius resp. relative to Earth’s moonIn[]:=
m=planets["Mars","Moons","Deimos","Mass"]/planets["Earth","Moons","Moon","Mass"];r=planets["Jupiter","Moons","Metis","Radius"]/planets["Earth","Moons","Moon","Radius"];ReplacePart[{{"Mars","Moons","Deimos","Mass"}->m,{"Jupiter","Moons","Metis","Radius"}->r}]@planets;
planets[ReplacePart[{{"Mars","Moons","Deimos","Mass"}->m,{"Jupiter","Moons","Metis","Radius"}->r}]]
12 Convert in-place all planets’ and moons’ radii relative to Earth’s moon
12
Convert in-place all planets’ and moons’ radii relative to Earth’s moonradiimoon=planets["Earth","Moons",1,"Radius"];MapAt[With[{r=radiimoon},#/r&],{{All,"Moons",All,"Radius"},{All,"Radius"}}]@planets;
planetsReplaceAtmm_,{All,2}
mm
radiimoon
13 Add a density column to the Moons column
13
Add a density column to the Moons columnIn[]:=
e4=QueryAll,<|#,"Moons"->QueryAll,<|"Mass"->#Mass,"Radius"->#Radius,"Density"->N#MassPi#Radius^3|>&@#Moons|>&@e3;
4
3
e3All,MapAt#~Join~<|"Density"->N#MassPi#Radius^3|>&,{"Moons",All}
4
3
14 Create a Dataset with all moons’ densities relative to earth’s
14
Create a Dataset with all moons’ densities relative to earth’sIn[]:=
d=e4["Earth","Moons",1,"Density"];Join@@Query[Values,"Moons",All,#Density/d&]@e4//DeleteMissing//ReverseSort;
e4[Values/*Apply[Join]/*DeleteMissing/*ReverseSort,"Moons",All,#"Density"/d&]
15 Extract the properties of the Moon and Deimos
15
Extract the properties of the Moon and DeimosIn[]:=
Extract[{{"Earth","Moons",{"Moon"}},{"Mars","Moons",{"Deimos"}}}]@planets//Dataset;
planets["Earth","Moons",{"Moon"}]~Join~planets["Mars","Moons",{"Deimos"}]
16 Select all moons with a larger mass than earth’s Moon
16
Select all moons with a larger mass than earth’s MoonIn[]:=
em=planets["Earth","Moons","Moon","Mass"];Join@@Query[All,"Moons",Select[#Mass>em&],{"Mass"}]@planets;
planets[Join@@#&,"Moons",Select[#Mass>em&],{"Mass"}]
In[]:=
Query[All,"Moons",Select[#Mass>planets["Earth","Moons","Moon","Mass"]&]/*Keys]@planets;
planets[All,"Moons",Select[#Mass>planets["Earth","Moons","Moon","Mass"]&]/*Keys]
17 Rename the planets mass and radius column headers
17
Rename the planets mass and radius column headersIn[]:=
Query[All,KeyMap[Replace[{"Mass"->"PlanetMass","Radius"->"PlanetRadius"}]]]@planets;
planets[All,<|"PlanetMass"->"Mass","PlanetRadius"->"Radius","Moons"->"Moons"|>]
18 Replace the Moons’ mass and radius column headers
18 Replace the Moons’ mass and radius column headers
In[]:=
Query[All,<|#,"Moons"-><|Query[All,KeyMap[Replace[{"Mass"->"MoonMass","Radius"->"MoonRadius"}]]]@#Moons|>|>&]@planets;
planets[MapAt[<|"MoonMass"->#["Mass"],"MoonRadius"->#["Radius"]|>&,{All,3,All}]]
19 Rename the planets names to Latin ones
19
Rename the planets names to Latin onesIn[]:=
Query[KeyMap[Replace[{"Mercury"->"Mercurius","Venus"->"Lucifer","Earth"->"Terra","Mars"->"Martius","Jupiter"->"Lovis","Saturn"->"Saturnus","Neptune"->"Neptunus"}]],All]@planets;
planets[<|"Mercurius""Mercury","Lucifer""Venus","Terra""Earth","Martius""Mars","Lovis""Jupiter",
"Saturnus""Saturn","Uranus"->"Uranus","Neptunus""Neptune"|>]
20 Rename the two Mars moons to “satellite” 1 and 2
20 Rename the two Mars moons to “satellite” 1 and 2
23 Remove Saturn’s moons
23 Remove Saturn’s moons
25 Remove the entire Moons’ mass column
25 Remove the entire Moons’ mass column
28 Filter the planets Dataset by a moon mass criterion
28 Filter the planets Dataset by a moon mass criterion
29 Sort the planets by name
29 Sort the planets by name
30 Sort the moons by name
30 Sort the moons by name
31 Swap the planets’ mass and radius columns
31 Swap the planets’ mass and radius columns
32 Swap the moons’ mass and radius columns
32 Swap the moons’ mass and radius columns
33 Swap the row keys of Uranus and Saturn
33 Swap the row keys of Uranus and Saturn
34 Swap the row keys of Phobos and Deimos
34 Swap the row keys of Phobos and Deimos