Message Boards Message Boards

0
|
122 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Conditionally modify values in a dataset

Posted 1 day ago

I have the dataset

dataset=Dataset[{
  <|"x" -> 320, "y" -> 178, "z" -> 291|>,
  <|"x" -> 360, "y" -> 114, "z" -> 109|>,
  <|"x" -> 400, "y" -> 186, "z" -> 265|>,
  <|"x" -> 440, "y" -> 419, "z" -> 469|>,
  <|"x" -> 480, "y" -> 228, "z" -> 251|>,
  <|"x" -> 520, "y" -> 153, "z" -> 129|>,
  <|"x" -> 560, "y" -> 284, "z" -> 177|>,
  <|"x" -> 600, "y" -> 246, "z" -> 323|>,
  <|"x" -> 640, "y" -> 333, "z" -> 108|>,
  <|"x" -> 680, "y" -> 383, "z" -> 261|>
  }]

I want to change the "z" values to 0 if the corresponding "x" value is smaller than 380 OR larger than 620.

If this dataset is a list, I can do

data = dataset // Values // Normal
Map[
 If[#[[1]] < 380 || #[[1]] > 620, {#[[1]], #[[2]], 0}, #] &
 , data
 ]
% // TableForm

enter image description here

But I need some help achieving it when the numbers are inside a dataset.

POSTED BY: Ehud Behar
4 Replies

Eric's elegant answer, but still polished up ...

dataset=dataset[All, <|"x" -> #x, "y" -> #y, "z" -> If[#x < 380 || #x > 620, 0, #z]|> &]
POSTED BY: Henrik Schachner
Posted 1 day ago

You could do something like this:

dataset = dataset[All, <|"x" -> #["x"], "y" -> #["y"], "z" -> If[#["x"] < 380 || #["x"] > 620, 0, #["z"]]|> &]
POSTED BY: Eric Rimbey
Posted 1 day ago

Now it looks simple. Thanks!

POSTED BY: Ehud Behar
Posted 18 hours ago

Here is an alternative using Map and ReplacePart

Map[If[#[["x"]] < 380 || #[["x"]] > 620, ReplacePart[#, "z" -> 0], #] &, dataset]

Can also be "polished" as Henrik suggested:

Map[If[#x < 380 || #x > 620, ReplacePart[#, "z" -> 0], #] &, dataset]
POSTED BY: Hans Milton
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract