This is the data I am working with
Sales <- data.frame(State = c("New York", "New Jersey", "Texas","New Mexico","California",
"Kansas","Florida","Alaska","Montana", "Maine"),
Units = c(455,453,125,135,135,568,451,125,215,314),
Sales = c("20000","12530","51110","54110","65000",
"58220","54612","45102","45896","12510"),
ManagerName = c("Chip","Kelly","Steve","Young","Troy",
"Aikman", "Chad","Pennington","Mike","Vick")
)
My end goal is to have 10 different excel files for each "State".
I know I can do something like
#Seperate by State
library(dplyr)
NewYork <- Sales %>% filter(State == "New York")
#Save to excel
library(xlsx)
write.xlsx(NewYork, file="NewYork-Sales-November2020.xlsx",
sheetName="NewYork")
But the problem is that I have over 50+ rows and do not want to have 50 commands to filter by state and then another 50 commands to write an excel file.
Is there a more efficient way to save each row "State" into an different excel files?