I have a file with 25 million rows and need to split it into smaller files based on the factor levels. I created a dataframe to include distinct factor levels and wrote a loop to perform some operations and write out a csv
.
data looks like this:
Country Col2 Code Year
A C 1 2020
A D 1 2020
A C 1 2020
A D 2 2020
A C 2 2020
A D 2 2020
A C 2 2020
A D 3 2020
Intention is to write a csv file for every subset based on code
d1 <- data %>%
distinct(Code)
for(i in 1:nrow(d1))
{
subset <- data %>%
filter(Code == Code[i])
co <- subset$Code[i]
yr<- subset$Year[i]
setwd("C:/Users/...")
write.csv(subset, paste(co,"_",Year, ".csv", sep=""), append = FALSE, row.names = FALSE)
}
The output keeps getting written to the same file instead of creating separate files in the directory.
IS there any better way of doing this? Thank you.