5
$\begingroup$

I could not find an answer on this fundamental question, even though I was reading the references on Dataset and Association and browsing through the board. I want to use Datasetas a data base with changing data for the sake of readability of the code. To get a value by f["two","a"] is more associative for the reader than f[[2,2]], especially if the number of rows of f and the meaning of the rows change. I have the following data:

f = Dataset[<|
   "one" -> <|"a" -> Quantity [20, "Hour"], 
     "b" -> Quantity[0.5, "Hour"]|>, 
   "two" -> <|"a" -> Quantity [8, "Hour"], "b" -> Quantity[2, "Hour"]|>|>]

and need to change for instance the "b" value of row "two" to a new value. At best in a readable form. My guess was f["two","b"]=new; but this does not work. I also tried Replacebut also could not get it working.

$\endgroup$
4
$\begingroup$

ReplacePart[] works:

ReplacePart[f, {"two", "b"} -> Quantity[10, "Minutes"]]

I don't know how efficient this is if you're doing multiple changes, however.

$\endgroup$
0
$\begingroup$

With the link of WReach if found that this works as well:

f[{"two" -> (<|#, "b" -> Quantity [0, "Hour"]|> &)}]

However, for the sake of code readability I favour J.M.'s answer.

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