Split data in r and save all the split files in csv

前端 未结 3 795
情话喂你
情话喂你 2021-01-26 01:27

I have a dataset named data

  Model Garage        City    
  Honda      C     Chicago       
 Maruti      B      Boston  
Porsche      A    New York    
  Honda          


        
相关标签:
3条回答
  • 2021-01-26 02:06

    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"))
    }
    
    0 讨论(0)
  • 2021-01-26 02:11

    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")]
    
    0 讨论(0)
  • 2021-01-26 02:12
    # 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"))})
    
    0 讨论(0)
提交回复
热议问题