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

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

提交回复
热议问题