Git Product home page Git Product logo

Comments (7)

pbeshai avatar pbeshai commented on August 22, 2024 1

I see! You can use the function API to accomplish this with select. e.g.:


function firstNKeys(n) {
  return items => Object.keys(items[0]).slice(0, n)
}

output = tidy(input,
  mutate({ foo: 'foo' }),
  select([firstNKeys(1), 'foo', everything()])
);

Or


function myCustomKeys(items) {
  const keys = Object.keys(items[0])
  return [keys[0], 'foo', ...keys.slice(1)]
}

output = tidy(input,
  mutate({ foo: 'foo' }),
  select([myCustomKeys])
);

from tidy.

pbeshai avatar pbeshai commented on August 22, 2024

You can use selectors with select to make this less painful, e.g.:

tidy(input,
  mutate({ foo: 'foo' }),
  select(['foo', everything()])
);

This puts foo as the first key. Does that work for you?

from tidy.

mhkeller avatar mhkeller commented on August 22, 2024

That's interesting but I don't think it covers my use case. I wanted to insert foo as the second column. This may sound like an absurdly nitpicky requirement but it was the most logical ordering of the sheet. I was also converting something from d3-nest to use tidjs and i wanted the outputs to match exactly so i could easily ensure the conversion was successful.

from tidy.

mhkeller avatar mhkeller commented on August 22, 2024

Instead of adding this as an option to mutate maybe there's another tidy function called reorder or move that would allow you to do specify a column name and its new index like:

reorder('foo', 1)

from tidy.

mhkeller avatar mhkeller commented on August 22, 2024

Thanks for these examples. In your first example, would the first key be duplicated when you call everything() or does it somehow know not to include it?

I think I would still find it easier and more readable to have it in some kind of tidy function but I understand if this is out of scope for what you want to do.

from tidy.

pbeshai avatar pbeshai commented on August 22, 2024

Once a key has been selected, it stays in that spot, so there's no issue duplicating it with everything(). We could add firstKeys and lastKeys as selectors, but they'd operate identically to the firstNKeys example I put above, so I'd recommend for now just making that a utility function you use in your code base. If there's sufficient other interest we can add it, but it seems a bit niche to me at least right now.

from tidy.

mhkeller avatar mhkeller commented on August 22, 2024

Sounds good

from tidy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.