本文介绍了在R中分割数据并将所有分割文件保存在CSV中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为data的数据集

I have a dataset named data

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

这是100000行,我想按汽车,位置和城市分割此数据,并将分割后的文件保存在不同的csv文件中.

It is 100000 rows and I want to split this data by car,location and city and also save the split files in different csv's.

split(Data, with(Data, interaction(Model,City,Garage)), drop = TRUE)

现在此代码将其列为列表.如何取消列出并保存所有拆分类型的不同csv文件

Now this code makes it a list. How do I unlist and save different csv files for all split types

Ex- Honda将具有三个分割文件,分别为Honda C ChicagoHonda B ChicagoHonda C New York

Ex- Honda will have three split files as Honda C Chicago, Honda B Chicago and Honda C New York

谢谢

推荐答案

# 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"))})

这篇关于在R中分割数据并将所有分割文件保存在CSV中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 10:52