2016-03-11 83 views
0

我想弄清楚如何将CSV文件分成小块。我想分割任何数量或行。也许20,1000,或其他。如何将CSV文件分成小块?

setwd("C:/Users/my_path/test_folder/") 
mydata = read.csv("NHLData.csv") 


split(mydata, ceiling(seq_along(mydata)/20)) 

错误:警告消息:在split.default(X = seq_len(nrow(X))中,f = F,一滴一滴=,...):数据长度不是分裂可变

的倍数

我也试过这个。

split(mydata, ceiling(seq_along(mydata)/(length(mydata)/20))) 

相同的错误:警告消息:在split.default(X = seq_len(nrow(X))中,f = F,一滴一滴=,...):数据长度不分裂可变的倍数

我为这些想法Google搜索。我没有真正发现其他任何有用的东西。这一定非常简单,没错。

+0

http://stackoverflow.com/questions/14164525/splitting-a-large-data-frame-into-smaller-segments有几个解决方案 –

+0

'read.csv中'skip'和'nrows'的组合'会给你所有需要阅读的任何你想要的csv文件的行... – cory

+0

Ryguy72(72),不要多个帐户。 [见这里](http://meta.stackexchange.com/help/merging-accounts)了解如何合并它们。 –

回答

0

利用'样本'功能,这将有所帮助。

setwd("C:/Users/my_path/test_folder/") 
mydata = read.csv("NHLData.csv") 

# If you want 5 different chunks with same number of lines, lets say 30. 
Chunks = split(mydata,sample(rep(1:5,30))) ## 5 Chunks of 30 lines each 

# If you want 20 samples, put any range of 20 values within the range of number of rows 
First_chunk <- sample(mydata[1:20,]) ## this would contain first 20 rows 

# Or you can print any number of rows within the range 
Second_chunk <- sample(mydata[100:70,] ## this would contain last 30 rows in reverse order if your data had 100 rows. 

# If you want to write these chunks out in a csv file: 
write.csv(First_chunk,file="First_chunk.csv",quote=F,row.names=F,col.names=T) 
write.csv(Second_chunk,file="Second_chunk.csv",quote=F,row.names=F,col.names=T) 

希望这对我有所帮助。