Skip to content

Best way to "replace" data? #297

Discussion options

You must be logged in to vote

After wrapping my head around Table Expressions, I came up with the following solution for my issue:

negativeToZero = function(dt) {
  // Programmatically create a values object that replaces values
  const ids = dt.columnNames();
  let values = {}
  ids.map(id => values[id] = "d => d['" + id + "'] <= 0 ? 0 : d['" + id + "']");
  // Create and return the dt with all negative values replaced
  let new_dt = dt.derive(values);
  return(new_dt)
}

This isn't built as table method but it gets the job done.

let dt = aq.table({
    'a': [1,2,3,-1],
    'b': [7,8,-1,9]
  })

negativeToZero(dt).print();

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by jonathancallahan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant