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

前端 未结 3 819
情话喂你
情话喂你 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"))
    }
    

提交回复
热议问题