Message Boards Message Boards

Apply UnitConvert to nested Entity data?

GROUPS:

I've got a list of planet names, images, and distances from Wolfram Alpha. I want to change the distances from au to light minutes.

EntityValue[EntityClass["Planet", All], {"Name", "Image", "DistanceFromEarth"}]

How can i use UnitConvert on the values I get from "DistanceFromEarth? I've tried the following:

EntityValue[
 EntityClass["Planet", All], {"Name", "Image", 
  UnitConvert["DistanceFromEarth", "light minutes"]}]

get and error message: Quantity: Unable to interpret unit specification DistanceFromEarth I guess that means it is trying to apply UnitConvert before it has loaded the value "Distance from Earth"

The following works in that it gives me a list of the distances in light minutes:

EntityValue[EntityClass["Planet", All], "DistanceFromEarth"]

{Quantity[11.3015, "LightMinutes"], Quantity[3.46009, "LightMinutes"], Quantity[0., "LightMinutes"], Quantity[16.3815, "LightMinutes"], Quantity[39.8955, "LightMinutes"], Quantity[86.8167, "LightMinutes"], Quantity[170.83, "LightMinutes"], Quantity[257.162, "LightMinutes"]}

However i don't see how to apply the UnitConvert function inside of the above so I get a nice list of the planet name, image, and distance in light units. I looked at using Apply to the above, but I don't see how to add the convert options to the function and choose only the 3rd element of each row to apply it too.

Convert [a,"light minutes"] (where a is the list I obtain above, but limit it to the 3rd element of each row

Apply[Convert, a < - what do I do here to add the function, restrict to 3rd element in each row, and input the conversion units "light minutes"? Must be barking up the wrong tree here.

POSTED BY: Marshall Crenshaw
Answer
10 months ago

There are numerous ways to post-process. After you got your data in au as

data=EntityValue[EntityClass["Planet",All],{"Name","Image","DistanceFromEarth"}];

You can use any of these methods and there probably many more:

data/.x_Quantity->UnitConvert[x,"LightMinutes"]

MapAt[UnitConvert[#,"LightMinutes"]&,#,3]&/@data

Transpose[MapAt[UnitConvert[#,"LightMinutes"]&,Transpose[data],3]]

Cases[data,{a_,b_,c_}:>{a,b,UnitConvert[c,"LightMinutes"]}]
POSTED BY: Vitaliy Kaurov
Answer
10 months ago

in addition to Vitaliy's long list of great options you can also do it with one function:

MapAt[UnitConvert[#, "light minutes"] &, planets, {All, 3}]

which says to map the UnitConvert over all the planet lists but apply it to the third element of each planet list.

POSTED BY: Neil Singer
Answer
10 months ago

Group Abstract Group Abstract