I have a dataset named data
Model Garage City
Honda C Chicago
Maruti B Boston
Porsche A New York
Honda
you could easily use a loop. that shouldn't be a problem with 100k or so lines.
x <- read.table(text = "Model, Garage, City
Honda, C, Chicago
Maruti, B, Boston
Porsche, A, New York
Honda, B, Chicago
Honda, C, New York", sep = ",", header = TRUE)
x
# Model Garage City
# Honda C Chicago
# Maruti B Boston
# Porsche A New York
# Honda B Chicago
# Honda C New York
library(dplyr)
you just iterate over all unique combinations of Model, Garage and City, filter
them from your data.frame
and export the temporary data.frame
to a csv table.
uni <- unique(x[,c("Model", "Garage", "City")])
for (j in 1:nrow(uni)) {
i <- uni[j,]
tmp <- x %>% filter(Model == i$Model, Garage == i$Garage, City == i$City)
write.table(tmp, paste(i$Model, "_", i$City, "_", i$Garage, ".csv"))
}
Just to add more options, you can use data.table
:
library(data.table)
x <- as.data.table(x)
x[, write.table(.SD, paste("path/file_", Model, "_", Garage, "_", City, ".csv", sep = "")), by = c("Model", "Garage", "City")]
# create all combinations of data.frames possible based on unique values of Model, Garage, City
l = split(x, list(x$Model, x$Garage, x$City))
# create csv filrs only if data.frame had any rows in it
lapply(names(l), function(x) if(dim(l[[x]])[1] != 0){write.csv(l[[x]], paste0("path", x,".csv"))})