0

I'm trapped by what should be a simple scenario. I want to use the unique values in one column of a tibble to drive a series of subset outputs. I used 'unlist' to create a vector of "Supplier" (in this case with 28 unique values) and hope to write a loop whereby for each unique value I can return the subset from the table. Kind of like pulling apart a pivot table in excel and printing it sheet by sheet.

My data looks like this (sample only):

Supplier Receipt PO Tracking
Denco Audio Ltd 23/02/2022 232960 AAAC0002630
Devonport Chocolates 23/02/2022 232969 4519880002717001AKL001AP
Devonport Chocolates 23/02/2022 232843 4519880002716901AKL001AS
Electric Pukeko 23/02/2022 232954 ca0006969325
Glen Dimplex New Zealand Ltd 23/02/2022 232818 4603720008201401HAS011AS
Glen Dimplex New Zealand Ltd 23/02/2022 232862 4603720008209401HAM006AS
Glen Dimplex New Zealand Ltd 23/02/2022 232818 4603720008201401HAS011AS
Glen Dimplex New Zealand Ltd 24/02/2022 232952 4603720008236101AKL006AS
Glen Dimplex New Zealand Ltd 23/02/2022 232876 4603720008209301AKL006AS
Glen Dimplex New Zealand Ltd 23/02/2022 232775 4603720008202602CHC016BS
Glen Dimplex New Zealand Ltd 23/02/2022 232775 4603720008202602CHC016BS

So I should be able to get a subset for Denco, Devonport Chocolates, etc and print it to file.

Excel output 1:

Supplier Receipt PO Tracking
Denco Audio Ltd 23/02/2022 232960 AAAC0002630

Excel output 2:

Supplier Receipt PO Tracking
Devonport Chocolates 23/02/2022 232969 4519880002717001AKL001AP
Devonport Chocolates 23/02/2022 232843 4519880002716901AKL001AS
Parfait
  • 97,543
  • 17
  • 91
  • 116
Dazz
  • 1
  • 3
  • 2
    `library(purrr); data %>% split(.$Supplier) %>% iwalk(~ write.csv(.x, paste0(.y, ".csv")))` may do what you want. If not, see this answer about providing a more complete reproducible example: https://stackoverflow.com/a/5963610/17303805 – zephryl Feb 25 '22 at 01:51
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please don't post data in images. We can't easily use for testing. – MrFlick Feb 25 '22 at 01:52
  • @MrFlick noob effort from me! Is it better now? My code is almost irrelevant, as it seems using a loop is not the best way to approach this simple table. – Dazz Feb 25 '22 at 02:09
  • @zephryl appreciate the tip. I'm still writing my code using conventional syntax, not pipe operators, but I get the idea. I'll have a crack using 'split'. – Dazz Feb 25 '22 at 02:13
  • Oh my goodness, group_split from the (dplyr) package! – Dazz Feb 25 '22 at 02:38

0 Answers0