0
votes

I have the data below, and I'm looking to filter it into some new data frames.

I'm curious if there's a better way of doing it than the following rather than having a separate line for each city and date?

vector.sydney = dplyr::filter(vector, grepl('Sydney', Location)) %>% group_by(Location) %>% filter(Date == '2018-12-07')
vector.adelaide = dplyr::filter(vector, grepl('Adelaide', Location)) %>% group_by(Location) %>% filter(Date == '2018-12-07')
vector.melbourne = dplyr::filter(vector, grepl('Melbourne', Location)) %>% group_by(Location) %>% filter(Date == '2018-12-07')
vector.sydney = dplyr::filter(vector, grepl('Sydney', Location)) %>% group_by(Location) %>% filter(Date == '2018-12-06')
vector.adelaide = dplyr::filter(vector, grepl('Adelaide', Location)) %>% group_by(Location) %>% filter(Date == '2018-12-06')
vector.melbourne = dplyr::filter(vector, grepl('Melbourne', Location)) %>% group_by(Location) %>% filter(Date == '2018-12-06')

I assume I can probably create a list of Locations eg: c("Sydney", "Melbourne", "Adelaide") but I can't figure out how I'd name the outputs.

Can anyone please point me in the right direction?

Dateframe

vector = structure(list(Date = c("6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "6-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18", "7-Dec-18"), Delivery = c("12-Jan-19", "22-Jan-19", "31-Jan-19", "19-Jun-19", "25-Jul-19", "7-Aug-19", "8-Aug-19", "9-Aug-19", "22-Aug-19", "27-Aug-19", "28-Aug-19", "29-Aug-19", "30-Aug-19", "3-Sep-19", "2-Oct-19", "17-Jan-19", "19-Jan-19", "8-Feb-19", "9-Feb-19", "8-Jun-19", "20-Jul-19", "7-Aug-19", "1-Oct-19", "11-Oct-19", "16-Oct-19", "25-Oct-19", "31-Oct-19", "2-Nov-19", "12-Nov-19", "19-Nov-19", "12-Jan-19", "22-Jan-19", "31-Jan-19", "8-Jun-19", "19-Jun-19", "25-Jun-19", "25-Jul-19", "7-Aug-19", "27-Aug-19", "28-Aug-19", "29-Aug-19", "30-Aug-19", "3-Sep-19", "2-Oct-19", "31-Oct-19", "12-Jan-19", "22-Jan-19", "31-Jan-19", "19-Jun-19", "25-Jun-19", "25-Jul-19", "7-Aug-19", "8-Aug-19", "9-Aug-19", "22-Aug-19", "27-Aug-19", "28-Aug-19", "29-Aug-19", "30-Aug-19", "2-Oct-19", "17-Jan-19", "8-Feb-19", "9-Feb-19", "8-Jun-19", "20-Jul-19", "7-Aug-19", "1-Oct-19", "11-Oct-19", "16-Oct-19", "25-Oct-19", "26-Oct-19", "31-Oct-19", "2-Nov-19", "12-Nov-19", "19-Nov-19", "12-Jan-19", "22-Jan-19", "31-Jan-19", "5-Jun-19", "8-Jun-19", "19-Jun-19", "25-Jun-19", "25-Jul-19", "7-Aug-19", "27-Aug-19", "28-Aug-19", "29-Aug-19", "30-Aug-19", "2-Oct-19", "31-Oct-19"), Item.No = c(675L, 440L, 648L, 29L, 605L, 511L, 686L, 656L, 229L, 92L, 331L, 384L, 379L, 332L, 461L, 84L, 418L, 535L, 254L, 90L, 721L, 984L, 342L, 197L, 562L, 327L, 991L, 225L, 514L, 34L, 790L, 675L, 95L, 195L, 407L, 495L, 187L, 529L, 914L, 728L, 839L, 981L, 30L, 728L, 610L, 218L, 265L, 890L, 127L, 2L, 412L, 800L, 65L, 358L, 8L, 737L, 966L, 778L, 617L, 984L, 280L, 374L, 113L, 101L, 310L, 389L, 426L, 572L, 925L, 972L, 611L, 672L, 362L, 145L, 963L, 595L, 186L, 640L, 359L, 714L, 898L, 985L, 93L, 324L, 208L, 629L, 969L, 622L, 235L, 411L), Rank = c(3L, 8L, 7L, 6L, 9L, 5L, 13L, 14L, 11L, 12L, 4L, 2L, 10L, 15L, 1L, 14L, 10L, 11L, 7L, 9L, 13L, 2L, 1L, 6L, 15L, 5L, 4L, 12L, 3L, 8L, 4L, 8L, 6L, 15L, 7L, 12L, 9L, 3L, 10L, 5L, 2L, 13L, 14L, 1L, 11L, 3L, 7L, 8L, 6L, 15L, 9L, 5L, 13L, 14L, 11L, 12L, 4L, 2L, 10L, 1L, 13L, 12L, 6L, 10L, 14L, 1L, 2L, 7L, 11L, 5L, 15L, 3L, 9L, 4L, 8L, 4L, 7L, 8L, 15L, 14L, 6L, 11L, 9L, 3L, 10L, 5L, 2L, 12L, 1L, 13L), Location = c("Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Sydney", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Adelaide", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne", "Melbourne")), class = "data.frame", row.names = c(NA, -90L))
1
your vector isn't complete. It's missing some brackets maybeRonak Shah
What do you do next with the data? If you are doing the same "something" to all six vector, you could also get away with filter(Date %in% c("2018-12-06","2018-12-07"), grepl("Sydney|Adelaide|Melbourne", Location)) %>% group_by(Location, Date) %>% "something". (Regardless, you can reduce each vector by moving the filter(Date==...) to within the first one, as in filter(vec, grepl(...), Date==...).)r2evans

1 Answers

1
votes

Perhaps you're looking for split? It'll give you a list back, but that should be just as functional.

dfs <- vector %>% split(list(.$Location, .$Date))

You can then access the individual data frames by "Location.Date" indexing:

> dfs$`Sydney.7-Dec-18`
       Date  Delivery Item.No Rank Location
46 7-Dec-18 12-Jan-19     218    3   Sydney
47 7-Dec-18 22-Jan-19     265    7   Sydney
48 7-Dec-18 31-Jan-19     890    8   Sydney
49 7-Dec-18 19-Jun-19     127    6   Sydney
50 7-Dec-18 25-Jun-19       2   15   Sydney
51 7-Dec-18 25-Jul-19     412    9   Sydney
52 7-Dec-18  7-Aug-19     800    5   Sydney
53 7-Dec-18  8-Aug-19      65   13   Sydney
54 7-Dec-18  9-Aug-19     358   14   Sydney
55 7-Dec-18 22-Aug-19       8   11   Sydney
56 7-Dec-18 27-Aug-19     737   12   Sydney
57 7-Dec-18 28-Aug-19     966    4   Sydney
58 7-Dec-18 29-Aug-19     778    2   Sydney
59 7-Dec-18 30-Aug-19     617   10   Sydney
60 7-Dec-18  2-Oct-19     984    1   Sydney

or you can always assign new names if you'd prefer

names(dfs) <- c("adelaide.12.6", "melbourne.12.6", "sydney.12.6",
                "adelaide.12.7", "melbourne.12.7", "sydney.12.7")