7

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.

Eisbär
  • 1,476
  • 7
  • 14

2 Answers2

8

ReplacePart[] works:

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

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

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
0

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.

Eisbär
  • 1,476
  • 7
  • 14