How can I change some elements in dataset by related column and row


I want to replace 0. value in [row_5,B] become value of [row_5,A], and the value is changing in many rows, and I don’t like to type it {0.0->1.516*10^6}

One solution of mine is row_5 lines, and split the data in parts 2=part1,part2,
2.change part1[with row_5] modifiy row B by rowA
3.combine part1 and part2

and I do this in the association, not the dataset.




Like this? ReplacePart[dataset, {{2, “a”}, {5, “a”}} -> y]
– JasonB
Apr 12 at 12:37



@JasonB yes, may be values of y is y1, y2, also some ways like Query, dataset[All, something] is welcome
– HyperGroups
Apr 12 at 12:38


2 Answers


To change all rows of type “row_5” to that the “B” value is replaced with the corresponding “A” value, we can write:

dataset[All, <| #, "B" -> Replace[#type, {“row_5” -> #A, _ -> #B}] |> &]

If we only want to change the cases where the “B” value is zero, we can add that as an extra condition:

dataset[All, <| #, "B" -> Replace[#type, {“row_5” :> #A /; #B == 0., _ -> #B}] |>&]

If we wanted to replace all zero values in the B column with the corresponding A value, we could do this:

dataset[All, <| #, "B" -> Replace[#B, 0. -> #A] |> &]



Hi, yes, but sometimes, it’s not 0. but 1.15*10^100, and maybe not so easy to type it rightly, since the table is large,some rows are hidden, and If some 0.0 is not in type=row_5, I don’t want it to be replaced.
– HyperGroups
Apr 12 at 14:29



So you want to change all row_5 entries, even when the value is not zero?
– WReach
Apr 12 at 14:32



yes, that’s the real purpose, better if could do a check, if value of [row_5,B] is 0, then replaced by [row_5,A]
– HyperGroups
Apr 12 at 14:34



ha, I find it , thanks. dataset[All,<|#,"B"->If[#type==”row_5″,#A,#B]|>&]
– HyperGroups
Apr 12 at 14:40

Probably more than one way to do this, but scrolling through the documentation I found this,

dataset[All,{n->f}] selectively maps fff onto the nthnthn^{th} column

What you want to do is map a replacement rule on to the “a” column,

dataset[All, {“a” -> (# /. {2 -> y1, 5 -> y2} &)}]



sorry, I changed the dataset and make the problem clearer
– HyperGroups
Apr 12 at 14:08



Anyway, thanks, I find the answer, via WReach’s answer.
– HyperGroups
Apr 12 at 14:41