How to get subsets by unique values

Is there a function to get all subsets by their each unique value available in 'Department' column ? Can this be filter by unique value and copy --> create a dataframe ?

ID Department Strength DoD Group_ID
82950 Training 96 953 A
32045 Training 86 5277 A
84817 Sales 77 1633 C
2857 Logistics 88 5629 Z
55587 Training 46 6710 L
73103 Sales 51 1892 M
5837 Sales 62 2394 A
7140 Sales 81 2854 C
70450 Logistics 56 5449 A
34207 Manufacturing 85 1332 S
20095 Manufacturing 58 4589 X
30200 Sales 91 1094 W
47211 Manufacturing 36 5862 C
8932 Manufacturing 75 4847 A
82663 Manufacturing 39 5798 A

What I expect to see;
Logistics.xlsx

ID Department Strength DoD Group_ID
2857 Logistics 88 5629 Z
70450 Logistics 56 5449 A

Manufacturing.xlsx

ID Department Strength DoD Group_ID
34207 Manufacturing 85 1332 S
20095 Manufacturing 58 4589 X
47211 Manufacturing 36 5862 C
8932 Manufacturing 75 4847 A
82663 Manufacturing 39 5798 A

and so on.....

Hello,

Is this what you had in mind? (I just haven't unpacked the list object yet but you can easily save each seperately with the right name)

library(tidyverse)


df <- data.frame(
  stringsAsFactors = FALSE,
                ID = c(82950L,32045L,84817L,2857L,
                       55587L,73103L,5837L,7140L,70450L,34207L,20095L,
                       30200L,47211L,8932L,82663L),
        Department = c("Training","Training","Sales",
                       "Logistics","Training","Sales","Sales","Sales",
                       "Logistics","Manufacturing","Manufacturing","Sales",
                       "Manufacturing","Manufacturing","Manufacturing"),
          Strength = c(96L,86L,77L,88L,46L,51L,
                       62L,81L,56L,85L,58L,91L,36L,75L,39L),
               DoD = c(953L,5277L,1633L,5629L,
                       6710L,1892L,2394L,2854L,5449L,1332L,4589L,1094L,5862L,
                       4847L,5798L),
          Group_ID = c("A","A","C","Z","L","M",
                       "A","C","A","S","X","W","C","A","A")
)


filter_values <- df %>% select(Department) %>% distinct() %>% unlist()



df_list <- list()

for (i in 1:length(filter_values)) {
  df_list[[i]] <-  df %>% filter(Department == filter_values[i])
  
}

df_list[[1]]
#>      ID Department Strength  DoD Group_ID
#> 1 82950   Training       96  953        A
#> 2 32045   Training       86 5277        A
#> 3 55587   Training       46 6710        L

df_list[[2]]
#>      ID Department Strength  DoD Group_ID
#> 1 84817      Sales       77 1633        C
#> 2 73103      Sales       51 1892        M
#> 3  5837      Sales       62 2394        A
#> 4  7140      Sales       81 2854        C
#> 5 30200      Sales       91 1094        W

df_list[[3]]
#>      ID Department Strength  DoD Group_ID
#> 1  2857  Logistics       88 5629        Z
#> 2 70450  Logistics       56 5449        A

df_list[[4]]
#>      ID    Department Strength  DoD Group_ID
#> 1 34207 Manufacturing       85 1332        S
#> 2 20095 Manufacturing       58 4589        X
#> 3 47211 Manufacturing       36 5862        C
#> 4  8932 Manufacturing       75 4847        A
#> 5 82663 Manufacturing       39 5798        A

Created on 2020-10-11 by the reprex package (v0.3.0)

1 Like

Here is another approach giving a result similar to @GreyMerchant's code.

library(purrr)
DF <- data.frame(ID = 1:10, 
                 Department = c("T", "L", "T", "S", "L", "S", "T", "M", "S", "M"))
Depts <- unique(DF$Department)
names(Depts) <- Depts
MyFunc <- function(Nm, D, Dpt) D[D[[Dpt]] == Nm,]
DataFrames <- map(Depts, .f = MyFunc, D = DF, Dpt = "Department")
DataFrames
#> $T
#>   ID Department
#> 1  1          T
#> 3  3          T
#> 7  7          T
#> 
#> $L
#>   ID Department
#> 2  2          L
#> 5  5          L
#> 
#> $S
#>   ID Department
#> 4  4          S
#> 6  6          S
#> 9  9          S
#> 
#> $M
#>    ID Department
#> 8   8          M
#> 10 10          M

Created on 2020-10-11 by the reprex package (v0.3.0)

2 Likes

This is fantastic. Thank you @GreyMerchant

Also, you mentioned something about unpacking.

df1 <-- df_list[[1]] gives me a subset for department 'Training'. Is there a way to automatically unlist one by one by their department and save as department.xlsx ?

see this post to get additional details:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.