0
$\begingroup$

somewhat similarly to Replace Elements in a Dataset by rules, I'd like to replace the components in all columns named "microseconds" in a hierarchical dataset, with their respective quantity. Something like:

Replace[ myDataSet, Rule["microseconds", x_Integer] -> Rule["microseconds", Quantity[x, "Microseconds"], All]

$\endgroup$
1
1
$\begingroup$

My attempt (using initial input from @Tomi answer):

ds = Dataset[AssociationThread[{"Data 1", "Data 2", "Microseconds"} -> #] & /@ 
     Table[{RandomReal[], RandomReal[], RandomReal[]}, {i, 1, 10}]]

Now to change "Microseconds" column we apply appropriate function to it:

ds[All, {"Microseconds" -> (Quantity[#, "Microseconds"] &)}]

enter image description here

$\endgroup$
0
$\begingroup$

I'm a little confused as to what you are looking for. But lets say your data looks like:

    (* make fake data *) 

mydataset = 
  Table[{RandomReal[], RandomReal[], RandomReal[]}, {i, 1, 10}];
mydataset = Prepend[mydataset, {"Data 1", "Data 2", "Microseconds"}];
MatrixForm[mydataset]

enter image description here

We could do something like

mydataset[[2 ;;, {3}]] = 
 Quantity[#, "Microseconds"] & /@ mydataset[[2 ;;, {3}]] 

What I'm doing here is selecting all values from the second line to the last line, and then the {3}rd column. I then use Map (aka &/@) to apply the function Quantity[] to this dataset, and this results in: enter image description here

$\endgroup$
0
$\begingroup$

Replace the UnitConvert with Quantity if the column is not already a Quantity.

SeedRandom[123];
RandomSample[ResourceData["Sample Data: Titanic Survival"], 10][All,
  <|#, "Age" -> UnitConvert[#Age, "Microseconds"]|> &]
$\endgroup$
0
$\begingroup$

I think I solved my issue with KeyValuePattern

The final expression becomes:

ds[Replace[ #, 
   match : KeyValuePattern["microseconds" -> t_] :> 
    Append[ KeyDrop[match, "microseconds"], 
     "microseconds" ->  Quantity[t, "Microseconds"]], All ] &]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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