Skip to content

Instantly share code, notes, and snippets.

@ddbs
Last active September 2, 2021 00:09
Show Gist options
  • Save ddbs/3361eed5702b7a575682097c6d90ddeb to your computer and use it in GitHub Desktop.
Save ddbs/3361eed5702b7a575682097c6d90ddeb to your computer and use it in GitHub Desktop.
Export dataframe/datatable groups to individual excel files
library(data.table)
data <- rio::import("compensation1.xls")
data <- janitor::clean_names(data)
colnames(data)
filename <- tools::file_path_sans_ext(file) #get filename sans extension or path
getwd()
slicer <- c("manager_first_name")
prefix <- c("") #end with _
suffix <- c("") #start with _
setDT(data) # convert the data into a data.table
data[, rio::export(.SD, paste0(filename, "_", prefix, .BY, suffix, ".xlsx")), by = slicer]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment