This is an example of how to do it, this produces independent password protected xlsx files by each unique value on the "Department" column.
library(dplyr)
library(purrr)
library(xlsx)
# Sample data on a copy/paste friendly format
sample_df <- data.frame(
stringsAsFactors = FALSE,
ID = c(82950,32045,84817,2857,
55587,73103,5837,7140,70450,34207,20095,30200,47211,
8932,82663),
Department = c("Training","Training","Sales",
"Logistics","Training","Sales","Sales","Sales",
"Logistics","Manufacturing","Manufacturing","Sales",
"Manufacturing","Manufacturing","Manufacturing"),
Strength = c(96,86,77,88,46,51,62,81,
56,85,58,91,36,75,39),
DoD = c(953,5277,1633,5629,6710,
1892,2394,2854,5449,1332,4589,1094,5862,4847,5798),
Group_ID = c("A","A","C","Z","L","M",
"A","C","A","S","X","W","C","A","A")
)
sample_df %>%
group_nest(Department) %>%
walk2(.x = .$Department,
.y = .$data,
.f = ~ write.xlsx(x = .y,
file = paste(.x, ".xlsx"),
password = "1234")
)