1
$\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

4 Answers 4

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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.