2016-12-31 122 views
0

我有一個數據集命名數據分割數據,並保存爲CSV全部的分割文件

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

它爲100000行,我想分割的車程,地理位置優越,城市這個數據也保存分割文件在不同的csv中。

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

現在這個代碼使它成爲一個列表。如何選擇不公開和保存不同的CSV文件時,所有拆分類型

EX-本田將有三個分割文件作爲Honda C ChicagoHonda B ChicagoHonda C New York

感謝

+0

你嘗試過這麼遠嗎?因爲有類似的問題已經在SO –

回答

1
# 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

,你可以很容易地使用一個循環。這應該不是100k左右線路的問題。

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) 

你只是從你的data.frame迭代模型,車庫和市filter他們的所有獨特的組合和出口臨時data.frame爲csv表。

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

只是爲了增加更多的選項,你可以使用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

好想法! –