Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set colum index for mutations #68

Open
mhkeller opened this issue Nov 7, 2022 · 7 comments
Open

Set colum index for mutations #68

mhkeller opened this issue Nov 7, 2022 · 7 comments

Comments

@mhkeller
Copy link

mhkeller commented Nov 7, 2022

Does anyone else run into the problem where you want to add a new column via mutate but you don't want that new key to be added at the very end? One common example is I have a bunch of state fips codes that are my first column and I add the full state name via a lookup with a mutate call. I'd like to have that full state name then be the second column so my spreadsheet is easier to read. I could do a select / pick call but I'd have to write out all of my columns and that is a bit verbose.

Perhaps mutate could be supplied an index and it inserts the key at that index? Open to other workarounds people have found for this...

@pbeshai
Copy link
Owner

pbeshai commented Nov 8, 2022

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?

@mhkeller
Copy link
Author

mhkeller commented Nov 8, 2022

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.

@mhkeller
Copy link
Author

mhkeller commented Nov 8, 2022

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)

@pbeshai
Copy link
Owner

pbeshai commented Nov 8, 2022

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])
);

@mhkeller
Copy link
Author

mhkeller commented Nov 9, 2022

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.

@pbeshai
Copy link
Owner

pbeshai commented Nov 9, 2022

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.

@mhkeller
Copy link
Author

Sounds good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants